Add a User to a Group/Team with Graph

%3CLINGO-SUB%20id%3D%22lingo-sub-1590110%22%20slang%3D%22en-US%22%3EAdd%20a%20User%20to%20a%20Group%2FTeam%20with%20Graph%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1590110%22%20slang%3D%22en-US%22%3E%3CP%3EI%20know%20this%20is%20basic%20but%20I've%20been%20trying%20for%20hours%20to%20get%20this%20to%20work.%20The%20example%20in%20the%20docs%20is%20wrong.%20I%20want%20to%20add%20a%20user%20to%20a%20group%2Fteam.%20Here's%20the%20code%20at%20issue%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3Evar%20group%20%3D%20new%20Group%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%20%20%20%20AdditionalData%20%3D%20new%20Dictionary%3CSTRING%3E()%3CBR%20%2F%3E%20%20%20%20%7B%3CBR%20%2F%3E%20%20%20%20%20%20%7B%22%40odata.id%22%2C%20%22https%3A%2F%2Fgraph.microsoft.com%2Fv1.0%2Fusers%2F%7B00000000-0000-0000-000000000000%7D%22%7D%3CBR%20%2F%3E%20%20%20%20%7D%3CBR%20%2F%3E%7D%3B%3CBR%20%2F%3Eawait%20graphClient.Groups%5B%22%7B00000000-0000-0000-0000-000000000000%7D%22%5D.Request().UpdateAsync(group)%3B%3C%2FSTRING%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3BI%20feel%20like%20it's%20the%20odata%20part%20that%20I%20don't%20have%20right%20but%20I%20can't%20find%20an%20example%20that%20shows%20the%20correct%20format.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1590110%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EGraph%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ETeams%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1591656%22%20slang%3D%22en-US%22%3ERe%3A%20Add%20a%20User%20to%20a%20Group%2FTeam%20with%20Graph%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1591656%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F756987%22%20target%3D%22_blank%22%3E%40SHWork%3C%2FA%3E%26nbsp%3B%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENot%20sure%20if%20it%20is%20different%20if%20you%20are%20adding%20this%20via%20code%20as%20I%20just%20make%20use%20of%20the%20Graph%20for%20custom%20connectors%20or%20by%20way%20of%20PowerShell.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHowever%20I%20can%20confirm%20that%20the%20below%20would%20work%20as%20a%20straight%20call.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESo%20in%20the%20request%20the%20GUID%20is%20the%20object%20ID%20of%20the%20group%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fgraph.microsoft.com%2Fv1.0%2Fgroups%2Fa9f664a0-ea32-4e98-b332-68a2e92c0f9d%2Fmembers%2F%24ref%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgraph.microsoft.com%2Fv1.0%2Fgroups%2Fa9f664a0-ea32-4e98-b332-68a2e92c0f9d%2Fmembers%2F%24ref%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20the%20body%2C%20the%20GUID%20is%20the%20object%20ID%20of%20a%20user%3C%2FP%3E%3CDIV%3E%3CDIV%3E%3CSPAN%3E%7B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%22%40odata.id%22%3C%2FSPAN%3E%3CSPAN%3E%3A%20%3C%2FSPAN%3E%3CSPAN%3E%22%3CA%20href%3D%22https%3A%2F%2Fgraph.microsoft.com%2Fv1.0%2FdirectoryObjects%2Fa1d5b682-9554-4721-8eb8-c2175b17f4ab%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgraph.microsoft.com%2Fv1.0%2FdirectoryObjects%2Fa1d5b682-9554-4721-8eb8-c2175b17f4ab%3C%2FA%3E%22%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%7D%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3EResponse%3A%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3ENo%20Content%20-%20204%26nbsp%3B-%20226ms%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3EDon't%20forget%20and%20if%20not%20doing%20so%2C%20test%20via%20aka.ms%2Fge%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3EThanks%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3EHenry%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1593482%22%20slang%3D%22en-US%22%3ERe%3A%20Add%20a%20User%20to%20a%20Group%2FTeam%20with%20Graph%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1593482%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F693840%22%20target%3D%22_blank%22%3E%40henryarphillips365%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20for%20your%20help%2C%20doing%20it%20in%20c%23%20is%20different.%20Fortunately%20I%20discovered%20that%20the%20beta%20version%20of%20the%20docs%20has%20the%20correct%20code.%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fgraph%2Fapi%2Fgroup-post-members%3Fview%3Dgraph-rest-beta%26amp%3Btabs%3Dcsharp%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fgraph%2Fapi%2Fgroup-post-members%3Fview%3Dgraph-rest-beta%26amp%3Btabs%3Dcsharp%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20code%20to%20add%20a%20user%20to%20a%20group%20looks%20like%20this%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Evar%20directoryObject%20%3D%20new%20DirectoryObject%0A%7B%0A%20%20%20%20Id%20%3D%20%22%7BUser%20ID%7D%22%0A%7D%3B%0Aawait%20graphClient.Groups%5B%22%7BGroup%20ID%7D%22%5D.Members.References%0A%20%20%20%20.Request()%0A%20%20%20%20.AddAsync(directoryObject)%3B%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

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
{
AdditionalData = new Dictionary<string, object>()
{
{"@odata.id", "https://graph.microsoft.com/v1.0/users/{00000000-0000-0000-000000000000}"}
}
};
await graphClient.Groups["{00000000-0000-0000-0000-000000000000}"].Request().UpdateAsync(group);

 I feel like it's the odata part that I don't have right but I can't find an example that shows the correct format.

2 Replies

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

{
}
 
Response:
 
No Content - 204 - 226ms
 
Don't forget and if not doing so, test via aka.ms/ge
 
Thanks
 
Henry

@HenryPhillipsNimbitech 

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);