5

I want to query a list in SharePoint Online which has >5000 items. How can I do it?

Right now it is showing me thresh hold limit error when I am trying to query the list.

5

Create a view in your list. Set a filter on the view to filter the data so the total number of items displayed in the view is less than the threshold, based on e.g. a date range, a number, by item id etc.

Then after you can use the ListItemCollectionPosition class to implement paging list item retrieval according to the position of items relative to their collection. Use the RowLimit element to specify the number of items to return per page.It is very useful when list view threshhold limit is reached.

Below is the sample console code to explain the same.

static void Main(string[] args)
 {
 ClientContext clientContext = new ClientContext(“http://testLink”);

List list = clientContext.Web.Lists.GetByTitle(“Tasks”);

ListItemCollectionPosition itemPosition = null;
 while (true)
 {
 CamlQuery camlQuery = new CamlQuery();
 camlQuery.ListItemCollectionPosition = itemPosition;
 camlQuery.ViewXml = @”<View>
 <ViewFields>
 <FieldRef Name=’Title’/>
 </ViewFields>
 <RowLimit>1</RowLimit>
 </View>”;

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

itemPosition = listItems.ListItemCollectionPosition;

foreach (ListItem listItem in listItems)
 Console.WriteLine(“Item Title: {0}”, listItem[“Title”]);

if (itemPosition == null)
 break;

Console.WriteLine(itemPosition.PagingInfo);
 Console.WriteLine();
 }

Console.ReadLine();
 }​
  • is this working for you? – Hardik Apr 22 '16 at 6:26
  • This won't work if trying to query for a lookup field, right? Even though the field is indexed. (e.g., <Query><Where><Eq><FieldRef Name="Parent" LookupId="TRUE" /><Value Type="Integer">1</Value></Eq></Where></Query>, also with Type="Lookup"). – eirikb Jul 7 '16 at 8:37
3

Using Client Object Model, You can query large list by using ListItemCollection Property

ClientContext clientContext = new ClientContext("YOURURL");

List list = clientContext.Web.Lists.GetByTitle(“Tasks”);

ListItemCollectionPosition itemPosition = null;
while (true)
{
    CamlQuery camlQuery = new CamlQuery();
    camlQuery.ListItemCollectionPosition = itemPosition;
    camlQuery.ViewXml = @"<View>
   <ViewFields>
   <FieldRef Name='Title'/>
   </ViewFields>
   <RowLimit>1</RowLimit>
 </View>";

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

itemPosition = listItems.ListItemCollectionPosition;

foreach (ListItem listItem in listItems) {

    Console.WriteLine(“Item Title: {0}”, listItem[“Title”]); 
}
  if (itemPosition == null)
    break;

  Console.WriteLine(itemPosition.PagingInfo);
  Console.WriteLine();
}

Console.ReadLine();
}​

Reference link here

1

This might be duplicate of List with > 5000 items working with Paging? Please have a look and let me know whether it is helpful or not

Thanks

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.