Working With SharePoint Online Web Scoped Features Using PnP Core CSOM Library

Introduction

In this article, you will learn all the basic web scoped feature operations performed on SharePoint online site or sub sites, using PnP Core "Client Side Object Model" library.


The operations are compatible for site collection or sub sites.


Prerequisites

  • PnP Core CSOM documentation can be found from the official site here.

  • PnP Core CSOM packages can be downloaded from here.

The code sample explained below, is being tested using Visual Studio console application. Once the Console Application is created, the packages can be installed, using Install-Package SharePointPnPCoreOnline command on package manager console of Visual Studio. Once installed, the references and packages will be imported to the solution.


The references used in the sample are given below-

  • Microsoft.SharePoint.Client

  • OfficeDevPnP.Core 


Connect to SharePoint online site or sub site

The Authentication Manager is used to retrieve the client context of the site. To connect to SharePoint online site, the method, given below, is used-

  • GetSharePointOnlineAuthenticatedContextToken 

The parameters required are,

  • SharePoint online site URL

  • Tenant UserId

  • Tenant Password (or secured string)

The web scoped features can be accessed from the portal using https://siteurl/_layouts/15/ManageFeatures.aspx?Scope=Web


The following operations explain the site collection feature operations for SharePoint online site in detail.


Check If Web Scoped Feature is Active Users can check whether the web scoped feature is already activated on the site or sub site, using PnP Core CSOM library. The steps involved are,

  • Get the web scoped feature ID from the list available. (https://blogs.msdn.microsoft.com/razi/2013/10/28/listing-all-sharepoint-server-2013-features-including-name-title-scope-id-and-description/)

  • Authenticate and get the client context of the site and then get the web object.

  • Check if the feature is active using IsFeatureActive method with feature ID as GUID.

  • Output the required result on the console. 

The following code snippet shows the code sample to check the active web features.

  1. // Input Parameters

  2. string siteUrl = "https://nakkeerann.sharepoint.com/sites/learning";  

  3. string userName = "abc@nakkeerann.onmicrosoft.com";  

  4. string password = "***";  

  5. // PnP component to set context

  6. AuthenticationManager authManager = new AuthenticationManager();  

  7. try

  8. {  

  9. // Get and set the client context

  10. // Connects to SharePoint online site using inputs provided

  11. using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))      

  12. {  

  13. // Minimal download strategy Feature ID (Web scoped)         

  14. Guid featureId = new Guid("87294c72-f260-42f3-a41b-981a2ffce37a");  

  15. // Checks if the web scoped feature is active bool isFeatureActive = clientContext.Web.IsFeatureActive(featureId);  

  16. // Displays result

  17. if (isFeatureActive)          

  18. {              

  19. Console.WriteLine("Web Feature is Active");          

  20. }  

  21. else         

  22. {              

  23. Console.WriteLine("Web Feature is not active");           

  24. }          

  25. Console.ReadKey();      

  26. }  

  27. }  

  28. catch (Exception ex)  

  29. {      

  30. Console.WriteLine("Error Message: " + ex.Message);      

  31. Console.ReadKey();  

  32. }  

Activate Web Scoped Feature The web scoped features can be activated using PnP Core CSOM library. The steps involved are,

  • Get the web scoped feature ID from the list available. (https://blogs.msdn.microsoft.com/razi/2013/10/28/listing-all-sharepoint-server-2013-features-including-name-title-scope-id-and-description/)

  • Authenticate and get the client context of the site and then get the web object.

  • Activate the required feature using "ActivateFeature" method using web object with the feature id as parameter.

  • Output the required result on the console.

The following code snippet shows the code sample to activate web features.

  1. // Input Parameters

  2. string siteUrl = "https://nakkeerann.sharepoint.com/sites/learning";  

  3. string userName = "abc@nakkeerann.onmicrosoft.com";  

  4. string password = "***";  

  5. // PnP component to set context

  6. AuthenticationManager authManager = new AuthenticationManager();  

  7. try

  8. {  

  9. // Get and set the client context

  10. // Connects to SharePoint online site using inputs provided

  11. using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))      

  12. {  

  13. // Minimal download strategy Feature ID (Web scoped)         

  14. Guid featureId = new Guid("87294c72-f260-42f3-a41b-981a2ffce37a");  

  15. // Activates the above web scoped feature         

  16. clientContext.Web.ActivateFeature(featureId);          

  17. Console.WriteLine("Web Feature is activated");          

  18. Console.ReadKey();      

  19. }  

  20. }  

  21. catch (Exception ex)  

  22. {      

  23. Console.WriteLine("Error activating web feature");      

  24. Console.WriteLine("Error Message: " + ex.Message);      

  25. Console.ReadKey();  

  26. }  

Deactivate Web Scoped Feature The web scoped features can be deactivated using PnP Core CSOM library. The steps involved are,

  • Get the web scoped feature ID from the list available. (https://blogs.msdn.microsoft.com/razi/2013/10/28/listing-all-sharepoint-server-2013-features-including-name-title-scope-id-and-description/)

  • Authenticate and get the client context of the site and then get the web object.

  • Deactivate the required feature using "DeactivateFeature" method using web object with feature id as parameter.

  • Output the required result on the console.

The following code snippet shows the code sample to deactivate web features.

  1. // Input Parameters

  2. string siteUrl = "https://nakkeerann.sharepoint.com/sites/learning";  

  3. string userName = "abc@nakkeerann.onmicrosoft.com";  

  4. string password = "***";  

  5. // PnP component to set context

  6. AuthenticationManager authManager = new AuthenticationManager();  

  7. try

  8. {  

  9. // Get and set the client context

  10. // Connects to SharePoint online site using inputs provided

  11. using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))      

  12. {  

  13. // Minimal download strategy Feature ID (Web scoped)         

  14. Guid featureId = new Guid("87294c72-f260-42f3-a41b-981a2ffce37a");  

  15. // Deactivates the web feature         

  16. clientContext.Web.DeactivateFeature(featureId);          

  17. Console.WriteLine("Web Feature is deactivated");          

  18. Console.ReadKey();      

  19. }  

  20. }  

  21. catch (Exception ex)  

  22. {      

  23. Console.WriteLine("Error in deactivating web feature");      

  24. Console.WriteLine("Error Message: " + ex.Message);      

  25. Console.ReadKey();  

  26. }  

Note - To test the code, press F5 and wait for the Console. Summary Thus, you have learned how to check the active web scoped features and activate or deactivate web scoped features on the SharePoint online site or sub sites. The main advantage of using the PnP Core CSOM libraries is reduced code complexity.