SharePoint.Client递归获取所有文件夹

时间:2013-05-20 14:55:30

标签: sharepoint-2010 sharepoint-clientobject

我想为站点调用clientContext.ExecuteQuery()一次以提高性能。

我要加载的内容集包括网站中所有文档库的所有文件夹和文件。当我说所有我真的意味着所有。即如果文件夹中有文件夹,在文件夹中,我希望它们一次性完成。这是可能的,还是我必须坚持递归加载每个子文件夹并明确加载它们的文件夹和文件。

我现在拥有的是获取基础文件夹并以递归方式获得其余文件夹的内容:

private static void SharePoint()
{
    var clientContext = new ClientContext(@"http://myURL")
                            {
                                Credentials = System.Net.CredentialCache.DefaultCredentials
                            };


    var web = clientContext.Web;
    clientContext.Load(web);
    clientContext.Load(web.Folders);
    clientContext.Load(web.Lists, lists => lists.Include(l => l.ContentTypes.Include(c => c.Fields),
                                                              l => l.BaseType,
                                                              l => l.Hidden,
                                                              l => l.RootFolder,
                                                              l => l.RootFolder.Files.Include(fi => fi.ListItemAllFields, 
                                                                                              fi => fi.ListItemAllFields.ContentType,
                                                                                              fi => fi.Name),
                                                              l => l.RootFolder.Folders,
                                                              l => l.Title));
    clientContext.ExecuteQuery();

    var documentLibraries = web.Lists.ToList().Where(l => l.BaseType == BaseType.DocumentLibrary && !l.Hidden).ToList();
    foreach (var folder in documentLibraries.SelectMany(documentLibrary => documentLibrary.RootFolder.Folders.ToList().Where(fo => fo.Name != "Forms")))
    {
        LoadFolders(clientContext, folder);
    }
}

private static void LoadFolders(ClientContext clientContext, Folder folder)
{
    clientContext.Load(folder.Files, files => files.Include(fi => fi.ListItemAllFields,
                                                                fi => fi.ListItemAllFields.ContentType,
                                                                fi => fi.Name)); 
    clientContext.Load(folder.Folders);
    clientContext.ExecuteQuery();
    foreach (var childFolder in folder.Folders)
    {
        LoadFolders(clientContext, childFolder);
    }
}

1 个答案:

答案 0 :(得分:3)

由于SharePoint CSOM支持Request Batching,您可以考虑以下方法来检索Web内容(文件和文件夹):

    public static void LoadContent(Web web, out Dictionary<string, IEnumerable<Folder>> listsFolders, out Dictionary<string, IEnumerable<File>> listsFiles)
    {
        listsFolders = new Dictionary<string, IEnumerable<Folder>>();
        listsFiles = new Dictionary<string, IEnumerable<File>>();
        var listsItems = new Dictionary<string, IEnumerable<ListItem>>();

        var ctx = web.Context;
        var lists = ctx.LoadQuery(web.Lists.Where(l => l.BaseType == BaseType.DocumentLibrary));
        ctx.ExecuteQuery();

        foreach (var list in lists)
        {
            var items = list.GetItems(CamlQuery.CreateAllItemsQuery());
            ctx.Load(items, icol => icol.Include(i => i.FileSystemObjectType, i => i.File, i => i.Folder));
            listsItems[list.Title] = items;
        }
        ctx.ExecuteQuery();

        foreach (var listItems in listsItems)
        {
            listsFiles[listItems.Key] = listItems.Value.Where(i => i.FileSystemObjectType == FileSystemObjectType.File).Select(i => i.File);
            listsFolders[listItems.Key] = listItems.Value.Where(i => i.FileSystemObjectType == FileSystemObjectType.Folder).Select(i => i.Folder); 
        }
    }

用法

  using (var ctx = new ClientContext(webUrl))
  { 
      Dictionary<string, IEnumerable<Folder>> listsFolders;
      Dictionary<string, IEnumerable<File>> listsFiles;
      LoadContent(ctx.Web,out listsFolders,out listsFiles);
  }