Search code examples
office365microsoft-graph-api

Microsoft Graph: List all users and their groups in one request


I would like to list all users. For each user, I need to display the roles and groups specific to that user.

I tried:

https://graph.microsoft.com/v1.0/users?$expand=memberOf

But it gives exactly the same result as:

https://graph.microsoft.com/v1.0/users

According to the doc for the user object (http://graph.microsoft.io/en-us/docs/api-reference/v1.0/resources/user), I should be able to list the roles and groups for the user by using the memberOf relationship.

I can get the roles and groups I need for every user by doing one request per user (using https://graph.microsoft.com/v1.0/users/{user_id}/getMemberObjects) but it's a bit slow and overkill.

What am I missing?


Solution

  • Expanding navigation properties on user entities is currently not working on the production (v1.0) version of the Microsoft Graph endpoint. The functionality is live on the beta endpoint.

    This query works as you desire:

    https://graph.microsoft.com/beta/users?$expand=memberOf
    

    There is no timeline on when features move from beta to v1.0 or whether they even will in the current form.

    Currently, you have 3 choices using the Microsoft Graph APIs.

    Beta endpoint

    Use the beta endpoint but understand that it may change in functionality.

    Multiple Graph Calls

    Fetch the users collection then fetch the memberOf for each user as you need it.

    https://graph.microsoft.com/v1.0/users/{id}/memberOf
    

    or

    https://graph.microsoft.com/v1.0/users/{user_id}/getMemberObjects
    

    Expand members on Groups

    If you want to stick with the v1.0 endpoint and depending on your overall goal, you can attempt to go about finding the information you want the other way. Get the collection of groups and expand the members navigation property instead.

    https://graph.microsoft.com/v1.0/groups/?$expand=members