When to use SharePoint Feature Dependency
By: Shishir Bhandari | Updated: 2010-10-27 | Comments | Related: > SharePoint
This article deals with the Feature Dependency concept in SharePoint. The article assumes the reader knows how to create a feature. If you want to know more about creating a feature, refer to this msdn article.
In simple terms, Feature Dependency means a feature is dependent on another feature to work. In other words, unless the other feature is activated, the dependent feature won't work.
This article outlines when to use Feature Dependency, how to code Feature Dependency, and what the User Experience is activating this kind of feature.
What is a Feature Dependency?
Example 1: Suppose there are 5 features which deploy different lists to the web and all of these lists are using a lookup pointing to a master data list at the site collection level. The 5 lists won't work unless the master data list exists. So these 5 features are dependent on the feature which deploys the master data list and its lookup at the site collection level. If a user activates the list features first, the lists won't work since the lookup will throw an error. In order to avoid such a condition, we must use "SharePoint - Feature Dependency".
Example 2: A feature which deploys Custom Content Types is dependent on another feature which deploys custom site columns used in that custom content type. Unless the site columns are deployed, the content types won't work. To implement such dependency cases, "SharePoint - Feature Dependency" comes into picture.
When to Use Feature Dependency?
Scenario 1 :
(Similar to Example 1 in above section)
If you have to deploy various types of lists in a site, but you don't want to click multiple times for activating its features, you can assemble all the list features in one MAIN feature. When you activate this MAIN feature, it would activate all the list features by itself. Wouldn't that be nice?
Example Out of the box, the TeamCollab feature activation will cause activation of 19 other features deploying different list types. These list types along with their feature IDs are shown in the section below figure 1. SharePoint automatically activates all the features that are mentioned as a dependency in the TeamCollab feature.
Scenario 2 : (Similar to Example 2 in above section)
We have a feature called "Site Content types" which installs content types on activation and the "Site Columns" feature which installs site columns used in the Content type. If we activate the "Site Content types" feature first, it will cause issues since the relevant Site Columns haven't been installed yet.
In such cases, we need to create feature dependency between such features.
How to code Feature Dependency?
For Scenario 1, you need to define dependencies to all collaborative list type features in the TeamCollab feature itself.
Below is the feature.xml file for the TeamCollab feature. As shown below, create a tag <ActivationDependencies> where you will write all your feature dependencies. Each dependency will use the tag <ActivationDependency>. Specify the Feature ID / Guid in this tag. The list type name is mentioned in the comments next to the tag.
For Scenario 2, you need to define a dependency to the "Site Columns" feature in the "Site Content types" feature. Below is the feature.xml of the "Site Content types" feature.
What is the User Experience activating this kind of feature?
When a user activates the Site Content types feature, SharePoint will give a message that there is a dependent feature which needs to be activated and will show the dependent features (i.e. site columns feature). But we can hide this unnecessary message.
Just add Hidden = "true" in the feature.xml of all dependent features...all collaborative list types in scenario 1 and Site Columns feature in scenario 2.
Now the site columns feature (scenario 2) or collaborative list types features (scenario 1) will get automatically activated.
Things to Remember
- SharePoint takes care of all dependent features which are hidden
- Dependency chains are not supported. Dependencies can work only one level deep unless the dependent features are hidden.
- To read more on Feature Dependency Rules, refer to the msdn article
About the author
View all my tips
Article Last Updated: 2010-10-27