Microsoft Graph API with Azure AD B2C: Get all users and their group membership

Viewed 1281

We are trying to retrieve a list of all users in our B2C directory, and include the group memberships for all users.

We found this query, but unfortunately it is not supported for B2C:

https://graph.microsoft.com/v1.0/users?$select=displayname&$expand=memberof

Are there any workarounds for a B2C tenant?

We've considered two solutions, but neither are scalable:

  1. iterating over each user and retrieving their membership
  2. iterating over all known groups and retrieving members
1 Answers

As you have known, $expand=memberof is not supported for B2C, we can only choose the 2 solutions you mentioned.

To avoid more requests, maybe the solution 2 is better. But the logic is relatively more complicated. Firstly you have to query the list of all the users in case of that there is any user is not a member of any groups. Then you need to list all the groups and get the group members by using /groups/groupid/members. Finally, you can summarize all user and group member information.

The logic of solution 1 is simpler. You just need to call /users and /users/{id | userPrincipalName}/memberOf one by one. But since there are definitely more users than groups, the requests are more than solution 2.

I'm afraid there are no other better workarounds. Which solution you choose depends on performance and logic requirements.

Related