top of page
Writer's pictureNakkeeran Natarajan

Working With SharePoint Site Collection Features Using REST API

Updated: Mar 29, 2019

In this post, you will learn how retrieve, enable or disable the site collection features (site scoped) programmatically using REST API on SharePoint 2013 / SharePoint 2016 / SharePoint online. There are different ways that we use to work with site collection features. We will see how we can use REST APIs to achieve the functionalities.

  • Identify the site collection URL and build the REST API URL. 

  • In the URL, site denotes the site collection scope and set 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 return value in the data parameter.

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

  • Place these scripts on page using the web parts (content editor / script editor / any custom web part). 

Get Active Site Collection Features and Properties (Site scope):

The operation will fetch all the active site collection features present on the site.

  • In the URL, select parameters has necessary properties to be retrieved.

  • Using foreach loop, get and display the results.

// Retrieves all the site collection features enabled (site scoped features)

function GetSiteCollectionFeatures()

{  

var featuresURI = sitecollectionURL + "/_api/site/features?$select=DisplayName,DefinitionId";      

$.ajax(

{          

url: featuresURI,          

method: 'GET',          

headers: { "Accept": "application/json; odata=verbose" },          

dataType: "json",          

success: function (data) 

{  

var siteCollectionFeatures = data.d.results;  

for(var i=0;i<siteCollectionFeatures.length;i++)

{                 

console.log(siteCollectionFeatures[i].DefinitionId + " - " + siteCollectionFeatures[i].DisplayName);              }          },          

error: function(data) {          }      

});  }  

Get/Check Site Collection Feature Properties/Status (Site scope):

Here we will see how we can get the particular feature and check whether the site collection feature is active or not. You can identify whether the feature is activated at site scope or not.

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

  • Get the feature using GetById method with feature id in the REST API.

// Retrieves the site collection feature by id enabled (site scoped features) function GetSiteCollectionFeatureByID(){   var featuresURI = sitecollectionURL + "/_api/site/features/GetByID('151d22d9-95a8-4904-a0a3-22e4db85d1e0')?$select=DisplayName,DefinitionId";   

// Cross-Site Collection Publishing Feature ID     

$.ajax(

{          

url: featuresURI,          

method: 'GET',          

headers: { "Accept": "application/json; odata=verbose" },          

dataType: "json",          

success: function (data) 

{  

if(data.d != null)

{                  

console.log(data.d.DefinitionId + " - " + data.d.DisplayName + " feature is active");              

}  

else

{                  

console.log("Feature is not active");              

}          },          

error: function(data) {          }      

});  }  

Activate the site collection feature (Site scope):

In the above section, we have seen how we can check the status of the site collection feature. 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.

// Enables site collection feature (Site scoped feature)

function EnableSiteCollectionFeature()

{  

var featuresURI = sitecollectionURL + "/_api/site/features/add('151d22d9-95a8-4904-a0a3-22e4db85d1e0')"; 

// Cross-Site Collection Publishing Feature ID     

$.ajax({          

url: featuresURI,          

method: 'POST',          

headers: {   

"Accept": "application/json;odata=verbose",  

"X-RequestDigest": $("#__REQUESTDIGEST").val()           

},          

success: function (data) 

{              

console.log("Feature Activated");          

},          

error: function(data) 

{              

console.log(data.responseJSON.error.message.value);          }      

});  }  

Deactivate the site collection feature (Site scope):

In the above section, we have seen how we can activate the site collection feature. Here we will see how we can deactivate the feature.

  • To deactivate the site feature, use the remove method on URL. The necessary parameters 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.

// Disables site collection feature (Site scoped feature)

function DisableSiteCollectionFeature()

{  

var featuresURI = sitecollectionURL + "/_api/site/features/remove('151d22d9-95a8-4904-a0a3-22e4db85d1e0')"; 

// Cross-Site Collection Publishing Feature ID     

$.ajax({          

url: featuresURI,          

method: 'POST',          

headers: 

{   

"Accept": "application/json;odata=verbose",  

"X-RequestDigest": $("#__REQUESTDIGEST").val()           

},          

success: function (data) 

{              

console.log("Feature De-Activated");          

},          

error: function(data) 

{              

console.log(data.responseJSON.error.message.value);          

}      

});  }  


Note: The above set of operations needs to be called to get it executed.


$(document).ready(function()

{      

GetSiteCollectionFeatures(); 

// Retrieves all the site collection features enabled (site scoped features)     

GetSiteCollectionFeatureByID(); 

// Retrieves the site collection feature by id enabled (site scoped features)     

EnableSiteCollectionFeature(); 

// Enables site collection feature (Site scoped feature)     

DisableSiteCollectionFeature(); 

// Disables site collection feature (Site scoped feature)

});  

0 comments

Comments


bottom of page