Search code examples
c#microsoft-graph-apisharepoint-online

Ms Graph v5.0 How to get all SharePoint list items


I wrote piece of code and it works, but this way I can only get 200 elements of the list, the question is how to get everything?

this is what I wrote

var  response = await graphClient
         .Sites[siteId]
         .Lists[listId]
         .Items.GetAsync((requestConfiguration) =>
                    {
                        requestConfiguration.QueryParameters.Expand = new string[]
                        {
                            "fields($select=_x0421__x0434__x0440_,_x0421__x0435__x043a__x0446__x04," +
                            "_x041f__x043e__x0432__x0435__x04,_x041a__x043e__x0440__x043f__x04,_x0417__x0430__x0445__x0432__x04,_x0420__x043e__x0437__x0446__x04," +
                            "Title,_x041e__x0434__x002e__x0432__x04,_x041a__x0456__x043b__x044c__x04," +
                            "_x041a__x0456__x043b__x044c__x040,_x041f__x0456__x0434__x0440__x04," +
                            "_x041f__x0440__x0438__x043c__x04)"
                        };
                    });

Solution

  • You need to use PageIterator to get all items. PageIterator handles the odata.nextLink until the last page is reached.

    var  response = await graphClient
             .Sites[siteId]
             .Lists[listId]
             .Items.GetAsync((requestConfiguration) =>
                        {
                            requestConfiguration.QueryParameters.Expand = new string[]
                            {
                                "fields($select=_x0421__x0434__x0440_,_x0421__x0435__x043a__x0446__x04," +
                                "_x041f__x043e__x0432__x0435__x04,_x041a__x043e__x0440__x043f__x04,_x0417__x0430__x0445__x0432__x04,_x0420__x043e__x0437__x0446__x04," +
                                "Title,_x041e__x0434__x002e__x0432__x04,_x041a__x0456__x043b__x044c__x04," +
                                "_x041a__x0456__x043b__x044c__x040,_x041f__x0456__x0434__x0440__x04," +
                                "_x041f__x0440__x0438__x043c__x04)"
                            };
                        });
    
    // list which contains all items
    var listItems = new List<ListItem>();
    var pageIterator = PageIterator<ListItem, ListItemCollectionResponse>.CreatePageIterator(graphServiceClient, response,item=>
    {
        listItems.Add(item);
        return true;
    });
    
    await pageIterator.IterateAsync();