Site Collection Features Operations In SharePoint 2016 Using REST API

SharePoint 2016 general availability had been announced in the Future Of SharePoint conference in May 2016. The series that discusses the installation of SharePoint 2016 in Azure can be found at C# Corner from the below links.

In this article, we will see how to interact with site collection features in SharePoint 2016, using REST API. The scope of the article is,

  • Fetch all the active site collection features
  • Activate "Publishing" feature
  • Deactivate "Publishing" feature.

Fetch Active Site Collection Features

The features that are active in a site collection can be retrieved by issuing an AJAX GET request, using the below REST URL.

/_api/site/features?$select=DisplayName,DefinitionId

In headers attribute, we specify the type of return data expected, which is JSON here.The result returned will be iterated in the success call back function.The header of the AJAX GET request will be plain and simple, as shown below.
  1. headers: {  
  2.     "accept""application/json;odata=verbose",  
  3.     "X-RequestDigest": $("#__REQUESTDIGEST").val()  
  4. }  
Output - The site collection features will be displayed along with their name and definition id.



Full Code
  1. <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>  
  2. <script language="javascript" type="text/javascript">  
  3.     $(document).ready(function() {  
  4.         //Make sure to append $select . Else only DefinitionId will be retrieved   
  5.         var newSiteFeatureUrl = "/_api/site/features?$select=DisplayName,DefinitionId";  
  6.         activateSiteFeature(newSiteFeatureUrl)  
  7.     });  
  8.   
  9.     function activateSiteFeature(newSiteFeatureUrl) {  
  10.         $.ajax({  
  11.             url: _spPageContextInfo.webAbsoluteUrl + newSiteFeatureUrl,  
  12.             type: "GET",  
  13.             headers: {  
  14.                 "accept""application/json;odata=verbose",  
  15.                 "X-RequestDigest": $("#__REQUESTDIGEST").val()  
  16.             },  
  17.             success: function(data) {  
  18.                 var items = data.d.results;  
  19.                 console.log("The active site collection features are :");  
  20.                 for (var i = 0; i < items.length; i++) {  
  21.                     console.log(items[i].DisplayName + ":" + items[i].DefinitionId);  
  22.                 }  
  23.             },  
  24.             error: function(error) {  
  25.                 alert(JSON.stringify(error));  
  26.             }  
  27.         });  
  28.     }  
  29. </script>  
Activate a particular Site Collection Feature

In order to activate a site collection feature, we can issue an AJAX POST request, using the REST URL.

"/_api/site/features/add('f6924d36-2fa8-4f0b-b16d-06b7250180fa')"

'f6924d36-2fa8-4f0b-b16d-06b7250180fa’ is the feature id for the site collection feature: "SharePoint Server Publishing Infrastructure". It has to be activated to use the SharePoint Publishing features.

The header section will look like the following.
  1. headers: {  
  2.     "accept""application/json;odata=verbose",  
  3.     "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
  4. },  
Here, accept attribute specifies the data type for the return value. In POST request, we have to send the X-RequestDigest value along with the request for form validation without which we will get a validation error. To fulfill this, we will be assigning the $("#__REQUESTDIGEST").val() which sets the value of the form digest control present within the page to X-RequestDigest key.

Output - Thus, the site collection "Publishing" feature has been enabled, as shown below.



Full Code
  1. <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>  
  2. <script language="javascript" type="text/javascript">  
  3.     $(document).ready(function() {  
  4.         var newSiteFeatureUrl = "/_api/site/features/add('f6924d36-2fa8-4f0b-b16d-06b7250180fa')";  
  5.         activateSiteFeature(newSiteFeatureUrl)  
  6.     });  
  7.   
  8.     function activateSiteFeature(newSiteFeatureUrl) {  
  9.         $.ajax({  
  10.             url: _spPageContextInfo.webAbsoluteUrl + newSiteFeatureUrl,  
  11.             type: "POST",  
  12.             headers: {  
  13.                 "accept""application/json;odata=verbose",  
  14.                 "X-RequestDigest": $("#__REQUESTDIGEST").val()  
  15.             },  
  16.             success: function(data) {  
  17.                 console.log(data);  
  18.             },  
  19.             error: function(error) {  
  20.                 alert(JSON.stringify(error));  
  21.             }  
  22.         });  
  23.     }  
  24. </script>  
Deactivate a Site Collection Feature

In order to deactivate an already activated site collection feature, we can issue an AJAX POST request, using the REST URL - /_api/site/features/remove('f6924d36-2fa8-4f0b-b16d-06b7250180fa') 
 
'f6924d36-2fa8-4f0b-b16d-06b7250180fa’ is the feature id for SharePoint Server Publishing Infrastructure which we have activated earlier. We will deactivate the same feature, now.

The header section will look like the following.
  1. headers: {   
  2. "accept""application/json;odata=verbose",   
  3. "X-RequestDigest": $("#__REQUESTDIGEST").val(),   
  4. },  
Full Code
  1. <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>  
  2. <script language="javascript" type="text/javascript">  
  3.     $(document).ready(function() {  
  4.         var newSiteFeatureUrl = "/_api/site/features/remove('f6924d36-2fa8-4f0b-b16d-06b7250180fa')";  
  5.         deActivateSiteFeature(newSiteFeatureUrl)  
  6.     });  
  7.   
  8.     function deActivateSiteFeature(newSiteFeatureUrl) {  
  9.         $.ajax({  
  10.             url: _spPageContextInfo.webAbsoluteUrl + newSiteFeatureUrl,  
  11.             type: "POST",  
  12.             headers: {  
  13.                 "accept""application/json;odata=verbose",  
  14.                 "X-RequestDigest": $("#__REQUESTDIGEST").val()  
  15.             },  
  16.             success: function(data) {  
  17.                 console.log(data);  
  18.             },  
  19.             error: function(error) {  
  20.                 alert(JSON.stringify(error));  
  21.             }  
  22.         });  
  23.     }  
  24. </script>  
Let’s see how we can implement it in SharePoint. Save the script as a text file and upload it to the site assets library. 
 
SharePoint Implementation - 
  • Go to the edit settings of the SharePoint page and click on Web part from the Insert tab.



  • Add Content Editor Web part.



  • Click on Edit Web part from Content Edit Web part. Assign the URL of the script text file and click on Apply.


Output - The SharePoint Server Publishing Infrastructure feature has now been deactivated.



Summary - Thus, we have seen how to fetch the details of all the activated features, activate and deactivate site collection features in SharePoint 2016 using REST API.

Up Next
    Ebook Download
    View all
    Learn
    View all