获取共享点列表的所有项目

Get all items of a sharepoint list

我想下载我的共享点列表中的所有文件。 我用这个方法下载文件:

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


               }


           }

       }

现在我的问题是,子文件夹中的文件不是用这种方法下载的。 是否有机会浏览我列表中的所有子文件夹并下载所有文件?

当您拥有 listItem 对象时,它可能已经是一个文件夹,因此您可以使用 .Folder 属性:

将其视为一个文件夹
var listItemAsFolder = listItem.Folder;
if (listItemAsFolder != null) {
  // here you can access listItemAsFolder.Files to get all files in the folder
  // also, you can access listItemAsFolder.SubFolders to get all subfolders.
}

顺便说一句,还有一个回头路:当您有一个 SPFolder 对象时,使用 .Item 将其视为一个 ListItem。

因为你的目标是:

to download all files of my SharePoint list

以下示例演示了如何完成它:

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

哪里

    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