0

I am currently using the below code to query over 5000 items in a SharePoint Online Library.

        CamlQuery camlQuery = new CamlQuery();
        camlQuery.ViewXml = "<View Scope='RecursiveAll'><RowLimit>5000</RowLimit><ViewFields><FieldRef Name='ID' /><FieldRef Name='Title' /></ViewFields></View>";

        List<ListItem> items = new List<ListItem>();
        do
        {
            ListItemCollection listItemCollection = list.GetItems(camlQuery);
            ctx.Load(listItemCollection);
            ctx.ExecuteQuery();

            //Adding the current set of ListItems in our single buffer
            items.AddRange(listItemCollection);
            //Reset the current pagination info
            camlQuery.ListItemCollectionPosition = listItemCollection.ListItemCollectionPosition;

        } while (camlQuery.ListItemCollectionPosition != null);

       items.SingleOrDefault(c => c.FieldValues["Title"].ToString() == "Test");

Effectively, I load batches of 5000 items by CAML Query (since 5000 is the limit) into a ListItemCollection and then query the ListItemCollection with LINQ. So if we have say 20,000 items in the library, we'd have 4 iterations appending 5000 items to the ListItemCollection. The LINQ aspect makes querying easy, however, with over 6000 items in the library it already takes a relatively long time - I wonder what will happen with 20000 items!

Is there any other way to go around the 5000 item limit for CAML queries that does not require loading all the items in the list everytime? Perhaps by using indexed columns or something of the sort?

Note: the columns I query on are all indexed columns.

  • is the title column indexed via list settings > indexed columns ? – Gautam Sheth Sep 29 '17 at 12:10
  • yes. And I can make it a point to have all other columns that are required for querying indexed also :) – Jurgen Cuschieri Sep 29 '17 at 12:31
  • Have you considered using a search query instead? Use the path managed property and wildcard if you want to return everything. – Bunzab Sep 29 '17 at 13:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.