Add/Retrieve Site Collection Admins To/From SharePoint Online Site Using PnP Core CSOM Library

Introduction
 
In this article, you will learn how to retrieve or add users as site collection administrators from/to SharePoint online sites, using PnP Core CSOM library.
 
The main advantage of using PnP Core libraries is the reduced code to get the required information. The required object can be retrieved with a very small piece of code, once the client context is set.
 
Prerequisite
  • PnP Core CSOM documentation can be found on the official site here.
  • PnP Core CSOM library packages can be downloaded here
The code, given below, is being tested, using Visual Studio console Application. Once the console application is created, the packages can be installed, using "Install-Package SharePointPnPCoreOnline" command on Package Manager console of Visual Studio. Once installed, the references and packages will be imported to the solution.
 
The references used in the sample are given below.
  • Microsoft.SharePoint.Client
  • OfficeDevPnP.Core 
Connect to SharePoint Online site
 
The Authentication Manager is used to retrieve the client context of the site. To connect to SharePoint Online site, the token, given below is used.
  • GetSharePointOnlineAuthenticatedContextToken 
The parameters required are.
  • SharePoint Online site URL
  • Tenant UserId
  • Tenant Password (or secured string) 
Add Site Collection Admins
 
Users can be added as site collection admins to SharePoint online sites, using PnP Core component. The following steps explain the process in detail.
  • Input the site detail, user details for authentication, and site collection admin information.
  • Authenticate and get the client context of the site and then the necessary Web object.
  • Using Web object, add the users using AddAdministrators method. The required parameters are given below.

    • User entities
    • Owner Permissions (Boolean)

  • Display the results. 
The code snippet, given below, shows the logic.
  1. // Input Parameters      
  2. string siteUrl = "https://nakkeerann.sharepoint.com/";  
  3. string userName = "[email protected]";  
  4. string password = "***";  
  5.   
  6. // PnP component to set context      
  7. AuthenticationManager authManager = new AuthenticationManager();  
  8. try  
  9. {  
  10.     // Get and set the client context  
  11.     // Connects to SharePoint online site using inputs provided  
  12.     using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))  
  13.     {  
  14.         // Input Parameters  
  15.         List<UserEntity> admins = new List<UserEntity>();  
  16.         UserEntity admin = new UserEntity();  
  17.         admin.LoginName = "nirmal";  
  18.         admins.Add(admin);  
  19.         // Multiple users can be added as admins with the help of above list  
  20.   
  21.         // Adds Site Collection Admins  
  22.         clientContext.Site.RootWeb.AddAdministrators(admins, true);  
  23.   
  24.         Console.WriteLine("User added as Site Collection Admin");  
  25.         Console.ReadKey();  
  26.     }  
  27. }  
  28. catch (Exception ex)  
  29. {  
  30.     Console.WriteLine("Error Message: " + ex.Message);  
  31.     Console.ReadKey();  
  32. }   
Retrieve Site Collection Administrators 
 
Site collection admin details can be accessed from SharePoint Online sites, using PnP Core component. The steps, given below, explain the process in detail.
  • Input the site detail, user details for authentication.
  • Authenticate and get the client context of the site and then the necessary Web object.
  • Using Web object, retrieve the users using GetAdministrators method.
  • Display the results.
The code snippet, given below, shows the logic.
  1. // Input Parameters      
  2. string siteUrl = "https://nakkeerann.sharepoint.com/";  
  3. string userName = "[email protected]";  
  4. string password = "***";  
  5.   
  6. // PnP component to set context      
  7. AuthenticationManager authManager = new AuthenticationManager();  
  8. try  
  9. {  
  10.     // Get and set the client context  
  11.     // Connects to SharePoint online site using inputs provided  
  12.     using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))  
  13.     {  
  14.         // Gets Site Collection Admins  
  15.         List<UserEntity> admins = clientContext.Site.RootWeb.GetAdministrators();  
  16.   
  17.         foreach (UserEntity admin in admins)  
  18.         {  
  19.             Console.WriteLine("Admin Name : " + admin.Title);  
  20.         }  
  21.         Console.ReadKey();  
  22.     }  
  23. }  
  24. catch (Exception ex)  
  25. {  
  26.     Console.WriteLine("Error Message: " + ex.Message);  
  27.     Console.ReadKey();  
  28. }   
The below snapshot shows the administrators page.
 
 
 
Summary
 
Thus, you have learned retrieving and adding users from/to site collection administrators group, using PnP Core CSOM library components. The above operations are compatible with SharePoint Online sites.

Up Next
    Ebook Download
    View all
    Learn
    View all