3

Hi I am having a requirement to download files from doc library using c# console app. the below is the code I use.

 ClientContext context = new ClientContext("https://siteurl");
        context.Credentials = new NetworkCredential("userid", "password", "domain");
        context.Load(context.Web);
        List list = context.Web.Lists.GetByTitle("document library");
        context.Load(list);
        context.ExecuteQuery();
        CamlQuery query = new CamlQuery();
        query.ViewXml = "<view/>";
        ListItemCollection licoll = list.GetItems(query);
        context.Load(licoll);
        context.ExecuteQuery();
        foreach(ListItem li in licoll)
        {
            Microsoft.SharePoint.Client.File file = li.File;
            if(file!=null)
            {
                //how to code this block to download the file?
            }
        }

how to download the file selected?

2

Try this IF Block below:

Code:

 if (file != null)
 {

     FileInformation fileInfo = file.OpenBinaryDirect(context, fileRef.ToString());

var fileName = Path.Combine(filePath,(string)listItem.File.Name);
 using (var fileStream = System.IO.File.Create(fileName))
 {                  
      fileInfo.Stream.CopyTo(fileStream);
 }
 }

Hope this will help you.

1

For SharePoint online, you can use following way to generate a client context and download a file to a specific location in your local machine.

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);
        }
    }
}

You can call the above method as follows

DownloadFilesFromSharePoint("https://tenant.sharepoint.com", "/SharedDocuments", @"c:\downloads");
0

If you need to upload files to a SharePoint site please visit the following link where it is explained how to read and upload files using CSOM

https://stackoverflow.com/a/53733630/10777492

-2
if (file != null)
 {

     FileInformation fileInfo = File.OpenBinaryDirect(context, fileRef.ToString());

var fileName = Path.Combine(filePath,(string)listItem.File.Name);
 using (var fileStream = System.IO.File.Create(fileName))
 {                  
      fileInfo.Stream.CopyTo(fileStream);
 }
 }
  • 2
    While the answer might do what requested, it's recommended to add some description/commenting for it to complete it as an answer. – moe Jun 24 '17 at 12:19

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.