SharePoint.Client Get all folders recursively

2019-04-12 21:45发布

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条回答
不美不萌又怎样
2楼-- · 2019-04-12 22:30

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);
  }
查看更多
登录 后发表回答