Desktop productivity for business analysts and programmers

Custom Task: add file as reference

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 118
Accepted Solution

Custom Task: add file as reference

I am aware that this is a question only @ChrisHemedinger may be able to answer, but i dare to ask anyway.

 

I am developing a custom task which creates a new file in the Windows file system (e.g. Excel). What i want to achieve is that after the custom task is executed a reference to this newly created file is added to the process flow.

I read the book "Custom Tasks for SAS Enterprise Guide Using Microsoft .NET" and tried the code from chapter 12 which seems to be close to the solution.

 

However this (simplified) code:

 public ISASTaskStream OpenResultStream(int Index)
  {
    var memoryStream = new MemoryStream(File.ReadAllBytes(Settings.ExcelOutputPath));

    var fileInfo = new FileInfo(Settings.ExcelOutputPath);

    return new StreamAdapter(memoryStream
                            , "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"// "application/vnd.ms-excel"
                            , fileInfo.Name);
  }

writes a full copy of the file to the process flow and embeds the file into the workflow:

2015-12-14 13_57_24-SAS Enterprise Guide - SampleProject.egp.png

 

I know that there are plenty of ways to generate an Excel file, but what i would like to achieve is a linked reference in the process flow like the "export as step in process" task does:

2015-12-14 13_59_38-SAS Enterprise Guide.png

 

So can anybody provide me a C# code snippet for adding a file reference or tell me which classes are involved?

 

Best Regards,

Andreas


Accepted Solutions
Solution
‎01-14-2016 04:19 AM
Community Manager
Posts: 2,692

Re: Custom Task: add file as reference

I've looked into this a bit, and don't yet have a solution.  But I'll share some things I've tried in case you want to hammer at them further.

 

First, I tried adding just a "file" as an output data item in the Task class.  

 

public override System.Collections.Generic.List<ISASTaskDataDescriptor> OutputDataDescriptorList
{
    get
    {
        ISASTaskDataDescriptor d = 
     SAS.Shared.AddIns.SASTaskDataDescriptor.CreateLocalDataDescriptor
      ("c:\temp\sample.xlsx", "Sample.xlsx");
        System.Collections.Generic.List<ISASTaskDataDescriptor> outList =
           new System.Collections.Generic.List<ISASTaskDataDescriptor>();
        outList.Add(d);
        return outList;
    }
}

Also added this an an attribute on the Task class:

[SAS.Shared.AddIns.CreatesDataDirectly(true)]

But no success.  I think that EG doesn't add the result because it's not a true data reference...but I haven't debugged enough to verify.

 

Another approach to add a file to a project.  With EG open, you can launch the SEGuide command with the /openfile command-line switch and add a file reference to the existing instance of EG.  You could tell .NET to take the existing Process command and form your own variation with the command-line option.  Example:

 

"C:\Program Files (x86)\SASHome\x86\SASEnterpriseGuide\7.1\SEGuide.exe" 
     /openfile:c:\temp\sample.xlsx

But, EG automatically launches the Import Data task for most files that you try to add in this way...because of course that's what EG does with files like Excel spreadsheets.  And even if you do get the file added to the project, it won't be linked from your task as output.

View solution in original post


All Replies
Community Manager
Posts: 2,692

Re: Custom Task: add file as reference

@AndreasMenrath - it sounds like you want to add a reference to an external file as a linked item in the flow, correct?

 

I'm not sure it's possible with the Custom Task APIs, but I will dig in and see if I can find something creative.

Frequent Contributor
Posts: 118

Re: Custom Task: add file as reference

@ChrisHemedinger: exactly.

What i want is just a reference like when an external file is drag&dropped into the process flow from the Windows explorer or like the file reference in the "export as step in process" task.

 

Thank you for looking into the solution of this issue :-)

Solution
‎01-14-2016 04:19 AM
Community Manager
Posts: 2,692

Re: Custom Task: add file as reference

I've looked into this a bit, and don't yet have a solution.  But I'll share some things I've tried in case you want to hammer at them further.

 

First, I tried adding just a "file" as an output data item in the Task class.  

 

public override System.Collections.Generic.List<ISASTaskDataDescriptor> OutputDataDescriptorList
{
    get
    {
        ISASTaskDataDescriptor d = 
     SAS.Shared.AddIns.SASTaskDataDescriptor.CreateLocalDataDescriptor
      ("c:\temp\sample.xlsx", "Sample.xlsx");
        System.Collections.Generic.List<ISASTaskDataDescriptor> outList =
           new System.Collections.Generic.List<ISASTaskDataDescriptor>();
        outList.Add(d);
        return outList;
    }
}

Also added this an an attribute on the Task class:

[SAS.Shared.AddIns.CreatesDataDirectly(true)]

But no success.  I think that EG doesn't add the result because it's not a true data reference...but I haven't debugged enough to verify.

 

Another approach to add a file to a project.  With EG open, you can launch the SEGuide command with the /openfile command-line switch and add a file reference to the existing instance of EG.  You could tell .NET to take the existing Process command and form your own variation with the command-line option.  Example:

 

"C:\Program Files (x86)\SASHome\x86\SASEnterpriseGuide\7.1\SEGuide.exe" 
     /openfile:c:\temp\sample.xlsx

But, EG automatically launches the Import Data task for most files that you try to add in this way...because of course that's what EG does with files like Excel spreadsheets.  And even if you do get the file added to the project, it won't be linked from your task as output.

Frequent Contributor
Posts: 118

Re: Custom Task: add file as reference

Hi @ChrisHemedinger

 

thank you for your suggestion. It is funny, but your suggested code works on my machine.

It successfully adds a referenced file to the process flow.

 

I think EG is case sensitive on the file name. Maybe that's the reason why your code did not run on your machine.

Using this just works fine:

var fileInfo = new FileInfo(@"C:\temp\Sample.xlsx");
ISASTaskDataDescriptor d = SASTaskDataDescriptor.CreateLocalDataDescriptor(fileInfo.FullName, fileInfo.Name);

Thank you and best regards,

Andreas

Community Manager
Posts: 2,692

Re: Custom Task: add file as reference

Well, I'm not going to question it. If it works for you, that's great!


Frequent Contributor
Posts: 118

Re: Custom Task: add file as reference

[ Edited ]

@ChrisHemedinger

 

I also noticed a common C# error in your code:
you used the string "c:\temp\sample.xlsx" inside your code.

The backslash in a string is used for masking, so \t is translated to a tabulator character.

So using double backslashes "c:\\temp\\sample.xlsx" oder using a verbatim string with @"c:\temp\sample.xlsx" should also fix your code :-)

Best Regards
Andreas

Community Manager
Posts: 2,692

Re: Custom Task: add file as reference

Dang, you're right! Rookie mistake!


☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 480 views
  • 3 likes
  • 2 in conversation