Feature Activation Dependency with Hidden Features !

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"
    4:     Version="1.0.0.0"
    5:     Scope="Site"
    6:     Hidden="FALSE"
    7:     xmlns="https://schemas.microsoft.com/sharepoint/"> 
    8: </Feature>

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"
    4:    Version="1.0.0.0"
    5:    Scope="Site"
    6:    Hidden="TRUE"
    7:    xmlns="https://schemas.microsoft.com/sharepoint/">
    8:         <ActivationDependencies>
    9:               <ActivationDependency FeatureId="74EE1A2F-2E2D-4a52-A674-BB16575234C7" />
   10:         </ActivationDependencies>
   11:       </Feature> 

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.)

Office SharePoint Server Publishing

Create a Web page library as well as supporting libraries to create and publish pages based on page layouts.

    

           One or more features must be turned on before this feature can be activated.

 
      

Office SharePoint Server Publishing Infrastructure

Provides centralized libraries, content types, master pages and page layouts and enables page scheduling and other publishing functionality for a site collection.

 
 

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;
    8:  
    9: namespace FeatureActivationDependency
   10: {
   11:     public class ActivationDependency : SPFeatureReceiver
   12:     {
   13:         public override void FeatureActivated(SPFeatureReceiverProperties properties)
   14:         {
   15:             
   16:             SPSite oSite = properties.Feature.Parent as SPSite;
   17:  
   18:             SPFarm oFarm = oSite.WebApplication.Farm;
   19:  
   20:             SPFeatureDefinitionCollection oFeatures = oFarm.FeatureDefinitions;
   21:             SPFeatureDefinition oFeature = oFeatures[new Guid("B92139A4-04D7-4d9d-9B61-2DF6E4A59D66")];
   22:  
   23:             if (oFeature == null)
   24:             {
   25:                 // install & activate the feature
   26:                 Process oProcess = new Process();
   27:                 ProcessStartInfo oProcInfo = new ProcessStartInfo();
   28:                 
   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;
   35:  
   36:                 // executing the stsadm command.
   37:                 oProcess.Start();                
   38:                 oProcess.WaitForExit();
   39:  
   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);
   44:  
   45:             }
   46:             else
   47:               // assume that feature already installed so we are activating this feature
   48:               oSite.Features.Add(oFeature.Id, true);            
   49:             
   50:             
   51:         }
   52:         public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
   53:         {
   54:            
   55:         }
   56:         public override void FeatureInstalled(SPFeatureReceiverProperties properties)
   57:         {
   58:            
   59:         }
   60:         public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
   61:         {
   62:             
   63:         }
   64:     }
   65: }