【发布时间】:2018-10-18 09:18:39
【问题描述】:
我正在尝试从给定的子文件夹开始返回 SharePoint 库中的所有文件和文件夹。
如果我将 CamlQuery 上的 FolderServerRelativeUrl 设置为我希望开始的文件夹,我可以获得该给定文件夹的所有列表项;但是,当我尝试添加 camlQuery.ViewXML 以递归返回带有任何其他子文件夹的项目时,我得到以下异常:
Microsoft.SharePoint.Client.ServerException: '尝试的操作被禁止,因为它超过了管理员强制执行的列表视图阈值。'
代码
public static IEnumerable<string> GetSharepointFiles2(string sharePointsite, string libraryName, string username, string password, string subFolders)
{
Uri filename = new Uri(sharePointsite);
string server = filename.AbsoluteUri.Replace(filename.AbsolutePath, "");
List<string> fullfilePaths = new List<string>();
using (ClientContext cxt = new ClientContext(filename))
{
cxt.Credentials = GetCreds(username, password);
Web web = cxt.Web;
cxt.Load(web, wb => wb.ServerRelativeUrl);
cxt.ExecuteQuery();
List list = web.Lists.GetByTitle(libraryName);
cxt.Load(list);
cxt.ExecuteQuery();
Folder folder = web.GetFolderByServerRelativeUrl(web.ServerRelativeUrl + subFolders);
cxt.Load(folder);
cxt.ExecuteQuery();
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = @"<View Scope='RecursiveAll'>
<Query>
</Query>
</View>";
camlQuery.FolderServerRelativeUrl = folder.ServerRelativeUrl;
ListItemCollection listItems = list.GetItems(camlQuery);
cxt.Load(listItems);
cxt.ExecuteQuery();
foreach (ListItem listItem in listItems)
{
if (listItem.FileSystemObjectType == FileSystemObjectType.File)
{
fullfilePaths.Add(String.Format("{0}{1}", server, listItem["FileRef"]));
}
else if (listItem.FileSystemObjectType == FileSystemObjectType.Folder)
{
Console.WriteLine(String.Format("{0}{1}", server, listItem["FileRef"]));
fullfilePaths.Add(String.Format("{0}{1}", server, listItem["FileRef"]));
}
}
}
return fullfilePaths;
}
private static SharePointOnlineCredentials GetCreds(string username, string password)
{
SecureString securePassword = new SecureString();
foreach (char c in password.ToCharArray()) securePassword.AppendChar(c);
return new SharePointOnlineCredentials(username, securePassword);
}
在阈值限制方面,我在一个只有 1 个文件和 1 个文件夹的文件夹上尝试过这个(反过来,该文件夹只有 1 个文件),所以如果限制是默认 5000,我不知道为什么我会得到这个。
【问题讨论】:
标签: c# .net sharepoint caml sharepoint-clientobject