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.
-
- OfficeDevPnP.Core.AuthenticationManager authenticationManager = new OfficeDevPnP.Core.AuthenticationManager();
-
- string[] authArray = { "http://sharepoint2016/sites/HOL", "Priyaranjan","password-1","SharePointHOL" };
- Create Client Context by passing the authentication details to the Authentication Manager object,
- 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’.
- Guid featureID = new Guid("f6924d36-2fa8-4f0b-b16d-06b7250180fa");
-
- clientContext.Site.ActivateFeature(featureID);
Output
Full Code
The full code to activate the site feature, using PnP Extension method is, as shown below:
-
- OfficeDevPnP.Core.AuthenticationManager authenticationManager = new OfficeDevPnP.Core.AuthenticationManager();
-
- string[] authArray = {
- "http://sharepoint2016/sites/HOL",
- "Priyaranjan",
- "password-1",
- "SharePointHOL"
- };
- try {
-
- using(var clientContext = authenticationManager.GetNetworkCredentialAuthenticatedContext(authArray[0], authArray[1], authArray[2], authArray[3])) {
- Guid featureID = new Guid("f6924d36-2fa8-4f0b-b16d-06b7250180fa");
-
- clientContext.Site.ActivateFeature(featureID);
- Console.WriteLine("SharePoint Server Publishing Infrastructure feature has been Activated.");
- Console.ReadLine();
- }
- } catch (Exception ex) {
- Console.WriteLine("Exception : " + ex.Message);
- }
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,
-
- OfficeDevPnP.Core.AuthenticationManager authenticationManager = new OfficeDevPnP.Core.AuthenticationManager();
-
- string[] authArray = { "http://sharepoint2016/sites/HOL", "Priyaranjan","password-1","SharePointHOL" };
- Create Client Context by passing the authentication details to the authentication manager object,
- 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’
- Guid featureID = new Guid("f6924d36-2fa8-4f0b-b16d-06b7250180fa");
-
- clientContext.Site.DeactivateFeature(featureID);
Output
Full Code
The full code to deactivate, the site feature using PnP Extension method, is as shown below,
-
- OfficeDevPnP.Core.AuthenticationManager authenticationManager = new OfficeDevPnP.Core.AuthenticationManager();
-
- string[] authArray = {
- "http://sharepoint2016/sites/HOL",
- "Priyaranjan",
- "password-1",
- "SharePointHOL"
- };
- try {
-
- using(var clientContext = authenticationManager.GetNetworkCredentialAuthenticatedContext(authArray[0], authArray[1], authArray[2], authArray[3])) {
- Guid featureID = new Guid("f6924d36-2fa8-4f0b-b16d-06b7250180fa");
-
- clientContext.Site.DeactivateFeature(featureID);
- Console.WriteLine("SharePoint Server Publishing Infrastructure feature has been Deactivated.");
- Console.ReadLine();
- }
- } catch (Exception ex) {
- Console.WriteLine("Exception : " + ex.Message);
- }
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.