1

Can we activate a feature by adding two features to the solution and activating the one of the feature from another features FeatureInstalled Event?

I am trying to activate the feature of the current solution when the solution is deployed?

And all I am asking is about a Site Collection Feature.

2

I'm almost 100% sure it's possible, but something tells me there's a better way of doing this depending on your use case and I think it's called Feature Stapling. I think of it as a declarative way to attach features to site definitions (yea, not really sticking it to other features but rather to site definitions, so that when a site gets created with said (or any) site definition, the feature is automatically activated).

If you'd rather only activate your Feature B when Feature A gets activated, I'd go ahead and just do it from Code.

if(feature==null)//if feature is not activated
{
   site.Features.Add(featureId);//activate feature
}

and the reverse:

if(feature!=null)//if feature is activated
{
    site.Features.Remove(feature.DefinitionId);//deactivate feature
}

EDIT: For Feature Stapling, keep this in mind:

The scope of the feature that does the stapling must be higher than the features being stapled. For example, a feature at the site collection scope can staple only features at site-level scope. So, you can fine tune the your feature scope accordingly

A nice feature stapling walkthrough

|improve this answer|||||
  • More on feature stapling: c-sharpcorner.com/UploadFile/anavijai/… – variable Mar 5 '14 at 8:39
  • Can it happen if both the features are site collection features of the same solution on the same site? – Niranjan Kulkarni Mar 5 '14 at 13:56
  • I don't see a reason it wouldn't work. Only thing to make sure is that dependencies are respected between features (activate the dependency first). Check my answer's edit for more info. – Cameron Verhelst Mar 6 '14 at 7:20
  • Its working it just happened by feature stapling, I activated site scoped feature by using feature stapling. – Niranjan Kulkarni Mar 7 '14 at 7:03
  • Congrats :), would you mind marking it as the answer then ? – Cameron Verhelst Mar 7 '14 at 9:13
1

Automatically activating another feature inside the FeatureActivated handler for some initial feature works fine for Farm solutions. For Sandbox solutions it's a no go though - if you need that you may have to do it using JavaScript (it's not that hard).

|improve this answer|||||
  • 1
    Can you please add details regarding how to activate a feature through javascript? :) – Robert Lindgren Mar 5 '14 at 8:19
  • Is it possible if both the features are site collection features? – Niranjan Kulkarni Mar 5 '14 at 13:59
  • Robert, in elements.xml you can put <CustomAction Id="jqueryScript" Location="ScriptLink" ScriptBlock=" function activateFeature() { var clientContext = new SP.ClientContext.get_current(); var web = clientContext.get_site().get_rootWeb(); var guid = new SP.Guid('{insert feature guid}'); web.get_features().add(guid, false, SP.FeatureDefinitionScope.site); clientContext.executeQueryAsync(); } ExecuteOrDelayUntilScriptLoaded(activateFeature, 'sp.js'); </CustomAction> – Dylan Nicholson Mar 5 '14 at 21:26
  • Niranjan, haven't tried but don't see why not. Have you tried just putting (properties.Feature.Parent as SPSite).Features.Add(FeatureGuid); in your feature's FeatureActivated event? – Dylan Nicholson Mar 5 '14 at 21:32
  • Dylan, Ya I have tried that but that doesn't work. What I want to ask you is, I searched on Google and I found a solution that is feature stapling but that is given for web and webapplication level features. And I tried it by doing on site level it's not happening. Giving some error while deploying the solution. Do you know some thing about feature stapling? Does work for two features at site collection level? – Niranjan Kulkarni Mar 6 '14 at 5:30
0

This will depend on the order in which features are installed. If the feature you want to activate programmatically in other feature's installed event is already installed then it can be activated otherwise not.

|improve this answer|||||
  • Can it happen if both the features are site collection features of the same solution because for me it is giving an error, by trying in that way? – Niranjan Kulkarni Mar 5 '14 at 13:58
  • Its working, by using feature stapling it is working. – Niranjan Kulkarni Mar 7 '14 at 7:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.