Add Site Collection Administrators In SharePoint Using PnP Core Component

PnP which stands for Practices and Patterns is a community driven open source project where Microsoft and community members have created an implementation pattern for Office 365 and SharePoint on-premises. One of the branches of the PnP development is in PnP Core CSOM Library.

PnP Core library provides CSOM extension methods for SharePoint 2016 add-in model development. Official documentation can be accessed from here. PnP Core library increases the productivity of developers by abstracting complex operations. In this article we will see how to set up PnP core library remotely and work with  SharePoint 2016 using a console application.

In order to work with PnP Core library we first have to install the Nuget package manager which is explained in this article .Once the PnP Core Library is added we can kick of the implementation using a Console application.

Project structure

Create a console application and add the below references.

  • Microsoft.SharePoint.Client;
  • OfficeDevPnP.Core;

Scope of the article will be to perform the below operations using PnP Core CSOM Library.

  • Add new site collection administrator
  • Retrieve the site collection administrators

Add Site Collection administrator

  • Create instance of the authentication manager which will be used to create the client context.
    1. //Get instance of Authentication Manager  
    2. OfficeDevPnP.Core.AuthenticationManager authenticationManager = new OfficeDevPnP.Core.AuthenticationManager();  
    3. //Create authentication array for site url,User Name,Password and Domain  
    4. string[] authArray = { "http://sharepoint2016/sites/HOL""Priyaranjan","password-1","SharePointHOL" };
  • Create Client Context by passing the authentication details to the authentication manager object.
    1. var clientContext = authenticationManager.GetNetworkCredentialAuthenticatedContext(authArray[0], authArray[1],authArray[2], authArray[3]) 
  • Create a user entity object and add the user as the site collection admin
    1. List<OfficeDevPnP.Core.Entities.UserEntity> adminColl = new List<Core.Entities.UserEntity>();  
    2. Core.Entities.UserEntity admin = new Core.Entities.UserEntity();  
    3. admin.LoginName = @"SharePointHOL\Jinesh";  
    4. adminColl.Add(admin);  
    5. clientContext.Web.AddAdministrators(adminColl); 

Output

Upon running the script, it will add site collection administrator and will show up the console success message.



We can check the new site collection administrators form the Site Setting page as well.


Get site collection administrators

  • Create instance of the authentication manager which will be used to create the client context
    1. //Get instance of Authentication Manager  
    2. OfficeDevPnP.Core.AuthenticationManager authenticationManager = new OfficeDevPnP.Core.AuthenticationManager();  
    3. //Create authentication array for site url,User Name,Password and Domain  
    4. string[] authArray = { "http://sharepoint2016/sites/HOL""Priyaranjan","password-1","SharePointHOL" }; 
  • Create Client Context by passing the authentication details to the authentication manager object
    1. var clientContext = authenticationManager.GetNetworkCredentialAuthenticatedContext(authArray[0], authArray[1],authArray[2], authArray[3]) 
  • Create a user entity object and get the site collection admins
    1. List < OfficeDevPnP.Core.Entities.UserEntity > adminColl = new List < Core.Entities.UserEntity > ();  
    2. adminColl = clientContext.Web.GetAdministrators();  
    3. int i = 0;  
    4. foreach(Core.Entities.UserEntity admin in adminColl)  
    5. {  
    6.     i++;  
    7.     Console.WriteLine("Site Collection Administrator " + i + " : " + admin.LoginName);  

Output

Upon running the script, it will get site collection administrator and will show up in the console.



Full Code

  1. //Get instance of Authentication Manager  
  2. OfficeDevPnP.Core.AuthenticationManager authenticationManager = new OfficeDevPnP.Core.AuthenticationManager();  
  3. //Create authentication array for site url,User Name,Password and Domain  
  4. string[] authArray = {  
  5.     "http://sharepoint2016/sites/HOL",  
  6.     "Priyaranjan",  
  7.     "password-1",  
  8.     "SharePointHOL"  
  9. };  
  10.   
  11. try {  
  12.     //Create the client context  
  13.     using(var clientContext = authenticationManager.GetNetworkCredentialAuthenticatedContext(authArray[0], authArray[1], authArray[2], authArray[3])) {  
  14.         List < OfficeDevPnP.Core.Entities.UserEntity > adminColl = new List < Core.Entities.UserEntity > ();  
  15.         adminColl = clientContext.Web.GetAdministrators();  
  16.         int i = 0;  
  17.         foreach(Core.Entities.UserEntity admin in adminColl) {  
  18.             i++;  
  19.             Console.WriteLine("Site Collection Administrator " + i + " : " + admin.LoginName);  
  20.         }  
  21.   
  22.         Console.ReadLine();  
  23.     }  
  24. catch (Exception ex) {  
  25.     Console.WriteLine("Exception : " + ex.Message);  

Summary

Thus we saw how to add new Site Collection administrators to SharePoint Server 2016 using PnP Core Component.