【发布时间】:2021-09-01 19:25:38
【问题描述】:
我正在研究一种方法,该方法需要获取指定文件夹名称下的所有文件,并且可以选择包含所有子文件夹(递归)。我目前有以下代码适用于父文件夹文件:
using (var spClientContext = new AuthenticationManager().GetACSAppOnlyContext(siteUrl, _clientId, _clientSecret))
{
if (spClientContext != null)
{
List list = spClientContext.Web.Lists.GetByTitle("Documents");
spClientContext.Load(list);
spClientContext.Load(list.RootFolder);
spClientContext.Load(list.RootFolder.Folders);
spClientContext.ExecuteQuery();
FolderCollection fcol = list.RootFolder.Folders;
if (fcol != null)
{
foreach (Folder f in fcol)
{
if (f.Name.Equals(folderName, StringComparison.InvariantCultureIgnoreCase))
{
spClientContext.Load(f.Files,
items => items.Include(
item => item.Name,
item => item.Author,
item => item.ModifiedBy,
item => item.ListItemAllFields["Created"],
item => item.ListItemAllFields["Modified"],
item => item.ListItemAllFields["FileRef"],
item => item.Length)
);
spClientContext.ExecuteQuery();
FileCollection fileCol = f.Files;
foreach (File file in fileCol)
{
// do stuff
}
}
}
}
}
};
在所有子文件夹中包含文件的最佳方法是什么?也很好奇,在这里使用 CAML 查询会更好吗?我最终也需要优化排序,不确定是否只能通过 CAML 查询来实现?提前致谢!
【问题讨论】:
标签: c# asp.net sharepoint csom caml