Home > Net >  Not all users are retrieved with Microsoft.Graph
Not all users are retrieved with Microsoft.Graph

Time:11-03

I need to retrieved from Azure Active Directory all members (users) of a group, including those within nested groups. Here is my code:

        ClientCredentialProvider authenticationProvider = new ClientCredentialProvider(confidentialClientApplication);

        GraphServiceClient graphServiceClient = new GraphServiceClient(authenticationProvider);

        IGroupTransitiveMembersCollectionWithReferencesPage members = await graphServiceClient.Groups["{myGroupId"]  
            .TransitiveMembers
            .Request()
            .GetAsync();

        List<string> repEmails = members.OfType<User>().Select(member => $"{member.DisplayName} <{member.Mail}>").ToList();

This does retrieve the data, but today I realized that some of the users are missing in my list of results. Am I missing something, or is there any limitations on how many users can be retrieved?

P.S. I found the following elsewhere in StackOverflow:

Please note that it might not return all the users if there are too many. In that case you will get uri with nextLink to get next batch of results.

Can I get a sample on how this can be done?

CodePudding user response:

All Graph requests, which return a collection (like users, groups, etc.) are put into an collection object that is (within C#) named with Page at the end of the class name.

So you only get the first n element back. You can set the desired value through the $top query parameter (or in C# with .Top(x)). But be aware that the maximum allowed value can vary depending on the kind of collection you're going to ask. Most of them have a maximum value of 999, but a few have lower values (If you pick a too high number you get back an error message containing the maximum allowed value).

The retrieved page object has a property NextPageRequest which will be not null, if further data is available and you can get it by calling it:

var moreMembers = await members.NextPageRequest.GetAsync();

You can do this in a while loop, till the property NextPageRequest is null to get a list of all members.

  • Related