SharePoint.Client Get all folders recursively

心已入冬 提交于 2019-12-12 09:01:38

问题


I would like to call clientContext.ExecuteQuery() once for a site to improve performance.

The collections of things I want to load includes all the folders and files for all document libraries within the site. When I say ALL I really do mean ALL. i.e. If there are folders within folders, within folders, I want them all in one go. Is this possible or do I have to stick with recursively loading each child folder and loading it's folders and files explicitly.

What I have right now which gets the base level folders and recursively gets the rest:

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:


Since SharePoint CSOM supports Request Batching you could consider the following approach to retrieve web content (Files and Folders):

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

Usage

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


来源:https://stackoverflow.com/questions/16652288/sharepoint-client-get-all-folders-recursively

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!