8

How do I add Members to a Group via Microsoft Graph API?

According to documentation for adding Member to a particular Group, it requires the call below:

POST https://graph.microsoft.com/v1.0/groups/{id}/members/$ref
Content-type: application/json
Content-length: 30
{
    "@odata.id": "https://graph.microsoft.com/v1.0/users/{id}"
}

My questions lies in this API:

https://graph.microsoft.com/v1.0/groups/{id}/members/$ref
  • {id} => the group id,

  • members => adding members to the group

Now where is the users/members data/parameter to be added or posted?

Is it "@odata.id": "https://graph.microsoft.com/v1.0/users/{id}"?

Do I post @odata.id values as a member/users parameter when adding a member to a group?

3 Answers 3

10

That is correct. What you're technically passing is an ODATA Reference (ref$) to the user object within Active Directory rather than just an id.

To illustrate, lets take this fictitious user:

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users/$entity",
    "id": "48d31887-5fad-4d73-a9f5-3c356e68a038",
    "businessPhones": [
        "+1 412 555 0109"
    ],
    "displayName": "Megan Bowen",
    "givenName": "Megan",
    "jobTitle": "Auditor",
    "mail": "[email protected]",
    "mobilePhone": null,
    "officeLocation": "12/1110",
    "preferredLanguage": "en-US",
    "surname": "Bowen",
    "userPrincipalName": "[email protected]"
}

If we wanted to add Megan to the a Group with an id of 02bd9fd6-8f93-4758-87c3-1fb73740a315 the call would look like this:

POST https://graph.microsoft.com/v1.0/groups/02bd9fd6-8f93-4758-87c3-1fb73740a315/members/$ref
Content-type: application/json

{
  "@odata.id": "https://graph.microsoft.com/v1.0/users/[email protected]"
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hello @MarcLaFleur Could you please help me understand $ref? Is it predefined or how can I get it? Thank you,
3

It is also working when passing user Id:

POST url: https://graph.microsoft.com/v1.0/groups/{group_id}/members/$ref
Content-type: application/json

Content string: {"@odata.id": "https://graph.microsoft.com/v1.0/users/{user_id}"}

group_id - group object id

user_id - user object id

2 Comments

Hello @Maria Could you please help me understand $ref? Is it predefined or how can I get it? Thank you,
$ref is static, it is not variable
0

You can't use User Principal name or email, such as [email protected] replacing the userID in the request body. I tested it is not working.

So, this is the correct answer:

POST url: https://graph.microsoft.com/v1.0/groups/{group_id}/members/$ref
Content-type: application/json

Content string: {"@odata.id": "https://graph.microsoft.com/v1.0/users/{user_id}"}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.