Make this C# code usable in UiPath

Hi community,

I am trying to run the below C sharp code in UiPath using invoke code activity. I know the regular C sharp codes do not work in invoke code activity directly, can any one help me in compacting/ making the below code running state using invoke code activity.

 public virtual ActionResult Download()
    {          
        ClientContext ctx = new ClientContext("https://ABC.sharepoint.com");
        SecureString passWord = new SecureString();
        foreach (char c in "Pass@word12".ToCharArray()) passWord.AppendChar(c);
        ctx.Credentials = new SharePointOnlineCredentials("abc@gmail12.com", passWord);
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
        var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(ctx, "/sites/Test/testdoc.docx");        
        return File(fileInfo.Stream, "application/octet-stream", "testdoc.docx");
    }

Any helps much appreciated

Thanks

Hi @ppr @ptrobot @Yoichi can you please have a look on this problem and provide any inputs?

Thanks

Hey @Dhruvi_Arumugam

ClientContext ctx = new ClientContext("https://ABC.sharepoint.com");
SecureString passWord = new SecureString();
foreach (char c in "Pass@word12".ToCharArray()) passWord.AppendChar(c);
ctx.Credentials = new SharePointOnlineCredentials("abc@gmail12.com", passWord);
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(ctx, "/sites/Test/testdoc.docx");

fileInfo will be an out argument in the invoke workflow activity.

Hope this helps

Thanks
#nK

Hi @Nithinkrishna,

I tried this, it is giving me error - The Type or Namespace ClientContext could not be found. Can you please guide?

1 Like

Hey @Dhruvi_Arumugam

Did you installed the library and imported in studio ?

Check in your Visual studio using statements section !

Thanks
#nK

Hi @Dhruvi_Arumugam ,

You would require to install the Below Package for Resolving Namespace Validation issues I suppose :
image

or Install the below Dependencies of the Package separately, if not present already by Checking in the Imports Section :
image

We do have the Sharepoint Activities that should do a Similar Operation as you are trying to perform using the c# Code, you could use it too instead of the Code.

Hi @supermanPunch,

I tried with the package by Radu Bucur but that it is not giving me exactly what i want, i will try with the dependencies you mentioned

@Dhruvi_Arumugam ,

If you have the Package already Installed, then it shouldn’t give you the Problem of namespaces for your c# code.

Hi @supermanPunch,

I tried importing dependencies you mentioned but I am not able to find the below ones in import section

  • Microsoft.Data.Services.Client
    -Microsoft.Sharepoint2019.CSOM
    -SharePointPnPCore2016

Can you please help!

Hi,

How about the following?

Microsoft.SharePoint.Client.ClientContext ctx = new Microsoft.SharePoint.Client.ClientContext("https://ABC.sharepoint.com");
System.Security.SecureString passWord = new System.Security.SecureString();
foreach (char c in "Pass@word12".ToCharArray()) passWord.AppendChar(c);
ctx.Credentials = new Microsoft.SharePoint.Client.SharePointOnlineCredentials("abc@gmail12.com", passWord);
 System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(ctx, "/sites/Test/testdoc.docx");        
using(Stream outStream = File.OpenWrite(@"C:\temp\testdoc.docx"))
{
   fileInfo.Stream.CopyTo(outStream);
 }

Hi @Dhruvi_Arumugam,

As @supermanPunch mentioned install that dependencies and use the activity instead of code. It will be very easy to download the files using those activity

  1. Use “Sharepoint Application Scope” activity provide your sharepoint site link and credential eg: “https://ABC.sharepoint.com/Sites/Yoursitename
  2. Then use “Get File” activity to download the file eg: Item url "Shared Documents/Test/testdoc.docx" and local path "C:\temp\testdoc.docx"

Hi can you please guide me where can i learn c share advance ?

Hi,

Basically, the above code is added just proper namespace to the original code. (I re-wrote code regarding System.Web.Mvc.ActionResult)

We can get information for Microsoft.SharePoint.Client in details in the following document, for example.

Regards,

1 Like

Just one question where can i get complete list of predefined class in Microsoft.doc

Hi,

Just one question where can i get complete list of predefined class in Microsoft.doc

The following helps you.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.