SharePoint.Client递归获取所有文件夹 [英] SharePoint.Client Get all folders recursively
问题描述
我想一次调用clientContext.ExecuteQuery()来提高网站的性能.
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);
}
}
推荐答案
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);
}
}
用法
using (var ctx = new ClientContext(webUrl))
{
Dictionary<string, IEnumerable<Folder>> listsFolders;
Dictionary<string, IEnumerable<File>> listsFiles;
LoadContent(ctx.Web,out listsFolders,out listsFiles);
}
这篇关于SharePoint.Client递归获取所有文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!