A Feature activation dependency expresses a requirement in the relationship between two Features. You can express activation dependencies either for Features of the same scope or for Features across different scopes. Please check this link for getting more information about the Activation Dependency. More . Here we are going to talk about the Feature Activation Dependency with Hidden features.
A hidden feature cannot have any activation dependencies. For eg: Feature-A has an activation dependency with Feature-B, that means, only if we activate the Feature-B then only we can activate the Feature-A, thus if the Feature-B is hidden then we can’t activate it, as a result we can’t activate both features.
If you want to check this one quickly you can try the following feature definitions.
1. Create a feature with name Feature1 and then create a feature.xml with the following.
1: <Feature Id="74EE1A2F-2E2D-4a52-A674-BB16575234C7"
2: Title="testActivate one"
3: Description="Test one"
2. Create a feature with name Feature2 and then create a feature.xml with the following.
1: <Feature Id="B92139A4-04D7-4d9d-9B61-2DF6E4A59D66"
2: Title="testActivate two;"
3: Description="Test two"
9: <ActivationDependency FeatureId="74EE1A2F-2E2D-4a52-A674-BB16575234C7" />
This means, we can activate Feature1 only after activating the Feature2. Thus now we can see what will be outcome while installing these features using STSADM.EXE
1. STSADM.exe –o installfeature –name Feature1 –force
Operation completed successfully
2. STSADM.exe –o installfeature –name Feature2 –force
Hidden features with activation dependencies are not supported.
If you want to ensure quickly about the real usage of activation dependency in OOB, in the site feature you can try to activate the Office SharePoint Server Publishing. Actually, this feature is depending on the Office SharePoint server Publishing Infrastructure in the site collection feature. Thus, once you activate that site collection feature then only you can activate the site feature. In this scenario, think about if the site collection feature (Office SharePoint server publishing Infrastructure) is hidden, then we can’t activate that feature which results that we can’t activate the site feature (Office SharePoint server Publishing.)
Now we can think about a scenario in which we need to make a Feature dependent on hidden feature which is not yet installed in to the Farm. So, how we can activate that hidden feature without even installing it in the Farm? No need to think about too much we will get the features only if they are installed in the Farm. So that, we have to install it first and then only we can activate it.
Actually, this was one my customers requirement. So, what I have tried that whenever I install the feature 1 then hook an event handler which will capture that event and before completing that transaction install and activate the hidden feature 2 which is the dependent one. Unfortunately we don’t have a synchronous event (Feature Activating) event but, what I have seen that if any exception thrown in Feature Activated event it will not roll back and the feature will be still in inactive stage only. Thus once we activate any other feature at the time of feature activated event, first that feature 2 activation process will over and then it will complete the feature activation process of feature 1.
In my testing scenario, there were two features, Feature1 and Feature2. Feature1 is a hidden feature and Feature 2 is not a hidden feature. Our intention is whenever we try to activate the Feature2 then first we must need to activate the Feature1 and then only we can activate Feature2.
Then I decided to do the following:
1. Install the hidden Feature1
2. Install the Feature2
3. Register an FeatureActivated event handler for Feature2
4. Whenever we activate the feature, capture that event and then activate the hidden feature Feature1.
Here is the code snippet for the feature event handler.
1: using System;
2: using System.Collections.Generic;
3: using System.Text;
4: using Microsoft.SharePoint;
5: using Microsoft.SharePoint.Administration;
6: using Microsoft.SharePoint.Utilities;
7: using System.Diagnostics;
9: namespace FeatureActivationDependency
11: public class ActivationDependency : SPFeatureReceiver
13: public override void FeatureActivated(SPFeatureReceiverProperties properties)
16: SPSite oSite = properties.Feature.Parent as SPSite;
18: SPFarm oFarm = oSite.WebApplication.Farm;
20: SPFeatureDefinitionCollection oFeatures = oFarm.FeatureDefinitions;
21: SPFeatureDefinition oFeature = oFeatures[new Guid("B92139A4-04D7-4d9d-9B61-2DF6E4A59D66")];
23: if (oFeature == null)
25: // install & activate the feature
26: Process oProcess = new Process();
27: ProcessStartInfo oProcInfo = new ProcessStartInfo();
29: // here I am hardcoding the physical locatio nof the STSADM command.
30: oProcInfo.FileName = @"C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN\STSADM.EXE";
31: // Here Act2 is my hidden feature’s name | if you want to test this functionality then first you can install an unhidden feature then you
32: can see that the feature get installed and activated in the features list.
33: oProcInfo.Arguments = " -o installfeature -name Act2 -force";
34: oProcess.StartInfo = oProcInfo;
36: // executing the stsadm command.
40: // after installing the feature we are activating this feature
41: SPFeatureDefinitionCollection oAllFeatures = oFarm.FeatureDefinitions;
42: SPFeatureDefinition oHiddenFeature = oAllFeatures[new Guid("B92139A4-04D7-4d9d-9B61-2DF6E4A59D66")];
43: oSite.Features.Add(oHiddenFeature.Id, true);
47: // assume that feature already installed so we are activating this feature
48: oSite.Features.Add(oFeature.Id, true);
52: public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
56: public override void FeatureInstalled(SPFeatureReceiverProperties properties)
60: public override void FeatureUninstalling(SPFeatureReceiverProperties properties)