How to Add / Remove Site Collection Administrator in SharePoint Office 365 Programmatically using C# Client Site Object Model (CSOM)

Sathish Nadarajan
 
Solution Architect
March 30, 2017
 
Rate this article
 
Views
8385

In the previous article, we saw how to add a User as Site Collection administrator in SharePoint Office 365. But, it requires a Tenant Admin User Credentials. But, most of the cases, we may not get that for all the customers. Hence, thought of coming up with an approach with Site Collection Permission itself. The below code is self-explanatory.

To Add the Site Collection Administrator

 namespace Console.Office365
 {
     using OfficeDevPnP.Core;
     using OfficeDevPnP.Core.Entities;
     using System;
     using System.Collections.Generic;
     using System.Linq;
     using System.Text;
     class Program
     {
         static void Main(string[] args)
         {
             AuthenticationManager authManager = new AuthenticationManager();
             var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant("https://********.sharepoint.com/sites/CommunitySite/", "Sathish@*****.com", "**********");
             
             clientContext.Load(clientContext.Web);
             clientContext.Load(clientContext.Site);
             clientContext.Load(clientContext.Site.RootWeb);
             clientContext.ExecuteQuery();
 
             List<string> lstTargetGroups = new List<string>();
             lstTargetGroups.Add("User1@********.com"); // Add the Users here.
 
             List<UserEntity> admins = new List<UserEntity>();
             foreach (var targetGroup in lstTargetGroups)
             {
                 UserEntity adminUserEntity = new UserEntity();
                 adminUserEntity.LoginName = targetGroup;
                 admins.Add(adminUserEntity);
             }
 
             if (admins.Count > 0)
             {
                 clientContext.Site.RootWeb.AddAdministrators(admins, true);
             }
         }
     }
 }

The above piece of code will add all the Users added on the List lstTargetGroups.

Now, let us see how to remove the site collection administrators.

 namespace Console.Office365
 {
     using OfficeDevPnP.Core;
     using System;
     class Program
     {
         static void Main(string[] args)
         {
             AuthenticationManager authManager = new AuthenticationManager();
             var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant("https://*****.sharepoint.com/sites/CommunitySite/", "Sathish@*******.com", "******");
 
             clientContext.Load(clientContext.Web);
             clientContext.Load(clientContext.Site);
             clientContext.Load(clientContext.Site.RootWeb);
             clientContext.ExecuteQuery();
 
 
 
             var users = clientContext.Site.RootWeb.SiteUsers;
             clientContext.Load(users);
             clientContext.ExecuteQuery();
 
 
             foreach (var user in users)
             {
                 System.Console.ForegroundColor = ConsoleColor.White;
                 System.Console.WriteLine("Removing the User - " + user.Title.ToLower());
                 user.IsSiteAdmin = false;
                 user.Update();
                 clientContext.Load(user);
                 clientContext.ExecuteQuery();
 
             }
         }
     }
 }

The above Methods does not require the tenant Level Permission. The User Name and Password is at the Site Collection Level itself. Hope this helps.

Happy Coding,

Sathish Nadarajan.

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment