Creating Dependent Features in SharePoint

In a recent project, had to create a feature which provisions bunch of Lists, Libraries and Pages with custom web parts. Those custom web parts also deployed as feature based solutions. So the requirement is: Activate the features which brings the custom web parts first, and then activate the feature which creates the pages with custom web parts.

Problem: If the user tries to activate the Main feature- which creates web part pages with custom web parts, with out activating a feature which brings the custom web part: Boom - Error!

Here comes the solution: Feature Activation dependency! How to Implement? Simple! just include the  <ActivationDependencies> in Feature.xml

<?xml version="1.0" encoding="utf-8"?>
<Feature  Id="2cec1d22-290f-41b2-a14b-164a82d43b04"
          Title="Crescent Project Site Template"
          Description="Feature for Crescent project site Template"
          Scope="Web" >

<!--Set the activation dependency for Custom web parts feature -->
    <ActivationDependency FeatureId="GUID-OF-THE-DEPENDENT-FEATURE"/>
    <ElementManifest Location="elements.xml"/>

Important: If a dependent feature is Hidden, then it will be activated automatically when we activate the parent feature. If not, SharePoint will prompt the "Required Feature" page, instructing to activate the dependent feature first. We can't have <ActivationDependencies> In a Parent feature which has Hidden=TRUE attribute.
Dependent Features in SharePoint
Another frequent use of Feature Activation Dependency is: Page layouts!

Say, We've one Feature for Page Layouts and another for Custom content types. Custom Content types are the base for Page layouts (In another words, Page layout fields are from Custom content type). So we need Feature Activation Dependency here as well.

Further MSDN Readings:

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.