Azure GraphServiceClient limits results to 100, how to get all (C# .NET Core)?

88 Views Asked by At

I am working on an Azure Function App. It needs to retrieve all groups in Exchange. I am using this simple C#, .NET Core code:

GraphServiceClient graphClient;
var groups = graphClient.Groups.GetAsync().Result;

But, I only get 100 groups. How do I increase this limit to unlimited?

A lot of searching, but no luck.

2

There are 2 best solutions below

1
NotFound On

You can make use of the PageIterator:

var client = new GraphServiceClient(credentials);

var page = await client
    .Groups
    .GetAsync();

if (page is null)
{
    return;
}

//the resulting list with all groups
List<Group> groups = []; //or use new() for C# lang versions <12.0

var pageIterator = PageIterator<Group, GroupCollectionResponse>
    .CreatePageIterator(
        client,
        page,
        group =>
        {
            //perform an action on each result
            groups.Add(group);
            //return true to keep iterating
            return true;
        });

await pageIterator.IterateAsync();
0
Pravallika KV On

You can list all the available groups using $top query parameter.

  • With $top parameter, you can customize the result size.
  • If the response contains more results, Microsoft Graph will return an @odata.nextLink property to list the remaining groups.

Below is my code Snippet:

 [FunctionName("Function1")]
 public static async Task<IActionResult> Run(
     [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
     ILogger log)
 {
     log.LogInformation("C# HTTP trigger function processed a request.");
    string ClientId = "<Your_Client_ID>";
    string ClientSecret = "<Your_Client_Secret>";
    string scopes = "https://graph.microsoft.com/.default";
    IConfidentialClientApplication pravapp = ConfidentialClientApplicationBuilder
        .Create(ClientId)
        .WithClientSecret(ClientSecret)
        .WithAuthority(new Uri("https://login.microsoftonline.com/<your_Tenant_ID>/oauth2/v2.0/token"))
        .Build();
     var authResult1 = await pravapp.AcquireTokenForClient(new string[] { scopes }).ExecuteAsync();
     string myaccessToken = authResult1.AccessToken;

     String authorizationToken = myaccessToken;
     string token = authorizationToken.ToString().Replace("Bearer ", "");
     TokenProvider provider2 = new TokenProvider();
     provider2.token = token;
     var authenticationProvider1 = new BaseBearerTokenAuthenticationProvider(provider2);
     var graphServiceClient1 = new GraphServiceClient(authenticationProvider1);
     var group = await graphServiceClient1.Groups.GetAsync((requestConfiguration) =>
     {
         requestConfiguration.QueryParameters.Top = 5;
     });
     return new OkObjectResult(group);
 }
  • This will provide an @odatanextlink as shown below:

enter image description here

References:

  1. https://stackoverflow.com/a/73203861/19991670
  2. Paging Microsoft Graph data in your app