Get all items of a sharepoint list

17,484

Since your goal is:

to download all files of my SharePoint list

the following example demonstrates how to accomplish it:

using (var ctx = new ClientContext(webUri))
{
     var qry = new CamlQuery();
     qry.ViewXml = "<View Scope='RecursiveAll'>" +
                              "<Query>" + 
                                  "<Where>" + 
                                        "<Eq>" + 
                                             "<FieldRef Name='FSObjType' />" + 
                                             "<Value Type='Integer'>0</Value>" + 
                                        "</Eq>" + 
                                 "</Where>" + 
                               "</Query>" + 
                            "</View>"; 

    var sourceList = ctx.Web.Lists.GetByTitle(sourceListTitle);
    var items = sourceList.GetItems(qry);
    ctx.Load(items);
    ctx.ExecuteQuery();
    foreach (var item in items)
    {
        //1. ensure target directory exists
        var curPath = targetPath + System.IO.Path.GetDirectoryName((string)item["FileRef"]);
        Directory.CreateDirectory(curPath);
        //2. download a file
        DownloadAFile(item, curPath);   
     }
 }

where

    private static void DownloadAFile(Microsoft.SharePoint.Client.ListItem item,string targetPath)
    {
        var ctx = (ClientContext)item.Context;
        var fileRef = (string)item["FileRef"];
        var fileName = System.IO.Path.GetFileName(fileRef);
        var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(ctx, fileRef);
        var filePath = Path.Combine(targetPath, fileName);
        using (var fileStream = System.IO.File.Create(filePath))
        {
            fileInfo.Stream.CopyTo(fileStream);
        }
    }

Note: compatible with SharePoint 2010/2013 CSOM API

As a bonus, the folder structure will be preserved once files are downloaded

Share:
17,484
saaami11
Author by

saaami11

Updated on June 04, 2022

Comments

  • saaami11
    saaami11 over 1 year

    i want to download all files of my sharepoint list. I download the files with this method:

      public void DownloadFilesOfSpecialtys( )
           {
    
    
               using (var clientContext = new ClientContext(url))
               {
    
                   foreach (var item in ids)
                   {
    
                       int listItemId = int.Parse(item.ToString());
                       statics.files = int.Parse(ids.Count.ToString());
    
                       var list = clientContext.Web.Lists.GetByTitle(listtitle);
                       var listItem = list.GetItemById(listItemId);
                       clientContext.Load(list);
                       clientContext.Load(listItem, i => i.File);
                       clientContext.ExecuteQuery();
    
                       var fileRef = listItem.File.ServerRelativeUrl;
                       var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, fileRef);
                       var fileName = Path.Combine(@path , (string)listItem.File.Name);
    
    
    
                       using (var fileStream = System.IO.File.Create(fileName))
                       {
                           fileInfo.Stream.CopyTo(fileStream);
                       }
    
    
                   }
    
    
               }
    
           }
    

    Now my Problem is, that the files in the sub folders arent downloaded with this methode. Is there an opportunityto go through all sub folders in my list and download all files?

  • saaami11
    saaami11 over 8 years
    listItemAsFolder.SubFolder does not exist in my case
  • LInsoDeTeh
    LInsoDeTeh over 8 years
    it's SubFolders (with s), which is a collection of all subfolders of the folder
  • saaami11
    saaami11 over 8 years
    Yeah Subfolders i write it false sry, is no availible
  • LInsoDeTeh
    LInsoDeTeh over 8 years
    As any other objects, you have to load them in context first: var folders = listItemAsFolder.SubFolders; clientContext.Load(folders);
  • Vadim Gremyachev
    Vadim Gremyachev over 8 years
    @LInsoDeTeh, there is no need to check for a object type at all, you just need to construct the proper query. Besides ListItem.Folder property is only available in 2013 CSOM API