简体   繁体   English

批量获取SharePoint列表c#CSOM

[英]Get SharePoint List in batches c# CSOM

Can you help me getting the items of a SharePoint library (it is a library with document sets) using CSOM. 您能帮助我使用CSOM获取SharePoint库(它是带有文档集的库)的项目吗? The library has a lot more than 5000 items and I know SharePoint has a limit of 5000, so I am trying to get them in batches of 500. Is it possible? 该库有5000多个项目,我知道SharePoint的限制是5000个,因此我正尝试以500个为批次进行采购。是否可以?

clientContext.Credentials = new SharePointOnlineCredentials(username, securePassword);
List oList = clientContext.Web.Lists.GetByTitle(libraryName);
clientContext.Load(oList);
clientContext.ExecuteQuery();
CamlQuery query = new CamlQuery();
query.ViewXml = "<View><RowLimit>500</RowLimit></View>";
ListItemCollection allDocumentSet = oList.GetItems(query);
clientContext.Load(allDocumentSet);
clientContext.ExecuteQuery();

I do not know why is not working. 我不知道为什么不起作用。 I get the following error. 我收到以下错误。

Microsoft.SharePoint.Client.ServerException: 'The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator.' Microsoft.SharePoint.Client.ServerException:'禁止尝试执行操作,因为它超出了管理员强制执行的列表视图阈值。

Why do I get the error if I am getting only 500 items per batch? 如果每批仅获取500个项目,为什么会出现错误?

Also, one additional question. 另外,还有一个问题。 When should I use "ExecuteQuery" and Load function? 什么时候应该使用“ ExecuteQuery”和Load函数?

Thanks! 谢谢!

You need run ExecuteQuery to retrieve/update the data from server. 您需要运行ExecuteQuery来从服务器检索/更新数据。

Here is my sample test code to retrieve items more than 5000. 这是我的示例测试代码,可检索超过5000个项目。

 List lmsList = clientContext.Web.Lists.GetByTitle("LargeList");
                ListItemCollectionPosition itemPosition = null;
                while (true)
                {
                    CamlQuery camlQuery = new CamlQuery();
                    camlQuery.ListItemCollectionPosition = itemPosition;
                    camlQuery.ViewXml = @"<View><RowLimit>500</RowLimit></View>";

                    ListItemCollection listItems = lmsList.GetItems(camlQuery);
                    clientContext.Load(listItems);
                    clientContext.ExecuteQuery();

                    itemPosition = listItems.ListItemCollectionPosition;
                    Console.WriteLine(itemPosition);
                    //foreach (ListItem listItem in listItems)
                    //    Console.WriteLine("Item Title: {0}", listItem["Title"]);

                    if (itemPosition == null)
                        break;

                    Console.WriteLine(itemPosition.PagingInfo);                    
                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM