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);
}
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);
}
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>());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With