Handling Feature Dependencies


So a common scenario is you have to deploy a set of features to get a desired result. However Feature B is reliant on Feature A and you want them to auto-activate. However automatic activation in this scenario generates an error if Feature A hasn’t finished being activated yet.

So a simple workaround is to add a feature receiver (either in one of your core features or as a separate feature altogether that activates/deactivates everything for you). So here’s an example of how you would achieve this 🙂 enjoy!

  1. using Microsoft.SharePoint;
  2. using Microsoft.SharePoint.Administration;
  3. class FeatureReceiver : SPFeatureReceiver
  4. {
  5. public override void FeatureActivating(SPFeatureReceiverProperties properties)
  6. {
  7. SPWeb web = (SPWeb)properties.Feature.Parent;
  8. //active features in current spsite
  9. SPFeatureCollection _features = web.Site.Features;
  10. bool featureActive = false;
  11. //my custom feature definition
  12. SPFeatureDefinition myFeatureDef = web.Site.FeatureDefinitions[“FeatureName”];
  13. foreach (SPFeature feature in _features)
  14. {
  15. if (feature.Definition == myFeatureDef)
  16. {
  17. //found feature as activated already
  18. featureActive = true;
  19. }
  20. }
  21. //feature is present but not activated
  22. if (!featureActive)
  23. {
  24. web.Site.Features.Add(myFeatureDef.Id, true);
  25. }
  26. }
  27. public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
  28. {
  29. SPWeb web = (SPWeb)properties.Feature.Parent;
  30. //get active features from current spsite
  31. SPFeatureCollection _features = web.Site.Features;
  32. bool featureActive = false;
  33. //get specific definition for my custom feature
  34. SPFeatureDefinition myFeatureDef = web.Site.FeatureDefinitions[“FeatureName”];
  35. foreach (SPFeature feature in _features)
  36. {
  37. if (feature.Definition == myFeatureDef)
  38. {
  39. //feature found in activate feature collection
  40. featureActive = true;
  41. }
  42. }
  43. //if we found the feature active
  44. if (featureActive)
  45. {
  46. //feature guid, force remove true/false
  47. web.Site.Features.Remove(myFeatureDef.Id, true);
  48. }
  49. }
  50. }
  1. #1 by Joe on May 18, 2011 - 5:36 pm

    public override void FeatureActivating results in an error ‘myapp.Features.Utils.UtilsEventReceiver.FeatureActivating Microsoft.SharePoint.SPFeatureReceiverProperties)’: no suitable method found to override. 🙁 Am I missing a key element?

  2. #2 by Maarten on May 18, 2011 - 11:18 pm

    In your class it should read as:

    class UtilsEventReceiver : SPFeatureReceiver
    {
    public override void FeatureActivating(SPFeatureReceiverProperties properties)
    {
    //do stuff
    }
    public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
    {
    //do stuff
    }
    }

  3. #3 by Joe on May 19, 2011 - 7:43 pm

    I am thinking my issue is version. I am compiling against a v12 SP dll on a v14box. When I make a reciever I get the following
    public override void FeatureActivated; FeatureDeactivating;FeatureInstalled;FeatureUninstalling as well as a FeatureUpgrading

    is FeatureActivating 2007 or 2010 😉

    • #4 by Maarten on May 19, 2011 - 7:53 pm

      Activating is 2010, for v12 the equivalent is FeatureActivated, they didn’t have the async Activating event in 2007.

  4. #5 by Joe on June 3, 2011 - 9:56 am

    thanks for the clarification.

(will not be published)


%d bloggers like this: