In this post I will show how to retrieve paginated data from Azure AD using MS Graph .Net client library. As we already saw from previous examples (see e.g. Create Azure AD group and set group owner using Microsoft Graph Client library) client library is adopted for asynchronous usage (we can call its methods asynchronously using async/await C# keywords). In addition to that it also restricts number of objects returned from single request, e.g. when you get list of users via GraphServiceClient.Users.Request().GetAsync() call it will return only 100 users. In order to get other users we will need to load all pages by 100 users each. The following example shows how to do it:
I.e. we call graph.Users.Request().GetAsync() first (line 6) and then call graph.Users.NextPageRequest.GetAsync() (line 19) until next page won’t be empty. For convenience I also added number of request made to Graph API to the console (line 10) so it will be visible how many requests are needed for iterating through all users. For authentication we use application id and secret in custom AzureAuthenticationProvider as described in the following article: Work with Azure AD via Microsoft Graph API.