4

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?

Taterhead
  • 624
  • 5
  • 19
Obiliraj
  • 547
  • 2
  • 7
  • 21
  • 1
    See similar question on StackOverflow http://stackoverflow.com/questions/17057074/how-to-download-upload-files-from-to-sharepoint-2013-using-csom/21056425 – Rob Windsor Mar 09 '16 at 04:58

4 Answers4

6

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.

Dikesh Gandhi
  • 6,803
  • 4
  • 30
  • 55
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");
Venkat Konjeti
  • 4,959
  • 1
  • 10
  • 19
  • The / in /SharedDocuments caused an error for me. See this answer: https://sharepoint.stackexchange.com/a/214751/47303 – Chris Mar 22 '18 at 14:02
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);
 }
 }
moe
  • 5,267
  • 21
  • 34
  • 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