1

I have written a method that reads excel files from a certain folder in SharePoint and saves them in Desktop. This is my code , but it doesn't work because I don't see the file anywhere in the Desktop of my computer.

The connection with SharePoint is made in another method.

private static void transferFile(Microsoft.SharePoint.Client.Folder fold, 
ClientContext ctx,string targetPath, ref StringBuilder str, ref bool errors)
    {
        str.AppendLine("Folder " + fold.Name);
        ctx.Load(fold.Files, f1 => f1.Include(f2 => f2.Name, f2 => f2.ListItemAllFields));
        ctx.ExecuteQuery();

        foreach (Microsoft.SharePoint.Client.File file in fold.Files)
        {
            Dictionary<string, object> fieldsVal = file.ListItemAllFields.FieldValues;


            string extension = file.Name.Substring(file.Name.Length - 4);
            try
            {
                if (extension == "xlsx")
                {

                    string sourceUrl = "https://mysharepoint.com" + fieldsVal["FileRef"].ToString();

                    int size = int.Parse(fieldsVal[System.Xml.XmlConvert.EncodeName("File Size")].ToString());

                    try
                    {
                        int length = 0;
                        int bytesRead = 0;
                        int chunkSize = 32 * 1024;
                        byte[] buffer = new byte[chunkSize];

                        FileInformation ffl = Microsoft.SharePoint.Client.File.OpenBinaryDirect(ctx, fieldsVal["FileRef"].ToString());
                        MemoryStream stream = new MemoryStream();
                        while ((bytesRead = ffl.Stream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            length += bytesRead;
                            stream.Write(buffer, 0, bytesRead);
                        }

                        Stream streamFile = null;
                        string fileName;

                        streamFile = stream;
                        fileName = file.Name;

    var newFileName = Path.Combine(targetPath,fileName);
    var fileStream = System.IO.File.Create(newFileName);

     ffl.Stream.CopyTo(fileStream);
    }
    catch{
    }
    }
    catch
    {
    }
    }
    }  
  • What is the error you are getting if any? What is the issue ? – Unnie Jun 20 '17 at 14:03
  • I'm not getting any errors – angie Jun 20 '17 at 14:05
  • You're not getting errors because you're swallowing the exceptions in your empty catch blocks. – Derek Gusoff Jun 20 '17 at 16:44
  • It was a null reference exception, I fixed it – angie Jun 21 '17 at 8:25
0

You can download the file from SharePoint online to your local system using following code

internal static void DownloadFilesFromSharePoint(string siteUrl, string folderPath, string tempLocation)
{
    ClientContext ctx = new ClientContext(siteUrl);
    ctx.Credentials = new SharePointOnlineCredentials(Username, Password);

    FileCollection files = ctx.Web.GetFolderByServerRelativeUrl(folderPath).Files;

    ctx.Load(files);
    ctx.ExecuteQuery();

    foreach(File file in files)
    {
        FileInformation fileInfo = File.OpenBinaryDirect(ctx, file.ServerRelativeUrl);
        ctx.ExecuteQuery();

        var filePath = tempLocation + file.Name;
        using (var fileStream = new System.IO.FileStream(filePath, System.IO.FileMode.Create))
        {
            fileInfo.Stream.CopyTo(fileStream);
        }
    }
}

Here folderPath is a SharePoint folder relative path (e.g.: \site\libraryname\folder).

You can add your condition to check your file is .xlsx in foreach loop as follows

foreach(File file in files)
{
    if(file.Name.Contains(".xlsx")) {

        FileInformation fileInfo = File.OpenBinaryDirect(ctx, file.ServerRelativeUrl);
        ......
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.