Thursday, December 31, 2020

Add Microsoft Teams members in bulk

The Microsoft Graph team recently released a new way of adding multiple users to a Microsoft Teams team using a single request. The approach is straight forward and error-tolerant as it provides detailed information about which users could and could not be added to the team. This is what the new API looks like:

POST https://graph.microsoft.com/beta/teams/{team-id}/members/add

As you can see from the example above, the API is only available as a beta endpoint. Microsoft Graph beta APIs are subject to change. Hence, you should avoid using this endpoint in production applications. Have a look at the official Microsoft Graph documentation for more information.

The example below shows how to add two users to a team within one single request. The first user doesn’t exist in the tenant, but the second one is correct. In this case, the API returns an exception message for the first user, adds the second user to the team as expected and returns 207 as HTTP status code. However, if there are no error, the API returns 200 as HTTP status code and adds all users as expected. Currently, adding many users to a team is only possible by executing multiple requests which slows down the process. This new approach will be my favorite way of creating membership in a team once this API reaches GA.

POST https://graph.microsoft.com/beta/teams/{team-id}/members/add
{
   "values":[
      {
         "@odata.type":"microsoft.graph.aadUserConversationMember",
         "roles":[
            
         ],
         "user@odata.bind":"https://graph.microsoft.com/beta/users('123a3...')"
      },
      {
         "@odata.type":"microsoft.graph.aadUserConversationMember",
         "roles":[
            "owner"
         ],
         "user@odata.bind":"https://graph.microsoft.com/beta/users('96feb...')"
      }
   ]
}

This is what the response looks like:

{
   "@odata.context":"https://graph.microsoft.com/beta/...",
   "value":[
      {
         "@odata.type":"#microsoft.graph.aadUserConversationMemberResult",
         "userId":"123a3fb6-d598-482f-9c63-f124a537cf3c",
         "error":{
            "code":"NotFound",
            "message":""
         }
      },
      {
         "@odata.type":"#microsoft.graph.aadUserConversationMemberResult",
         "userId":"96feb819-e305-4fc5-99fe-6900c44d0678",
         "error":null
      }
   ]
}

The built-in error management increases the value of this new approach. I am looking forward to working with this API in the future. However, it is worth it to test this new endpoint with large amount of data and check its behavior. Thanks Microsoft Graph team for this Christmas gift. I am looking forward to seeing what the future holds for Microsoft Graph. Right now, I love to work with the Graph endpoints 💛

I hope it helps!
Thanks for reading my blog post.
I wish you all a happy, healthy, and successful 2021 🥳