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;
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var featureCollection; | |
var oneFeature; | |
var site; | |
var guid | |
function findFeatureId() { | |
var clientContext = new SP.ClientContext(); | |
site = clientContext.get_web(); | |
clientContext.load(site); | |
featureCollection = site.get_features(); | |
clientContext.load(featureCollection); | |
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(); | |
} | |
alert(featureInfo); | |
} | |
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function activateFeature(rawGuid){ | |
//get contextr of newly created site | |
var clientContext = new SP.ClientContext($site_url); | |
web = clientContext.get_web(); | |
//Activate Features | |
ActivateWebFeature(web); | |
function ActivateWebFeature(web){ | |
//trinedy branding feature GUID | |
var guid = new SP.Guid('{'+rawGuid+'}'); | |
var featDefinition = web.get_features().add(guid, true, SP.FeatureDefinitionScope.farm); | |
} | |
} |
This is especially useful when running with a function that manages site creation.