I'm requesting a list of users from AzureAD via Microsoft Graph.
I get the User objects back, but their MemberOf property is always null.
I thought I could use Expand to request that property specifically, and while it causes no error it also doesn't populate the property.
This question and answer from mid-2016 suggests this functionality was in beta at that time, and I thought it would have graduated to the production API by now?
var allUsers = await graphClient
.Users
.Request()
.Expand("memberOf")
.GetAsync();
var usersInGroup = allUsers
.Where(user => user.MemberOf.Any(memberOf => memberOf.Id.Equals(groupId, StringComparison.OrdinalIgnoreCase)))
.ToList();
(I've tried expanding "memberOf" and "MemberOf".)
I can retrieve a list of members via the Group.
But that returns a list of IDs, so I'd have to make two requests instead of just the one.
var groupMembers = await graphClient
.Groups[groupId]
.Members
.Request()
.GetAsync();
var groupMemberIds = groupMembers
.Select(groupMember => groupMember.Id)
.ToList();
var allUsers = await graphClient
.Users
.Request()
.GetAsync();
var usersInGroup = allUsers
.Where(user => groupMemberIds.Contains(user.Id))
.ToList();
If getting the IDs belonging to the Group, and then filtering the Users is the correct way then that's fine, I'll go with that.
Ideally I'd like to make a single request to retrieve the User objects and have the filtering done server side.
e.g.
var usersInGroup = await graphClient
.Users
.Request()
.Filter($"memberOf eq {groupId}")
.GetAsync();
Obviously that filter won't work, but something like that would be ideal.
(It was pointed out that I have been linking to the wrong set of documentation, so I've stripped out those links to prevent confusion for future readers)