Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microsoft.Graph get all users of a group

I'm using Microsoft.Graph and I want to return a list of all users in particular groups. I can not find any digestible information about this.

I have tried using .Request().Expand(x => x.Members) but this only returns 20 users. And they are not paged (see here).

I can pull a list of groups fine, but how can I filter users based on what group they're in? This is what I've got so far...

public async Task Do()
{
    var graphClient = new GraphServiceClient(_authProvider);
    var groups = await graphClient.Groups.Request().GetAsync();

    do
    {
        foreach (var group in groups)
        {
            Console.WriteLine($"{group.Id}, {group.DisplayName}");
            Console.WriteLine("------");

            var filter = $""; // What goes here?
            var users = await graphClient.Users.Request().Filter(filter).GetAsync();

            do
            {
                foreach (var user in users)
                {
                    Console.WriteLine($"{user.Id}, {user.GivenName} {user.Surname}, {user.MemberOf}");
                }
            }
            while (users.NextPageRequest != null && (users = await users.NextPageRequest.GetAsync()).Count > 0);

            Console.WriteLine();
        }
    }
    while (groups.NextPageRequest != null && (groups = await groups.NextPageRequest.GetAsync()).Count > 0);
}
like image 675
Mardoxx Avatar asked Dec 11 '22 09:12

Mardoxx


2 Answers

Something like this works

public async Task Do()
{
    var graphClient = new GraphServiceClient(_authProvider);

    var groups = await graphClient.Groups.Request().GetAsync();

    do
    {
        foreach (var group in groups)
        {
            Console.WriteLine($"{group.Id}, {group.DisplayName}");
            Console.WriteLine("------");

            var users = await graphClient.Groups[group.Id].Members.Request().GetAsync();

            do
            {
                foreach (var user in users)
                {
                    Console.WriteLine($"{user.Id}");
                }
            }
            while (users.NextPageRequest != null && (users = await users.NextPageRequest.GetAsync()).Count > 0);

            Console.WriteLine("------");
            Console.WriteLine();
        }
    }
    while (groups.NextPageRequest != null && (groups = await groups.NextPageRequest.GetAsync()).Count > 0);
}
like image 200
Mardoxx Avatar answered Dec 28 '22 11:12

Mardoxx


This is how I did it:


    var queryOptions = new List<QueryOption>();
    queryOptions.Add(new QueryOption("$count", "true"));

    var groupMembers = await graphClient
        .Groups[groupId]
        .Members
        .Request(queryOptions)
        .Header("ConsistencyLevel", "eventual")
        .GetAsync();

    var users = new List<User>();
    users.AddRange(groupMembers.CurrentPage.OfType<User>());

    while (groupMembers.NextPageRequest != null)
    {
        groupMembers = await groupMembers.NextPageRequest.GetAsync();
        users.AddRange(groupMembers.CurrentPage.OfType<User>());
    }
like image 24
Leo Barbas Avatar answered Dec 28 '22 12:12

Leo Barbas