skip to Main Content

we are using the following query to get the list of all the members in a group

https://graph.microsoft.com/v1.0/groups/{id}/members

requirement

  1. We need to get all the users in a group and also the count of the groups each users belongs too in a single call, so that we dont delete users who are part of multiple groups.

couldn’t find any information on searching on multiple entities (group and user).

2

Answers


  1. Make use of below code to get all the users in a group and also the count of the groups each users belongs too in a single call:

    namespace GraphGroupMembers
    {
        class Program
        {
            private static async Task Main(string[] args)
            {
                
                string tenantId = "TenantID";
                string clientId = "ClientID";
                string clientSecret = "ClientSecret";
                var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret);
    
                var graphClient = new GraphServiceClient(clientSecretCredential, new[] { "https://graph.microsoft.com/.default" });
    
                var groupId = "GroupID";
    
                var result = await graphClient.Groups[groupId].Members.GraphUser.GetAsync((requestConfiguration) =>
                {
                    requestConfiguration.QueryParameters.Expand = new string[] { "memberof/microsoft.graph.group($select=id)" };
                });
    
                
                foreach (var user in result.Value)
                {
                    if (user != null)
                    {
                        var groupCount = user.MemberOf?.Count ?? 0;
                        Console.WriteLine($"User: {user.DisplayName}, Group Membership Count: {groupCount}");
                    }
                }
            }
        }
    }
    

    enter image description here

    Login or Signup to reply.
  2. With a single query, you can retrieve users members of a specific group and expand groups where the user is a member of.

    GET /v1.0/groups/{group_id}/members/microsoft.graph.user?$expand=memberof/microsoft.graph.group($select=id)
    

    Counting must be done on the client

    If you prefer the Microsoft Graph .NET SDK

    var result = await graphClient.Groups["{group-id}"].Members.GraphUser.GetAsync((rc) =>
    {
        rc.QueryParameters.Expand = new string []{ "memberof/microsoft.graph.group($select=id)" };
    });
    
    foreach (var user in result.Value)
    {
        var groupCount = user.MemberOf.Count;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search