Forum Discussion

SHWork's avatar
SHWork
Copper Contributor
Aug 14, 2020

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
{
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

    {
    "@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's avatar
      SHWork
      Copper Contributor

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

       

       

Resources