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

道山

我正在尝试从给定的子文件夹返回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,我不知道为什么得到这个。

道山

终于找到了可行的解决方案,即使它有点大锤!

尽管我要检索的文件夹远远少于5000个,但问题是整个列表确实超出了此阈值(在这种情况下,大约为11,000个)。

我删除了FolderServerRelativeURL属性,然后使用ListItemCollectionPosition对列表中的所有项目进行分页/批处理。一旦所有项目都在集合中,就可以使用Linq过滤相关子文件夹。CAML查询-遍历5000个列表项阈值

如果有人可以针对这些物品增加针对性,我很乐意看到它。

码:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

C#Sharepoint将大文件上传到子文件夹

列出C中给定路径中的文件夹,子文件夹和文件(Windows)

SharePoint Microsoft.SharePoint.Client.CamlQuery仅以递归方式返回文件夹(包括子文件夹)

在 C# 中复制除某些文件夹和文件之外的所有文件夹

C# sharepoint 循环遍历文件夹和所有子文件夹中的所有文件

使用Microsoft Graph API获取驱动器的所有SharePoint文件和文件夹

C#返回位于其中具有文件夹的目录中的所有文件

在具有特定子文件夹的列表框中列出文件夹 - C#

无法使用SharePoint Client API列出文件/文件夹

使用 C# 在线列出 Sharepoint 上文件夹中的所有文件

按大小C#对所有文件和文件夹进行排序

如何删除所有文件/文件夹,但将根文件夹保留在C#中

SharePoint 2013 REST API。过滤系统文件夹并扩展文件夹和文件。

我可以创建SAS URL来访问C#中Azure文件共享(即不是Blob)中目录的所有文件和文件夹吗?

C#搜索文件和文件夹(某些文件夹除外)

遍历文件夹中的文件(C ++)

如何在子文件夹 SharePoint Online C# 中创建子文件夹

Qt C++:包括项目文件夹中的所有库文件

如何使用C读取文件夹中的所有文件

如何包含文件夹中的所有源文件?(C ++,MS VS 2013)

如何从C#中的文件夹中选择所有xml文件并解析它们

在 C++ 中读取所有文件由 . 文件夹

编译C时如何链接文件夹中的所有目标文件?

C ++控制台中的星号列出了文件夹中的所有文件,为什么?

获取tar -C指定文件夹中的所有文件

REST APi使所有时间都在SharePoint文档库的特定文件夹中

C#在Listview中显示文件和文件夹图标

验证在C ++中创建多个文件夹和文件的正确方法

尝试删除文件夹中的所有文件,例如C#中的2个选定文件