title: MS Graph API C# Add user to group
body: <p>I've been investigating how to add (and later remove) a user from an Azure AD group using the Microsoft Graph API (the dotnet/C# library available on nuget).</p>
  
  <p><a href="https://www.nuget.org/packages/Microsoft.Graph" rel="noreferrer">Nuget MS Graph API</a></p>
  
  <p>Ignoring all else around getting a connected <code>GraphServiceClient</code> etc. I'm trying code very similar to the sample below, not getting any exception (suggesting things are fine) but when I get the group once again via the API, it's not got any members still!</p>
  
  <p><a href="https://i.stack.imgur.com/E5Seb.png" rel="noreferrer"><img src="https://i.stack.imgur.com/E5Seb.png" alt="Quickwatch of returned group object after modification attempt"></a></p>
  
  <p>Interestingly (as an aside), when I ask for <code>memberOf</code> property on the <code>user</code> object and tell it to expand it, it comes back null still.</p>
  
  <pre><code>var user = await client.Users[userPrincipalName]
               .Request()
                 .Select("id,memberOf")
                   .Expand("memberOf")
                     .GetAsync();
  
  var group = await client.Groups[groupId]
                .Request()
                  .Select("members")
                    .Expand("members")
                      .GetAsync();
  
  group.Members.Add(user);
  
  await client.Groups[groupId].Request().UpdateAsync(group);
  
  // userPrincipalName =&gt; "test.user@mytenant.com"
  // groupId =&gt; the object GUID for the group
  </code></pre>
  
  <p>Does anyone know what I'm doing wrong here? The docs I used to come up with this code were based on the links to the following documents:</p>
  
  <p><a href="https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/resources/group" rel="noreferrer">API doc for 'group'</a></p>
  
  <p><a href="https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/group_post_members" rel="noreferrer">Adding a member to a group</a></p>
  
  <p>Also, I tried to style the approach on the solution suggested here to setting licenses for users:</p>
  
  <p><a href="https://github.com/microsoftgraph/microsoft-graph-docs/issues/744" rel="noreferrer">Assign user license via Graph API</a></p>
  
  <p>As usual, thanks for any help.</p>
  
  <p>Peter</p>
  
  <p><strong>Additional</strong></p>
  
  <p>I've also tried poking around in the graph API looking at potentially updating the <code>.Members</code> property/resource rather than the group itself:</p>
  
  <pre><code>await client.Groups[groupId].Members.Request(). // &lt;-- Only has GetAsync()
  </code></pre>
  
  <p>But it only has the <code>GetAync()</code> method available to it.</p>
  
  <p><strong>Updated based on answer</strong></p>
  
  <pre><code>var usersGroups = await client.Users[userPrincipalName].MemberOf.Request().GetAsync();
  
  if (!usersGroups.Any(g =&gt; g is Group &amp;&amp; g.Id == groupId))
  {
      // User is not a member of the group, add them.
      var user = await client.Users[userPrincipalName].Request().Select("id").GetAsync();
      await client.Groups[groupId].Members.References.Request().AddAsync(user);
  }
  </code></pre>
  
  <p>I've added the code snippet above based on the answer, as I think it succinctly answers the issue regarding adding members.</p>
  
  <p>Thanks to Nan Yu for the answer.</p>