I want to access all members of all groups through Microsoft Graph. I want to gather this in a single call as opposed to iterating over each group.
I only want to select a subset of information for the expanded members.
I can't find a way to do this, but I believe it is supported. What is the appropriate way to select on an expand?
As far as I can tell, this should work to give me what I want, however, the result returns without the members property:
https://graph.microsoft.com/v1.0/groups?$expand=members($select=id,userPrincipalName)
This, however, does return all members, but provides way more information than I'm looking for:
https://graph.microsoft.com/v1.0/groups?$expand=members
I've tried this in two places:
Microsoft Graph API explorer (Specific examples from above).
Within my code in a real environment. I'm going to omit the code here as the results from code are the same as the Graph Explorer.
edit: While I've accepted Marc's answer as it helps solve this here, for others, I'm going to note that the following will still return groups and members in a single (albeit large) response:
https://graph.microsoft.com/v1.0/groups?$expand=members
While your query is properly formatted from an OData perspective, Groups doesn't implement this pattern. You'll need to query the membership for each Group separately.
What you can do is reduce the number of calls using JSON Batching. You can batch up to 20 calls at a time. The most straightforward way to do this is to set your page size to 20 records like this:
https://graph.microsoft.com/v1.0/groups?$select=id&$top=20
For each page, you then send a batch request for the groups on that page:
POST https://graph.microsoft.com/v1.0/$batch
{
"requests": [
{
"id": "1",
"method": "GET",
"url": "/groups/{group-id}/members"
},
{
"id": "2",
"method": "GET",
"url": "/groups/{group-id}/members"
},
//...
{
"id": "20",
"method": "GET",
"url": "/groups/{group-id}/members"
}
]
}
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