2

我想为站点调用一次 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);
    }
}
4

1 回答 1

3

由于 SharePoint CSOM 支持请求批处理,您可以考虑使用以下方法来检索 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);
  }
于 2014-12-14T22:07:36.000 回答