Skip to content

Instantly share code, notes, and snippets.

@OlafD
Created July 31, 2019 06:09
Show Gist options
  • Save OlafD/74bf51d449449ffe7df678d929560e15 to your computer and use it in GitHub Desktop.
Save OlafD/74bf51d449449ffe7df678d929560e15 to your computer and use it in GitHub Desktop.
Simple sample for looping a list with a caml query for a list with more than 5000 items.
List list = _ctx.Web.Lists.GetByTitle(Constants.Listnames.DocumentHistoryList);
_ctx.Load(list);
_ctx.ExecuteQueryRetry();
ListItemCollectionPosition collectionPosition = null;
CamlQuery query = new CamlQuery();
query.AllowIncrementalResults = true;
query.ViewXml =
"<view Scope='RecursiveAll'>" +
"<Query>" +
"<Where>" +
"<Eq>" +
"<FieldRef Name='WorkflowID' />" +
"<Value Type='Text'>4def8543-99a2-4239-88df-e4fe7298c646</Value>" +
"</Eq>" +
"</Where>" +
"</Query>" +
"<RowLimit Paged='TRUE'>500</RowLimit>" +
"</View>";
do
{
query.ListItemCollectionPosition = collectionPosition;
ListItemCollection items = list.GetItems(query);
_ctx.Load(items);
_ctx.ExecuteQueryRetry();
if (items != null)
{
collectionPosition = items.ListItemCollectionPosition;
if (items.ServerObjectIsNull == false)
{
if (items.Count > 0)
{
foreach (ListItem item in items)
{
// do anything with the listitem
}
}
}
}
else
{
collectionPosition = null;
}
}
while (collectionPosition != null);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment