Wednesday, June 20, 2012

Automatically activate a web or site scoped feature during site creation

I know of two good techniques for automatically activating a web or site scoped feature when a new SharePoint site is created and they are (1) if you're using a custom site definition, to insert a new <Feature> node within either the <SiteFeatures> or <WebFeatures> node of the ONET.xml file and (2) utilize a technique called Feature Stapling to attach your feature to a given site definition.

In both instances, you will need to know the ID, Title, and Scope of you feature.  These can be found by opening your feature's Feature.xml file and locating the appropriate attributes of the <Feature> node.  Here is a simplified example of what you might find in a sample Feature.xml file:

<?xml version="1.0" encoding="utf-8"?>
<Feature xmlns="..." Title="My Feature" Description="..." Id="b68c860b-b368-4e5f-ae06-dcfbfbd9c8da" ReceiverAssembly="..." Scope="Web"></Feature>


ONET.XML Update
This technique should only be used if your site is based on a custom site definition that you or your team created (i.e. that's the case if you have a custom ONET.xml file in your solution).  NOTE: You change will likely be overwritten with a service pack installation. With that said, the basic premise of this technique is to insert a new <Feature> node within either the <SiteFeatures> or <WebFeatures> node of the ONET.xml file which can be accomplished as follows:

  1. Open your ONET.xml file using Visual Studio or Notepad
  2. Locate the <SiteFeatures> or <WebFeatures> node depending on the scope of the feature
  3. Create a new <Feature> node and add the appropriate ID and Name attributes
  4. Save the changes
In this example, I will use the details from my sample feature:

<Project>
   <Configurations>
      <Configuration>
         <SiteFeatures />
         <WebFeatures>
            <Feature ID="b68c860b-b368-4e5f-ae06-dcfbfbd9c8da" Name="My Feature" />
         </WebFeatures>
      </Configuration>
   </Configurations>
</Project>

Feature Stapling
This technique is the only approach that I know of for automatically activating a feature on a standard SharePoint site template.  In this case, you will be creating a new Farm level feature that, when activated, will "staple" your feature to all sites that reference the specified template.  Here is an example of the "stapling" feature code that will be contained in a new Feature.xml file:

<?xml version="1.0" encoding="utf-8" ?>
<Feature Id="7ac7f1e9-1c6a-4ff0-a2bc-0d81e2360b70"
    Title="Feature Staple Sample"
    Description="Staple the associated feature(s) to the specified site definition(s)"
    Version="1.0.0.0"
    Hidden="FALSE"
    Scope="Farm"
    xmlns="http://schemas.microsoft.com/sharepoint/">
   <ElementManifests>
      <ElementManifest Location="staple.xml" />
   </ElementManifests>
</Feature>

and here is some sample code that will be contained in the references staple.xml file that will automatically launch the specified feature on the standard SharePoint Team Site template:

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
   <FeatureSiteTemplateAssociation Id="b68c860b-b368-4e5f-ae06-dcfbfbd9c8da" TemplateName="STS#0" />
</Elements>

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.