/// <summary> /// Activates the feature. /// </summary> /// <param name="webUrl">The web URL.</param> /// <param name="featureId">The feature identifier.</param> /// <param name="forceActivationFlag">if set to <c>true</c> [force flag].</param> /// <param name="featureScope">The feature scope.</param> /// <returns></returns> public bool ActivateFeature(string webUrl, Guid featureId, bool forceActivationFlag, FeatureDefinitionScope featureScope) { var output = true; try { using (var clientContextToken = new ClientContext(webUrl)) { // retrieves a list of the features // and add activate the one with the specified ID var listOfFeatures = clientContextToken.Web.Features; clientContextToken.Load(listOfFeatures); clientContextToken.ExecuteQuery(); listOfFeatures.Add(featureId, forceActivationFlag, featureScope); clientContextToken.ExecuteQuery(); } } catch(Exception ex) { output = false; //If something goes wrong, you can catch it here and // display on a page, message, add to log etc. } return output; }
There is a Catch…
This only works for Site and Farm scopes; doesn’t work for Web scope. You can have a look at the FeatureCollection signature here and see that FeatureDefinitionScope does not have Web as an option for SharePoint 2010.
Update: There is a Web property in the FeatureDefinitionScope for SharePoint 2013
by Edge Pereira
In SharePoint 2013 there is a FeatureDefinitionScope.Web available. See the link: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.featuredefinitionscope(v=office.15).aspx
ReplyDeletethanks for the update, Jean. fixed! :)
ReplyDelete