Office 365 – Microsoft Graph – Part 7 – Removing owner from Office 365 Group using CSOM – Codebase

:Office 365 – Microsoft Graph – Removing Owner from Office 365 Group Owners
:Office 365 – Microsoft Graph – Removing Owner from Office 365 Group Owners

Hi All,

In last articles we discussed fetching list of groups and respective owners, adding owners to the Office 365 Group.

In this article we will discuss how to remove Office 365 group owner programmatically using CSOM.

Let’s continue our Microsoft Graph show 😊 

In case 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

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

In this article we will continue with previous article use case.

Use case:

  1. 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.
  2.  Our approach is initially creating the empty modern team sites, add owners and then migrate the content. Here we are creating modern team site with our default service account. So, default owner of modern team site and hence of Group is our service account. 
  3. Once we added actual owners then we need to remove the default service account. Use-case is removing default service account as a group owner.

Here in this article we will discuss how to remove group owner using Microsoft Graph APIs and CSOM.

Here steps are exactly same as in previous article only change is calling DeleteAsync() of HttpClient  class to remove the group owner.

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 removing 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 – DeleteAsync 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 remove Group owner

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 one of previous 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 – RemoveOwnerFromGroupand will pass the required parameters as

/// <summary>
        /// Method to remove the owner from the group
        /// </summary>
        /// <param name="hc">instance of HttpClient</param>
        /// <param name="groupId">Id of the group from which we need to remove the Owner</param>
        /// <param name="userId">Id of the user which we need to remove as owner</param>
        /// <returns></returns>
        private static async Task RemoveOwnerFromGroup(HttpClient hc, string groupId, string userId)
        {
            var result = await hc.DeleteAsync($"https://graph.microsoft.com/beta/groups/{groupId}/owners/{userId}/$ref");

            result = await hc.DeleteAsync($"https://graph.microsoft.com/beta/groups/{groupId}/members/{userId}/$ref");

            if (!result.IsSuccessStatusCode)
            {
                var res = await result.Content.ReadAsStringAsync();
                throw new ApplicationException($"Could not remove user from owners. Error: " + res);
            }
        }

Figure 2:Office 365 – Microsoft Graph – Removing Owner from Office 365 Group Owners

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

Leave a Reply

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

%d bloggers like this: