Help required in below C sharp code to run in Uipath

Hi Community,

I am trying to download a folder from Sharepoint using below code in Uipath but it giving me dependencies issue and I am not sure will this code directly run or not. Any help will be appreciated.

static void Main(string[] args)
        {
            string _SiteUrl = "https://domain.sharepoint.com/sites/tst";
            using (var clientContext = new ClientContext(_SiteUrl))
            {
                Console.ForegroundColor = ConsoleColor.Green;
                string password = "pw";
                SecureString sec_pass = new SecureString();
                Array.ForEach(password.ToArray(), sec_pass.AppendChar);
                sec_pass.MakeReadOnly();
                clientContext.Credentials = new SharePointOnlineCredentials("lee", sec_pass);

                Web web = clientContext.Web;
                Folder folder = web.GetFolderByServerRelativeUrl("/sites/TST/MyDoc4/Folder");
                var files = folder.Files;
                clientContext.Load(files);
                clientContext.ExecuteQuery();
                Console.WriteLine();
                //Regex regex = new Regex(_SiteUrl, RegexOptions.IgnoreCase);

                var list = web.Lists.GetByTitle("MyDoc4");
                var libRootFolder = list.RootFolder;
                var subFolder = libRootFolder.Folders.GetByUrl("Folder");
                clientContext.Load(libRootFolder);
                clientContext.Load(subFolder);
                clientContext.ExecuteQuery();
                Console.WriteLine(libRootFolder.ItemCount);
                Console.WriteLine(subFolder.ItemCount);


                using (System.IO.MemoryStream mStream = new System.IO.MemoryStream())
                {
                    using (var archive = new ZipArchive(mStream, ZipArchiveMode.Create, true))
                    {
                        foreach (var file in files)
                        {
                            clientContext.Load(file);
                            Console.WriteLine(file.Name);
                            ClientResult<Stream> stream = file.OpenBinaryStream();
                            clientContext.ExecuteQuery();

                            var zipArchiveEntry = archive.CreateEntry(file.Name);
                            using (Stream zipEntryStream = zipArchiveEntry.Open())
                            {

                                    if (stream != null)
                                    {
                                        stream.Value.CopyTo(zipEntryStream);
                                    }

                            }
                        }
                    }
                    using (var fileStream = new FileStream(@"C:\Lee\FileDownload\test.zip", FileMode.Create))
                    {
                        mStream.Seek(0, SeekOrigin.Begin);
                        mStream.CopyTo(fileStream);
                    }
                }


                Console.WriteLine("done");
                Console.ReadKey();
            }

        }

@Yoichi @supermanPunch @Nithinkrishna @ppr @ptrobot can you please also help mere if possible.

thanks

we would also recommend checking if this task can be done with activities from packages offered by UiPath. In general to minimize Invoke Code caused black boxes are preferable.

Hi @ppr,

I have checked and it is not possible by activities.

Thanks

Hey @Dhruvi_Arumugam

It will run with few modifications… I hope you want to run it using invoke code right !

Thanks
#nK

Yes, through Invoke Code - C sharp!!

Hi @Dhruvi_Arumugam

Why don’t you try custom SharePoint activities available in uipath marketplace.

Or

Visit this case of uipath forum

Regards,

Hi @Aparna_Chaudhari,

thanks for the help, whereas I need to download multiple files that are inside folders, the above link provides a code to download one file by link.

Let me know if you have any solutions on that as well

Thanks!

Hey @Dhruvi_Arumugam

Just try looping through the code using the file list !

Thanks
#nK

Hi @Dhruvi_Arumugam,

As others have suggested I adivse you to use the functionality in the packages offered by UiPath. However, if you’re adamant about using the code you supplied I recommend you that you use a proper IDE with console functionality. Several lines of this code writes to the console which will help you with further troubleshooting.

You can also consider using activity “Invoke Power Shell” with the PowerShell module PNP Powershell, which has pre-built lightweight commands for downloading files located in SharePoint Online. Please see the link below: