Activate SharePoint site features using CSOM and javascript

In order to activate a feature via the client side we simply have to know the GUID of the feature.

If this is a site scoped feature this is pretty straight forward.

The following can be run from a script editor webpart;

var featureCollection;
var oneFeature;
var site;
var guid
function findFeatureId() {
var clientContext = new SP.ClientContext();
site = clientContext.get_web();
featureCollection = site.get_features();
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
function onQuerySucceeded() {
var listEnumerator = featureCollection.getEnumerator();
var featureInfo = '';
while (listEnumerator.moveNext()) {
oneFeature = listEnumerator.get_current();
featureInfo += 'Feature ID: ' + oneFeature.get_definitionId() + '\n';
guid = oneFeature.get_definitionId();
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());

We can then use the GUID found with the next function

function activateFeature(rawGuid){
//get contextr of newly created site
var clientContext = new SP.ClientContext($site_url);
web = clientContext.get_web();
//Activate Features
function ActivateWebFeature(web){
//trinedy branding feature GUID
var guid = new SP.Guid('{'+rawGuid+'}');
var featDefinition = web.get_features().add(guid, true,;

This is especially useful when running with a function that manages site creation.