Forum Discussion

Bernd Verhofstadt's avatar
Bernd Verhofstadt
Iron Contributor
Jan 17, 2017
Solved

C# combine multiple ListItemCollection into one

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 https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.listitemcollection.aspx 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 http://stackoverflow.com/q/41692628/2660428?stw=2

  • 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.

2 Replies

  • 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.

Resources