SOLVED

C# combine multiple ListItemCollection into one

Iron Contributor

Hi Everyone,

I hope someone can help me out :)

 

I have a list with more than 5000 items on Sharepoint Online Site. Before the list reached 5000 items, I've used the following snippet to get all items:

 

var camlquery = new CamlQuery();
camlquery.ViewXml = @"<View Scope='RecursiveAll'>
			<ViewFields>
			   <FieldRef Name='OppId' />
			</ViewFields>
			<Query>
			   <QueryOptions>
				<ViewAttributes Scope='Recursive' />
			   </QueryOptions>
			</Query>
		      </View>";
ListItemCollection listItems = _currentList.GetItems(camlquery);
_clientContext.Load(listItems, items => items.Include(item => item["OppId"], item => item.Id));
_clientContext.ExecuteQuery();

 

Now I got in this nasty situation where I have more than 5000 items. Normally this is not an problem. I've managed it before in powershell by paging through the results.

In this case I'm using Microsoft.SharePoint.Client.ListItemCollection and add items to the collection with the function GetItems(camlquery). When I try adding additional items from another 'page' it seems not to work.

I tried to loop through the items and add these to the ListItemCollection. But there is no Add or Insert function available.

 

foreach (ListItem myListItem in listItems2)
{
    listItems.Add(myListItem); 
}

 

I need to return one ListItemCollection with more than 5000 items.

Thanks for helping!

 

I've also posted this question on StackOverflow

2 Replies
best response confirmed by Bernd Verhofstadt (Iron Contributor)
Solution

I've found a way to get all items by using IList<ListItem> to collect all the objects of type ListItem from the ListItemCollection.

 

 

IList<ListItem> listItemsAll = new List<ListItem>();
// Loop through pages of list items
//... Getting all items

foreach (ListItem myListItem in listItems)
{    listItemsAll.Add(myListItem);
}
// End Loop
return listItemsAll;

In this case I return a List but I only needed to change the type of list from ListItemCollection to IList<ListItem>. Everythings works as expected.

@Bernd Verhofstadt - Can you please share the one you did using CSOM PowerShell to read the lists having more than 5000 items in it.

1 best response

Accepted Solutions
best response confirmed by Bernd Verhofstadt (Iron Contributor)
Solution

I've found a way to get all items by using IList<ListItem> to collect all the objects of type ListItem from the ListItemCollection.

 

 

IList<ListItem> listItemsAll = new List<ListItem>();
// Loop through pages of list items
//... Getting all items

foreach (ListItem myListItem in listItems)
{    listItemsAll.Add(myListItem);
}
// End Loop
return listItemsAll;

In this case I return a List but I only needed to change the type of list from ListItemCollection to IList<ListItem>. Everythings works as expected.

View solution in original post