Office 365 – Microsoft Graph – Part 6 – Adding Office 365 Group Owner using CSOM – Codebase

Office 365 - Microsoft Graph - Adding Owner to Office 365 Group
Office 365 - Microsoft Graph - Adding Owner to Office 365 Group

Hi All,

In one of the previous article we discussed – how to add Office 365 Group owner / Member through Outlook – Office 365: Adding Office 365 group owners through Outlook – Configure the link for adding Owners/Members to the Office 365 groups

In this article we will discuss how to add owner to Office 365 Group using CSOM. We will go through high level steps and detailed code base.

In last articles we discussed fetching list of groups and respective owners. In this article we will discuss how to add group owners programmatically using CSOM.

Let’s continue our Microsoft Graph show 😊 If you missed the previous articles in this series, here is the list, please have a look once.

Office 365 – Microsoft Graph and Graph Explorer

Office 365 – Azure Active Directory – Registering/Creating new Azure App – detailed steps

Office 365 – Microsoft Graph beginning – Part 1

Office 365 – Microsoft Graph – Part 2 – Granting permission to Azure Apps to use Microsoft Graph APIs using CSOM

Office 365 – Microsoft Graph – Part 3 – Azure Access Token: to call Graph APIs from CSOM

Office 365 – Microsoft Graph – Part 4 – Fetching all Office 365 groups using CSOM- Codebase

Office 365 – Microsoft Graph – Part 5 – Fetching Office 365 group owners using CSOM – Codebase

In this article we will discuss one newer use case.

Use case: We were migrating our classic team sites from on premises to modern team sites. So, the behind the scene Office 365 group for every modern team site. Our approach is initially creating the empty modern team sites, add owners and then migrate the content. Here use-case is creating modern team sites and assigning owner to the respective group. Here in this article we will discuss how to add group owner using Microsoft Graph APIs and CSOM.

Let’s begin the show 😊

In previous articles we have discussed detailed steps. In this article we will directly go through the required code base.

High Level steps:

  1. Create the instance of HttpClient instance
  2. Set the AccessToken to the request header of HttpClient instance
  3. Rest API to be called for adding owner to the Office 365 Group – https://graph.microsoft.com/v1.0/groups/{groupId}/owners/$ref – {groupId} – Id of the Group for which we need to add the owner
  4. There is method called – PostAsync method of HttpClient instance which takes two parameters:
    • RequestURL – Rest API from step 3
    • HTTPContent – data to be passed – user id  / owner id  – Email Id of the user which we need to set the Group owner

Note: Here, REST API for adding Group owners is https://graph.microsoft.com/v1.0/groups/{groupId}/owners/$ref similarly if we need to add the members to the group then there is only one change as –
https://graph.microsoft.com/v1.0/groups/{groupId}/members/$ref

Following are the detailed steps with code:Start the Visual Studio 2017, create console application, let’s say “knowledge-junction” as

Figure 1: Office 365 – Microsoft Graph – Console Application – to verify all Office 365 groups owners

Install following require packages using NuGet manager – for details please go through last article once.

  1. Microsoft Graph
  2. Microsoft Identity Client Active Directory

Once we have required packages are in place, we can call “Microsoft Graph” APIs.

Next, create the instance of HttpClient instance and set the access token

HttpClient hc = new HttpClient();

hc.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken); //pass the access token

Next, I’ll write one method – AddOwnerToGroup and will pass the required parameters as

private static async Task AddOwnerToGroup(HttpClient hc, string groupId, string userId)
        {
            result = await hc.PostAsync($"https://graph.microsoft.com/v1.0/groups/{groupId}/owners/$ref",
               new StringContent($"{{'@odata.id':'https://graph.microsoft.com/v1.0/users/{userId}'}}",
                                Encoding.UTF8,
                                "application/json"));
            if (!result.IsSuccessStatusCode)
            {
                var res = await result.Content.ReadAsStringAsync();
                throw new ApplicationException($"Could not add user as owner. Error: " + res);
            }
        }

Figure 3: Office 365 – Microsoft Graph – Adding Owner to Office 365 Group

References: HttpClient

Thanks for reading 😊 

Keep reading, share your thoughts, experiences. Feel free to contact us to discuss more. If you have any suggestion / feedback / doubt, you are most welcome.

Stay tuned on Knowledge-Junction, will come up with more such articles

Prasham Sabadra

LIFE IS VERY BEAUTIFUL :) ENJOY THE WHOLE JOURNEY :) Founder of Knowledge Junction and live-beautiful-life.com, Author, Learner, Passionate Techie, avid reader. Certified Professional Workshop Facilitator / Public Speaker. Scrum Foundation Professional certificated. Motivational, Behavioral , Technical speaker. Speaks in various events including SharePoint Saturdays, Boot camps, Collages / Schools, local chapter. Can reach me for Microsoft 365, Azure, DevOps, SharePoint, Teams, Power Platform, JavaScript.

You may also like...

1 Response

  1. January 12, 2019

    […] Office 365 – Microsoft Graph – Part 6 – Adding Office 365 Group Owner using CSOM – Codebase […]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Microsoft 365

Subscribe now to keep reading and get access to the full archive.

Continue reading