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):
Usage