Working With SharePoint Web Scoped Site Features Using REST API

Introduction

In this article, you will learn how to retrieve, enable or disable the site features (Web Scoped features) programmatically using REST API on SharePoint 2013 / SharePoint online. Here, we will see in detail about the usage of REST API to complete these operations. The web scoped features are present on this page.

Steps Involved

  • Identify the site or sub site URL and build the REST API URL.

  • URL is built in the below sections, web denotes the sites or sub sites and sets the necessary parameters to filter out the data. You need to provide select parameters to get the necessary feature properties in get request.

  • Use Ajax jQuery call to accomplish the necessary operation.

  • You can set the return value type in the Ajax call request. In my case, I have specified JSON to be the return value in the data parameter.

  • In these cases, you can check the result values in a browser debugger console. You can change the logic to display results on pages.

  • Place these scripts on the page using the web parts (content editor / script editor / any custom web part).
Get Active Site Features and Properties (Web scope)
 
The operation will fetch all the active site features (web scoped) present on the site or sub site.
  • In the URL, select parameters has necessary properties to be retrieved.

  • Using <for> each loop, get and display the results.
    1. // Retrieves all the site features enabled (web scoped features)  
    2. function GetSiteFeatures(){  
    3.     //  Here site URL can be root level or sub sites. But the web scoped features will be retrieved  
    4.     var featuresURI = siteURL + "/_api/web/features?$select=DisplayName,DefinitionId";  
    5.     $.ajax({  
    6.         url: featuresURI,  
    7.         method: 'GET',  
    8.         headers: { "Accept""application/json; odata=verbose" },  
    9.         dataType: "json",  
    10.         success: function (data) {  
    11.             var siteFeatures = data.d.results;  
    12.             for(var i=0;i<siteFeatures.length;i++){  
    13.                 console.log(siteFeatures[i].DefinitionId + " - " + siteFeatures[i].DisplayName);  
    14.             }  
    15.         },  
    16.         error: function(data) {  
    17.         }  
    18.     });  
    19. }  

Get/Check Site Feature Properties/Status (Web scope)

Here, we will see how we can get the particular feature and check whether the site feature (Web Scoped) is active or not. You can identify whether the feature is activated at Web Scope or not.

  • Get the feature ID from Microsoft site. The scope of the feature should be Web.

  • Get the feature using GetById method with feature ID in the REST API.
    1. // Retrieves all the site features enabled (web scoped features)  
    2. function GetSiteFeatureByID(){  
    3.     var featuresURI = siteURL + "/_api/web/features/GetByID('d95c97f3-e528-4da2-ae9f-32b3535fbb59')?$select=DisplayName,DefinitionId";  
    4.     $.ajax({  
    5.         url: featuresURI,  
    6.         method: 'GET',  
    7.         headers: { "Accept""application/json; odata=verbose" },  
    8.         dataType: "json",  
    9.         success: function (data) {  
    10.             if(data.d.DefinitionId != null){  
    11.                 console.log(data.d.DefinitionId + " - " + data.d.DisplayName + " feature is active");  
    12.             }  
    13.             else{  
    14.                 console.log("Site Feature is not active");  
    15.             }  
    16.         },  
    17.         error: function(data) {  
    18.         }  
    19.     });  
    20. }  

Activate the site feature (Web scope)

In the above section, we saw how we can check the status of the site feature (Web Scoped). Here, we will see how we can activate the feature which is already installed on the site.
  • To activate the site feature, use the ADD method to the features collection. The necessary parameter to be passed in the query is feature ID.

  • POST method will be used to accomplish the task.

  • RequestDigest will be passed in the header to validate the page requests.
    1. // Enables site feature (web scoped feature)  
    2. function EnableSiteFeature(){  
    3.     var featuresURI = siteURL + "/_api/web/features/add('d95c97f3-e528-4da2-ae9f-32b3535fbb59')"// Mobile Browser View Feature ID  
    4.     $.ajax({  
    5.         url: featuresURI,  
    6.         method: 'POST',  
    7.         headers: {   
    8.             "Accept""application/json;odata=verbose",  
    9.             "X-RequestDigest": $("#__REQUESTDIGEST").val()   
    10.         },  
    11.         success: function (data) {  
    12.             console.log("Feature Activated");  
    13.         },  
    14.         error: function(data) {  
    15.             console.log(data.responseJSON.error.message.value);  
    16.         }  
    17.     });  
    18. }  

Deactivate the site feature (Web scope)

In the above section, we have seen how we can activate the site feature (Web Scoped). Here, we will see how we can deactivate the feature.
  • To deactivate the site feature, use the Remove method on URL. The necessary parameters will be passed in the query is feature ID.

  • POST method will be used to accomplish the task.

  • RequestDigest will be passed in the header to validate the page requests.
    1. // Disables site feature (web scoped feature)  
    2. function DisableSiteFeature(){  
    3.     var featuresURI = siteURL + "/_api/web/features/remove('d95c97f3-e528-4da2-ae9f-32b3535fbb59')"// Mobile Browser View Feature ID  
    4.     $.ajax({  
    5.         url: featuresURI,  
    6.         method: 'POST',  
    7.         headers: {   
    8.             "Accept""application/json;odata=verbose",  
    9.             "X-RequestDigest": $("#__REQUESTDIGEST").val()   
    10.         },  
    11.         success: function (data) {  
    12.             console.log("Site Feature De-Activated");  
    13.         },  
    14.         error: function(data) {  
    15.             console.log(data.responseJSON.error.message.value);  
    16.         }  
    17.     });  
    18. }  

Note - The above set of operations needs to be called in order to execute it.

  1. $(document).ready(function(){  
  2.     GetSiteFeatures(); // Retrieves all the site features enabled (web scoped features)  
  3.     GetSiteFeatureByID(); // Retrieves all the site features enabled (web scoped features)  
  4.     EnableSiteFeature(); // Enables site feature (web scoped feature)  
  5.     DisableSiteFeature(); // Disables site feature (web scoped feature)  
  6. });   
Summary

You learned how to retrieve, activate, or de-activate the site features (Web Scoped) using REST API on SharePoint 2013 / SharePoint online sites.