Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paging when using Microsoft.Graph.GraphServiceClient

I'm using GraphServiceClient to access the Graph API. I'm not sure how to handle paging though. Let's say I run the following query which will return more than 100 records (and hence page):

var users = await client.Users
    .GetAsync(rc => { 
        rc.QueryParameters.Select = new[] 
        {
            "userPrincipalName", "displayName", "id" 
        };
    });

In the results response I can see the OdataNextLink property, but I'm not sure how to use it. Can anyone give an example? All the documentation I've found seems to be based on using Graph Explorer or direct HTTP requests.

like image 471
Kevin O'Donovan Avatar asked Oct 19 '25 23:10

Kevin O'Donovan


1 Answers

You need to use PageIterator. PageIterator has property State which indicates whether the iteration has started or whether is completed.

In CreatePageIterator you can specify callback action. Add the current user to a list of users.

Until pageIterator.State is not Complete you need to call pageIterator.IterateAsync() to return data from next page.

Each invoke of pageIterator.IterateAsync() will return users for the current page.

var response = await client.Users
            .GetAsync(rc => {
                rc.QueryParameters.Select = new string[] { "userPrincipalName", "displayName", "id" };
            });

var userlist = new List<User>();
var pageIterator = PageIterator<User, UserCollectionResponse>.CreatePageIterator(client, response, (user) =>
{
    userlist.Add(user);
    return true;
});

await pageIterator.IterateAsync();

The documentation can be found here (only simple example)

like image 130
user2250152 Avatar answered Oct 22 '25 14:10

user2250152