Forum Discussion
SHWork
Aug 14, 2020Copper Contributor
Add a User to a Group/Team with Graph
I know this is basic but I've been trying for hours to get this to work. The example in the docs is wrong. I want to add a user to a group/team. Here's the code at issue: var group = new Group {...
HenryPhillipsNimbitech
Aug 16, 2020Bronze Contributor
Hi SHWork ,
Not sure if it is different if you are adding this via code as I just make use of the Graph for custom connectors or by way of PowerShell.
However I can confirm that the below would work as a straight call.
So in the request the GUID is the object ID of the group
https://graph.microsoft.com/v1.0/groups/a9f664a0-ea32-4e98-b332-68a2e92c0f9d/members/$ref
In the body, the GUID is the object ID of a user
{
"@odata.id": "https://graph.microsoft.com/v1.0/directoryObjects/a1d5b682-9554-4721-8eb8-c2175b17f4ab"
}
Response:
No Content - 204 - 226ms
Don't forget and if not doing so, test via aka.ms/ge
Thanks
Henry
SHWork
Aug 17, 2020Copper Contributor
Thanks for your help, doing it in c# is different. Fortunately I discovered that the beta version of the docs has the correct code. https://docs.microsoft.com/en-us/graph/api/group-post-members?view=graph-rest-beta&tabs=csharp
The code to add a user to a group looks like this:
var directoryObject = new DirectoryObject
{
Id = "{User ID}"
};
await graphClient.Groups["{Group ID}"].Members.References
.Request()
.AddAsync(directoryObject);