How to query list with >5000 items in SharePoint Online?
-
08-10-2020 - |
Question
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.
Solution
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();
}
OTHER TIPS
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(); }
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