Add Site Collection Administrator To SharePoint 2016 Using PnP Core Extension Methods

PnP stands for Practices and Patterns, which 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 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 here. PnP Core library increases the productivity of the developers by abstracting complex operations. In this article, we will see how to set up PnP core library on a remote basis 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 the article.

Once PnP Core Library is added, we can kick of the implementation, using a console Application.

Project structure

Create a console Application and add the references, given below.

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

Add Site Collection administrator 

  • Create an 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();  
  4. //Create authentication array for site url,User Name,Password and Domain  
  5. string[] authArray = { "http://sharepoint2016/sites/HOL""Priyaranjan","password-101","SharePointHOL" };  
  •    Create Client Context by passing the authentication details to the authentication manager object. 

  1. var clientContext = authenticationManager.GetNetworkCredentialAuthenticatedContext(authArray[0], authArray[1],auhArray[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>();  
  3. Core.Entities.UserEntity admin = new Core.Entities.UserEntity();  
  4. admin.LoginName = @"SharePointHOL\Jinesh";  
  5. adminColl.Add(admin);  
  6. clientContext.Web.AddAdministrators(adminColl);  

Full Code

  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Text;    
  5. using System.Threading.Tasks;   
  6. using Microsoft.SharePoint.Client;    
  7. using OfficeDevPnP.Core;    
  8. namespace OfficeDevPnP    
  9. {  
  10.     class SP2016  
  11.     {  
  12.         static void Main(string[] args)   
  13.        {    
  14.         //Get instance of Authentication Manager  
  15.         OfficeDevPnP.Core.AuthenticationManager authenticationManager = new OfficeDevPnP.Core.AuthenticationManager();  
  17.         //Create authentication array for site url,User Name,Password and Domain  
  18.         string[] authArray = { "http://sharepoint2016/sites/HOL""Priyaranjan""password-101""SharePointHOL" };  
  20.         try   
  21.         {  
  22.             //Create the client context    
  23.              using (var clientContext = authenticationManager.GetNetworkCredentialAuthenticatedContext(authArray[0], authArray[1], authArray[2], authArray[3]))   
  24.             {    
  25.              List<OfficeDevPnP.Core.Entities.UserEntity> adminColl = new List<Core.Entities.UserEntity>();   
  26.              Core.Entities.UserEntity admin = new Core.Entities.UserEntity();    
  27.              admin.LoginName = @"SharePointHOL\Jinesh";    
  28.              adminColl.Add(admin);    
  29.              clientContext.Web.AddAdministrators(adminColl);    
  30.              Console.WriteLine("New Site Collection Administrator added.");   
  31.              Console.ReadLine();    
  32.             }    
  33.         }    
  34.         catch (Exception ex)    
  35.         {  
  36.           Console.WriteLine("Exception : " + ex.Message);  
  38.         }    
  39.     }    
  40.   }    
  41. }  


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


Thus, we saw how to add a site collection administrator to SharePoint Server 2016, using PnP Core extension methods. 

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now