Home > Mobile >  Get ONLY the count of members of 'User' type from an AAD group using Graph API
Get ONLY the count of members of 'User' type from an AAD group using Graph API

Time:09-27

Is there a Graph API to get the count of members of a specific type from AAD group? For example, consider the following AAD group:

enter image description here

This group contains 3 members of type 'User'. Is there a Graph API to get ONLY that count i.e. 3? Or should I get all members from the group and do some filtering to get the members of 'User' type as follows and find out the count:

var users = new List<Guid>();
var response = await graphClient
           .Groups[groupId]
           .TransitiveMembers
           .Request()           
           .GetAsync();

users.AddRange(ToUsers(response));

private IEnumerable<Guid> ToUsers(IEnumerable<DirectoryObject> members)
{
            foreach (var directoryObj in fromGraph)
            {
                switch (directoryObj)
                {                    
                    case User user:
                        yield return Guid.Parse(user.Id);
                        break;
                    default:
                        break;
                }
            }
}

CodePudding user response:

To get the count of members of a specific type from AAD group, you can make use of below query:

GET https://graph.microsoft.com/v1.0/groups/<group_id>/members/microsoft.graph.user/$count

I tried to reproduce the same in my environment and got the below results:

I created one Azure AD group with members of different types like below:

enter image description here

I ran the below query via Graph Explorer and got the count of members of User type successfully as below:

GET https://graph.microsoft.com/v1.0/groups/<group_id>/members/microsoft.graph.user/$count

Make sure to add ConsistencyLevel : Eventual request header while running the query.

Response:

enter image description here

You can get the code in any language by selecting Code snippets tab like below:

enter image description here

Code sample in C#

GraphServiceClient graphClient = new GraphServiceClient( authProvider );

var int32 = await graphClient.Groups["groupID"].Members.$count
.Request()
.Header("ConsistencyLevel","Eventual")
.GetAsync();

CodePudding user response:

Firstly, let's see the graph enter image description here

By the way, you may want to append /$count as well. so that the code should like below:

var requestOptions = new List<HeaderOption>()
{
    new HeaderOption("ConsistencyLevel", "eventual")
};
var groupUrl = graphClient.Groups["group_id"].Members.AppendSegmentToRequestUrl("microsoft.graph.user/$count");
var groupRequest = await new GraphServiceGroupsCollectionRequest(groupUrl, graphClient, requestOptions).GetAsync();

But it will throw exception, because the response is a number but not Group. So we can't use it except we use http client to send get request manually, but it's not related to graph SDK then. Anyway I'll share the code here.

var clientSecretCredential = new ClientSecretCredential(
    tenantId, clientId, clientSecret);
var tokenRequestContext = new TokenRequestContext(scopes);
var token = clientSecretCredential.GetTokenAsync(tokenRequestContext).Result.Token;
httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token );
var groupUrl = graphClient.Groups["group_id"].Members.AppendSegmentToRequestUrl("microsoft.graph.user/$count");
HttpResponseMessage response = await _httpClient.GetAsync(groupUrl);

In the last, pls see screenshot below. So the code snippet generated by graph explorer is wrong.

enter image description here

  • Related