Sharepoint Online Csom:通过分页响应从文档库中获取根文件夹

问题描述 投票:0回答:1

我已经创建了一个C#控制台应用程序,该应用程序加载到文档库中,在此文档库的根目录中,有我们的客户文件夹。我通过两个函数传递一个客户文件夹,该函数简单地检查这些文件夹的内容,如果它们为空,则将其删除。这部分很简单。问题是我们的库超出了文档库的5000个视图限制。多年来,我一直不希望将其分解。我可以使用以下指南从库中将第100行中的所有项目下拉到listItemCollection中:https://morgantechspace.com/2017/08/get-all-files-from-sharepoint-document-library-csom.html

问题是,我需要从根文件夹级别开始,这会将库中的所有文件夹和文件放在一个列表中。如果我将其交给顶层文件夹,我的程序可以轻松浏览“客户端”文件夹。但是我认为处理单个列表中的所有文件会非常痛苦。

我目前正在使用以下代码块将所有文件夹都称为文档库的根目录(我们称为客户端文件夹。)>

 static void Main(string[] args)
        {
            string userName = "MyUserName";
            Console.WriteLine("Enter Your Password Please  ------- ");
            SecureString password = GetPasswordOfYourSite();
            string webUri = "https://conferencetechinc.sharepoint.com/sites/yoda";
            List<ListItem> items = new List<ListItem>();


            using (ClientContext clientContext = new ClientContext(webUri))
            {

                int rowLimit = 100;
                var camlQuery = new CamlQuery();
                camlQuery.ViewXml = @"<View Scope='Recursive'>
                    <Query>
                    </Query>
                    <RowLimit Paged='TRUE'>" + rowLimit + "</RowLimit></View>";

                clientContext.Credentials = new SharePointOnlineCredentials(userName, password);

                // This value is NOT List internal name
                List targetList = clientContext.Web.Lists.GetByTitle("TestingLibrary");

                // This method only gets the folders which are on top level of the list/library
                FolderCollection oFolderCollection = targetList.RootFolder.Folders;

                // Load folder collection

                clientContext.ExecuteQuery();

                //Write Folder name and folder URl on console
                foreach (Folder client in oFolderCollection)
                {
                    if (client.Name != "Forms")
                    {
                        Console.WriteLine("Folder Name: " + client.Name);
                          // YAY! Here we have the client level.  We must now break them down from here.
                        // Pass them to a method that injests the next level of folders - Opportunity, once we are at project or order level,
                        // we then pass the folder to a generic recursive fileChecker which returns a bool for deletion in the ClientFileDiver
                        clientFileDiver(client, clientContext);
                    }

                }
            }

我知道我需要应用Caml查询才能获得在分页响应中返回库的根目录中的文件夹的预期结果,但是我不确定如何应用它,或者不确定是否可以将其应用于targetList.RootFolder 。我刚刚从本周开始学习Sharepoint CSOM,并享受其中的乐趣,因此非常感谢您的帮助。希望我在这里输入足够的信息。

****续****

 using (ClientContext clientContext = new ClientContext(webUri))
            {

                int rowLimit = 1;
                var camlQuery = new CamlQuery();
                camlQuery.ViewXml = @"<RowLimit Paged='TRUE'>" + rowLimit + "</RowLimit></View>";

                clientContext.Credentials = new SharePointOnlineCredentials(userName, password);
                ListItemCollectionPosition position = null;

                // This value is NOT List internal name
                List targetList = clientContext.Web.Lists.GetByTitle("Clients");
                   // FolderCollection oFolderCollection = targetList.RootFolder.Folders;
                   // clientContext.Load(oFolderCollection);
                    //clientContext.ExecuteQuery();
                do
                {
                    ListItemCollection listItems = null;
                    camlQuery.ListItemCollectionPosition = position;
                    listItems = targetList.GetItems(camlQuery);
                    clientContext.Load(listItems);
                    clientContext.ExecuteQuery();
                    position = listItems.ListItemCollectionPosition;
                    items.AddRange(listItems.ToList());

                    foreach (ListItem item in listItems)
                    {
                        clientContext.Load(item);
                        clientContext.Load(item.Folder);
                        clientContext.ExecuteQuery();
                        if(item.Folder.GetType() == typeof(Folder))
                        {
                            Console.WriteLine(item.Folder.GetType());
                            Console.WriteLine(item.Folder.Name);
                            Console.ReadKey();
                        }

                    }

                }
                while (position != null);

这似乎更接近我所需要的,但是我仍然遇到以下问题:

    Microsoft.SharePoint.Client.ServerException
  HResult=0x80131500
  Message=The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator.
  Source=Microsoft.SharePoint.Client.Runtime
  StackTrace:
   at Microsoft.SharePoint.Client.ClientRequest.ProcessResponseStream(Stream responseStream)
   at Microsoft.SharePoint.Client.ClientRequest.ProcessResponse()
   at Microsoft.SharePoint.Client.ClientRequest.ExecuteQueryToServer(ChunkStringBuilder sb)
   at Microsoft.SharePoint.Client.ClientRequest.ExecuteQuery()
   at Microsoft.SharePoint.Client.ClientRuntimeContext.ExecuteQuery()
   at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()
   at ConsoleApp1.Program.Main(String[] args) in C:\Users\andre\source\repos\ConsoleApp1\ConsoleApp1\Program.cs:line 48

  This exception was originally thrown at this call stack:
    [External Code]
    ConsoleApp1.Program.Main(string[]) in Program.cs

尽管我仅在请求中请求1行。除非我提出错误的要求,否则我是否假设它仅查看文档库的整体大小,并且即使我们仅部分要求提供信息,也只是说“没有太大”?我也可能错误地应用了Caml查询,但是从我今天学到的知识来看,这应该是仅对数据应用行限制的方法。也许还有一些我想念的东西吗?

我已经创建了一个C#控制台应用程序,该应用程序加载到文档库中,在此文档库的根目录中,有我们的客户文件夹。我通过两个简单地通过...

sharepoint csom caml
1个回答
0
投票

问题主要是由于Caml,将其更改为以下内容:

© www.soinside.com 2019 - 2024. All rights reserved.