首页 文章

C#Sharepoint.Client - 返回给定子文件夹中的所有文件和文件夹

提问于
浏览
1

我试图从给定的子文件夹开始返回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,我不知道为什么我会得到这个 .

1 回答

  • 0

    终于找到了一个有效的解决方案,即使它有点大锤!

    虽然我想要检索项目的文件夹远远少于5000个项目,但问题是整个列表确实超过了这个阈值(在这种情况下,它大约是11,000个项目) .

    我删除了FolderServerRelativeURL属性,然后使用ListItemCollectionPosition对列表中的所有项进行分页/批处理 . 一旦所有项目都在一个集合中,就可以使用Linq过滤相关的子文件夹 . (CAML Query - Going around the 5000 List Item Threshold

    如果有人有办法更有针对性地使用这些物品,我很乐意看到它 .

    码:

    public static IEnumerable<ListItem> GetSharepointFiles2(string sharePointsite, string libraryName, string username, string password, string subFolders)
    {
        Uri filename = new Uri(sharePointsite);
        List<ListItem> items = new List<ListItem>();
    
        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();
    
            CamlQuery camlQuery = new CamlQuery();
            camlQuery.ViewXml = "<View Scope='Recursive'><RowLimit>5000</RowLimit></View>";
    
            do
            {
                ListItemCollection listItems = list.GetItems(camlQuery);
                cxt.Load(listItems);
                cxt.ExecuteQuery();
    
                items.AddRange(listItems);
                camlQuery.ListItemCollectionPosition = listItems.ListItemCollectionPosition;
    
            } while (camlQuery.ListItemCollectionPosition != null);
    
            var filteritems = items.Where(tt => tt.FieldValues["FileRef"].ToString().StartsWith(web.ServerRelativeUrl + subFolders));
    
            return filteritems;
        }
    }
    

相关问题