gpt4 book ai didi

sharepoint-2010 - SharePoint.Client 递归获取所有文件夹

转载 作者:行者123 更新时间:2023-12-04 04:56:54 26 4
gpt4 key购买 nike

我想为站点调用一次 clientContext.ExecuteQuery() 以提高性能。

我要加载的内容集合包括站点内所有文档库的所有文件夹和文件。当我说 ALL 时,我确实是指 ALL。即如果文件夹内有文件夹,文件夹内有文件夹,我希望它们一口气全部完成。这是可能的还是我必须坚持递归加载每个子文件夹并显式加载它的文件夹和文件。

我现在拥有的是获取基本级别文件夹并递归获取其余部分的内容:

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

最佳答案

由于 SharePoint CSOM 支持 Request Batching您可以考虑使用以下方法来检索 Web 内容(文件和文件夹):

    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-2010 - SharePoint.Client 递归获取所有文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16652288/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com