Working With Site Features In SharePoint 2016 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. The 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 off 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.

  • Activate the site collection feature : SharePoint Server Publishing Infrastructure
  • Deactivate the feature using PnP Core Library.

Activate the Feature

Let’s see how to activate the feature using the PnP extension method.

  • 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])  
  • Get the GUID of the feature and deactivate the feature using the PnP extension method ‘ActivateFeature’.
    1. Guid featureID = new Guid("f6924d36-2fa8-4f0b-b16d-06b7250180fa");  
    2. //Activate the site collection feature using extension method  
    3. clientContext.Site.ActivateFeature(featureID);  
Output

Output

Output

Full Code

The full code to activate the site feature, using PnP Extension method is, as shown below:
  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. try {  
  11.     //Create the client context  
  12.     using(var clientContext = authenticationManager.GetNetworkCredentialAuthenticatedContext(authArray[0], authArray[1], authArray[2], authArray[3])) {  
  13.         Guid featureID = new Guid("f6924d36-2fa8-4f0b-b16d-06b7250180fa");  
  14.         //Activate the site collection feature using extension method  
  15.         clientContext.Site.ActivateFeature(featureID);  
  16.         Console.WriteLine("SharePoint Server Publishing Infrastructure feature has been Activated.");  
  17.         Console.ReadLine();  
  18.     }  
  19. catch (Exception ex) {  
  20.     Console.WriteLine("Exception : " + ex.Message);  
  21. }  

Deactivate the Feature

Now, let’s see how to deactivate the already activated publishing feature.

  • 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])  
  • Get the GUID of the feature and deactivate the feature using the PnP extension method ‘DeactivateFeature’
    1. Guid featureID = new Guid("f6924d36-2fa8-4f0b-b16d-06b7250180fa");  
    2. //Deactivate the site collection feature using extension method  
    3. clientContext.Site.DeactivateFeature(featureID);  
Output

Output

Output

Full Code

The full code to deactivate, the site feature using PnP Extension method, is as shown below,
  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. try {  
  11.     //Create the client context  
  12.     using(var clientContext = authenticationManager.GetNetworkCredentialAuthenticatedContext(authArray[0], authArray[1], authArray[2], authArray[3])) {  
  13.         Guid featureID = new Guid("f6924d36-2fa8-4f0b-b16d-06b7250180fa");  
  14.         //Deactivate the site collection feature using extension method  
  15.         clientContext.Site.DeactivateFeature(featureID);  
  16.         Console.WriteLine("SharePoint Server Publishing Infrastructure feature has been Deactivated.");  
  17.         Console.ReadLine();  
  18.     }  
  19. catch (Exception ex) {  
  20.     Console.WriteLine("Exception : " + ex.Message);  
  21. }  

Summary

You can copy paste the ‘Full Code’ fragment into the Main() of the Console application of the project structure given at the starting of the article, to test it out from your end. Thus, we have seen how to activate and deactivate site features in SharePoint 2016 remotely using PnP Core Library. In this method, you don’t have to get into the Server where SharePoint 2016 is installed and it enables us to work remotely.