top of page

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

Introduction

In this article, you will learn all the basic site collection feature (site scoped) operations performed on SharePoint online site, using PnP Core Client Side Object Model library. The operations are compatible only for site collection features. The operations, stated below, explains that they will not be compatible for sub sites. 


Prerequisites

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

  • PnP Core CSOM packages can be downloaded 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 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 site collection / site scoped features can be accessed from the portal, using https://siteurl/_layouts/15/ManageFeatures.aspx?Scope=Site. The operations, given below, explains the site collection feature operations for SharePoint online site in detail. Check If Site Collection Feature is Active  The users can check whether the site collection feature is already activated on the site, using PnP Core CSOM library. The steps involved are-

  • Get the site collection 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.

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

  • Output the required result on the Console.

The code snippet, given below, shows the code sample to check the active features, given below- 

  1. // Input Parameters

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

  3. string userName = "nav@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. // Publishing Feature ID         

  14. Guid featureId = new Guid("f6924d36-2fa8-4f0b-b16d-06b7250180fa");  

  15. // Checks if the site collection feature is active

  16. bool isFeatureActive = clientContext.Site.IsFeatureActive(featureId);  

  17. // Displays result

  18. if (isFeatureActive)          

  19. {              

  20. Console.WriteLine("Feature is Active");          

  21. }  

  22. else         

  23. {              

  24. Console.WriteLine("Feature is not active");           

  25. }          

  26. Console.ReadKey();      

  27. }  

  28. }  

  29. catch (Exception ex)  

  30. {      

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

  32. Console.ReadKey();  

  33. }  

Activate Site Collection Feature The site collection features can be activated, using PnP Core CSOM library. The steps involved are- 

  • Get the site collection 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 site object.

  • Activate the required feature using ActivateFeature using site object with feature id as parameter.

  • Output the required result on the console.

The code snippet, given below, shows the code sample to activate the features.

  1. // Input Parameters

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

  3. string userName = "nav@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. // Publishing feature id         

  14. Guid featureId = new Guid("f6924d36-2fa8-4f0b-b16d-06b7250180fa");  

  15. // Activates the above site collection feature         

  16. clientContext.Site.ActivateFeature(featureId);         

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

  18. Console.ReadKey();      

  19. }  

  20. }  

  21. catch (Exception ex)  

  22. {      

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

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

  25. Console.ReadKey();  }  

Deactivate Site Collection Feature  The site collection features can be deactivated using PnP Core CSOM library. The steps involved are,

  • Get the site collection 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 site object.

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

  • Output the required result on the console.

The code snippet, given below, shows the code sample to deactivate the features.

  1. // Input Parameters

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

  3. string userName = "nav@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. // Publishing feature ID         

  14. Guid featureId = new Guid("f6924d36-2fa8-4f0b-b16d-06b7250180fa");  

  15. // Deactivates the site collection feature         

  16. clientContext.Site.DeactivateFeature(featureId);          

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

  18. Console.ReadKey();      

  19. }  

  20. }  

  21. catch (Exception ex)  

  22. {      

  23. Console.WriteLine("Error in deactivating 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 site collection/site scoped features, activate or deactivate site collection features on the SharePoint online site.

0 comments
bottom of page