Monday, July 16, 2012

Activate Features on Site creation


Most of the times we need to activate certain features as soon as site gets created using site template. These features can be custom or OOB. also we have to manage the feature sequence in which we want to turn them on.

Following are 2 ways to implement:

  1. Using ONET.XML
  2. Using Feature stapling concept

1. Using ONET.XML: The global Onet.xml file defines list templates for hidden lists, list base types, a default definition configuration, and modules that apply globally to the deployment. Each Onet.xml file in a subdirectory of the %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\SiteTemplates directory can define navigational areas, list templates, document templates, configurations, modules, components, and server email footers that are used in the site definition to which it corresponds.

In an Onet.xml file, the Feature element is used within a site definition configuration to contain a reference to a Feature instance and default property values. The Configuration element specifies lists and modules to use when creating SharePoint sites. For information about the format and elements used in site definitions, see Site Schema.

SharePoint Foundation activates Features specified within the Onet.xml file in the order that they are listed. even you can specify Features that are depended upon before Features that depend upon them.

Following is the Key Elements which can be set to activate features:

<SiteFeatures>: Can be used to activate Site Scope features as soon as Site Created using the Site Template.
<WebFeatures>: Can be used to activate Web Scope features as soon as Site Created using the Site Template.
<Lists>: Can be used to create instance of List as soon as Site Created using the Site Template.

<Configurations>
  ...
  <Configuration
    ID="0"
    Name="Default">
    <Lists>
      <List
        FeatureId="00BFEA71-E717-4E80-AA17-D0C71B360101"
        Type="101"
        Title="$Resources:core,shareddocuments_Title;"
        Url="$Resources:core,shareddocuments_Folder;"
        QuickLaunchUrl="$Resources:core,shareddocuments_Folder;/Forms/AllItems.aspx" />
      ...
    </Lists>
    <Modules>
      <Module
        Name="Default" />
    </Modules>
    <SiteFeatures>
      <Feature
        ID="00BFEA71-1C5E-4A24-B310-BA51C3EB7A57" />
      <Feature
        ID="FDE5D850-671E-4143-950A-87B473922DC7" />
    </SiteFeatures>
    <WebFeatures>
      <Feature
        ID="00BFEA71-4EA5-48D4-A4AD-7EA5C011ABE5" />
      <Feature
        ID="F41CC668-37E5-4743-B4A8-74D1DB3FD8A4" />
    </WebFeatures>
  </Configuration>
  ...
</Configurations>

=================================================================================

2. Using Feature Stapling
: This is also knows as “Feature Site Template Association”. This is used to attach Feature(s) to all new instances of sites for a given given site definition without modifying the site definition or creating code routines to activate the Feature on each site.

Feature stapling is a concept that allows you to attach (or staple) a SharePoint Feature to a SharePoint site definition without modifying the original site definition files in any way

Following is an example of feature stapling that associates the Feature with only the “MySite” site definition templates:

Add Module:
StapleeHolder Feature

  • SharePoint Server Publishing Infrastructure (f6924d36-2fa8-4f0b-b16d-06b7250180fa) (Site Feature)
  • Team Collaboration Lists (00bfea71-4ea5-48d4-a4ad-7ea5c011abe5) (Web Feature)

Add/Update Elements.xml of StaplerHolder Module:

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
   <FeatureSiteTemplateAssociation Id="f6924d36-2fa8-4f0b-b16d-06b7250180fa" TemplateName="MySite#0" />
   <FeatureSiteTemplateAssociation Id="00bfea71-4ea5-48d4-a4ad-7ea5c011abe5" TemplateName="MySite#0" />
   <FeatureSiteTemplateAssociation Id="f6924d36-2fa8-4f0b-b16d-06b7250180fa" TemplateName="MySite#1" />
   <FeatureSiteTemplateAssociation Id="00bfea71-4ea5-48d4-a4ad-7ea5c011abe5" TemplateName="MySite#1" />
</Elements>

So In above example we have 2 Site Template of “MySite” Site Definition. As soon as the site is created from any of above template the “SharePoint Server Publishing Infrastructure” and “Team Collaboration List” feature will be activated.

Add Feature at WebApplication Level: This will activate staple feature at WebApplication level. Add above created module in it.

StapleeHolder Feature Web