Home > front end >  Receiving only 100 Users from Azure AD - Graph API
Receiving only 100 Users from Azure AD - Graph API

Time:09-09

We are receiving only 100 users from below code (Asp.Net Core Api). We want to receive all users from Azure AD.

       var token = await _tokenAcquisition.GetAccessTokenForUserAsync("User.Read.All");
        var client = _clientFactory.CreateClient();
        client.BaseAddress = new Uri("https://graph.microsoft.com/v1.0");
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        var graphClient = new GraphServiceClient(client)
        {
            AuthenticationProvider = new DelegateAuthenticationProvider((requestMessage) =>
            {
                requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", token);
                return Task.CompletedTask;
            }),
            BaseUrl = "https://graph.microsoft.com/v1.0"
        };
        return graphClient.Users.Request().GetAsync();

CodePudding user response:

MS Graph uses paged output with default page size of 100 records. You've read just first page of the default 100 users, you can use top (999)

please check the sample code

var users = await graphServiceClient
  .Users
  .Request()
  .Top(999)  // <- Custom page of 999 records (if you want to set it)
  .GetAsync()
  .ConfigureAwait(false);

while (true) {
  //TODO: relevant code here (process users)

  // If the page is the last one
  if (users.NextPageRequest is null)
    break;

  // Read the next page
  users = await users
    .NextPageRequest
    .GetAsync()
    .ConfigureAwait(false);
} 
  • Related