Working With Web Features In SharePoint 2016 Using PnP Core Component

PnP stands for Practices and Patterns and 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 here. PnP Core library increases the productivity of the developers by abstracting the 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 this 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;

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

  • Activate the Web feature- SharePoint Server Publishing.
  • Deactivate the feature, using PnP Core Library.

Activate the feature.

  • 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();  
    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’. ‘94c94ca6-b32f-4da9-a9e3-f3d343d7ecb’ is the feature ID for ‘SharePoint Server Publishing’.
    1. Guid featureID = new Guid("94c94ca6-b32f-4da9-a9e3-f3d343d7ecb");  
    2. //Activate the feature  
    3. clientContext.Web.ActivateFeature(featureID);   
    Output

    The publishing feature has been activated.

    Output

    Output

    Full Code

    The full code to activate the Web feature is 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("94c94ca6-b32f-4da9-a9e3-f3d343d7ecb");  
    14.         // Activate the feature  
    15.         clientContext.Web.ActivateFeature(featureID);  
    16.         Console.WriteLine("The publishing web feature has been activated.");  
    17.         Console.ReadLine();  
    18.     }  
    19. catch (Exception ex) {  
    20.     Console.WriteLine("Exception : " + ex.Message);  
    21. }  

Deactivate the Feature

  • 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();  
    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 GUID of the Web feature and deactivate the feature, using the PnP extension method ‘DeactivateFeature’.
    1. Guid featureID = new Guid("94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb");  
    2. //Deactivate the web feature  
    3. clientContext.Web.DeactivateFeature(featureID);  
    Output

    The activated feature has been deactivated.

    Output

    Output

    Full Code

    The full code to deactivate the feature is 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("94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb");  
    14.         //Deactivate the web feature  
    15.         clientContext.Web.DeactivateFeature(featureID);  
    16.         Console.WriteLine("The publishing web feature has been deactivated.");  
    17.         Console.ReadLine();  
    18.     }  
    19. catch (Exception ex) {  
    20.     Console.WriteLine("Exception : " + ex.Message);  
    21. }  

Summary

You can copy and 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 at your end. Thus, we have seen, how to activate and deactivate the Web features in SharePoint 2016 operations on a remote basis, using PnP Core library. In this method, you don’t have to get into the Server, where SharePoint 2016 is installed and enables us to work on a remote basis.

Up Next
    Ebook Download
    View all
    Learn
    View all