The onet.xml file is basically divided into two parts, first is the "SiteFeatures
" element and the second element called "WebFeatures
". The "SiteFeatures
" section that holds the site features starts activating all the features only when creating a site collection. The "WebFeatures" Section that holds the web features starts activating all the web scoped features only when creating a site.
Scenario
You created a custom web template, deployed the solution and when trying to create a site from your custom web template you get the following error "the site template requires that the feature {GUID} be activated in the site collection". Of course, you can always activate the site collection scoped feature manually, but let's be serious, all the required features need be automatically activated.
Solution
When creating a site, you need to trigger the site collection scoped feature using a web scoped feature.
The steps are:
- Create an empty web scoped feature and in the "
FeatureActivated
" Event Receiver, add the following code:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
try
{
if (properties.Feature.Parent is SPWeb)
{
SPWeb web = (SPWeb)properties.Feature.Parent;
foreach (SPFeatureProperty property in properties.Feature.Properties)
{
Guid featureGuid = new Guid(property.Value);
SPFeature feature = web.Site.Features[featureGuid];
if (feature == null)
{
web.Site.Features.Add(featureGuid);
}
}
}
}
catch (Exception ex)
{}
}
- In the onet.xml file in the "
WebFeature
" Element, add the following XML:
<WebFeatures>
<Feature ID="YourEmptyFeatureGuid">
<Properties xmlns="http://schemas.microsoft.com/sharepoint/">
<Property Key="SiteScopedGUID" Value="YourSiteCollectionFeatureID"/>
</Properties>
</Feature>
</WebFeatures>
- In the Feature ID element, add your empty feature's ID.
- In the
Property Key="SiteScopedGUID"
element, add the site collection feature id that you want to activate.
Hope you’ll find this post helpful.