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