Click here to Skip to main content
15,885,309 members
Articles / Operating Systems / Windows
Technical Blog

SharePoint: Can’t Activate Site Collection Feature When Creating New Site From a Custom Web Template

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
4 Dec 2013CPOL1 min read 11.4K  
In SharePoint, can’t activate Site Collection Feature when creating new site from a Custom Web Template

Introduction

The onet.xml file is basically divided into two parts, first is the “SiteFeatures” element and the second element is 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 got 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, you need that all the needed features 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:

A) Create an empty web scoped feature and in the “FeatureActivated” Event Receiver, add the following code:

C#
public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {
                //Ensure that scope is correctly set
                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);
                        //Verify feature status
                        SPFeature feature = web.Site.Features[featureGuid];
                        if (feature == null)
                        {
                            //Activate site collection scoped feature, 
                            //if requested and not currently activated
                            web.Site.Features.Add(featureGuid);
                        }
                    }
                }
            }
            catch (Exception ex)
            {}
        }

B) In the onet.xml file in the “WebFeature” element, add the following XML:

XML
<WebFeatures>
      <!-- Custom Site collection scoped feature activation -->
       <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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) E4D
Israel Israel
Alex Choroshin is working as a Consultant/Web Developer at LogoUi company , he has a vast experience developing client side solutions and single page application using the latest web technologies: HTML5, CSS3 , AngularJS, JQuery, SignalR, ASP.NET MVC4, Web API, NodeJS etc.

Also experience with the SharePoint 2010 & SharePoint 2013 platform encompassing all the aspects of SharePoint architecture and development.

Comments and Discussions

 
-- There are no messages in this forum --