Feature Stapling : Associating a feature with a site template

In this post I will be demonstrating how to attach a custom feature with a site template.
Such a requirement arises when one wants to customize the out of box templates. Modifying the 12 hive folder is one option but isn’t recommended as it will render the site unsupported in case of an upgrade.
There are two steps involved
Step 1
We will first create a sample feature, which will be activated every time a site is created with a specified template.
Create a class library project and inherit the “SPFeatureReceiver” class to override FeatureActivated() method.
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Navigation;
namespace SampleFeature
{
class SampleFeature : SPFeatureReceiver
{
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
try
{
SPSite spSite = properties.Feature.Parent as SPSite;
SPWeb web = spSite.RootWeb;
{
//////Any Operation
} } } } }

Now we can change master page, create document library, add a link in quick launch, manage permissions, change search settings or whatever may be required as per situation, like for example if we need a new doc library
web.Lists.Add("My Expense Receipts", "My Expense Receipts Repository", SPListTemplateType.DocumentLibrary);
SPDocumentLibrary spdMyExpense = (SPDocumentLibrary)myweb.Lists["My Expense Receipts"];
spdMyExpense.NoCrawl = true;
spdMyExpense.OnQuickLaunch = true;
spdMyExpense.Update();

instead of //////Any Operation
Or we can add a link in quick launch as
SPNavigationNodeCollection nodes = myweb.Navigation.QuickLaunch;
SPNavigationNode navNode = new SPNavigationNode("Custom Documents", "http://www.google.com", true);
foreach (SPNavigationNode node in nodes)
{
if (node.Title.Equals("Documents"))
{
node.Children.AddAsLast(navNode);
}
}
instead of //////Any Operation
Simply strong name the assembly and create a feature.xml file with following entries
'<'Feature xmlns="http://schemas.microsoft.com/sharepoint/" Id="2CE21EFC-BC43-4a42-9803-A5FA269BADCE" Title= "SampleFeature" ReceiverAssembly="SampleFeature, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ac6e3aaa3fb73cae" ReceiverClass="SampleFeature.SampleFeature" Version="1.0.0.0" Scope="Site"'>'
'<'/Feature'>'

(* replace '<' with <)

Add the assemble to GAC and create a folder called “samplefeature” in 12 hive folder




Place the feature.xml here and install with following command at command prompt
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN>stsadm -o installfeature -name SampleFeature

Step 2
After creating and installing a feature, we simply need to associate a template with this feature, for that purpose we will again create a feature but with scope as FARM (This is called a stapler feature)
1. In 12 hive folder, create a new folder as MyStapler (any name of your choice)
Create a Feature.XML file as
'<'Feature xmlns="http://schemas.microsoft.com/sharepoint/" Id="03F29027-BA4D-4f8a-A084-7972F181E7EE" Title="My Feature Stapler" Scope="Farm"'>'
'<'ElementManifests'>'
'
'<'/ElementManifests'>'
'<'/Feature'>'
(* replace '<' with <)

2. Similarly create a elemet.xml file with following content
'<'Elements xmlns="http://schemas.microsoft.com/sharepoint/" '>'
'<'FeatureSiteTemplateAssociation Id="2CE21EFC-BC43-4a42-9803-A5FA269BADCE" TemplateName="SPSPERS#0"/'>'
'<'/Elements'>'

(* replace '<' with <)

Here, Id is id of the sample feature and TemplateName is the teamplet name(SPSPERS) and # and its id as mentioned in configuration tag of site definition.
In the end we simply need to install our newly created feature and we are good to go.
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN>stsadm -o installfeature -name MyStapler
Now, whenever a new site is created, the feature (Samplefeature in our case) will be activated and the code will do its job.

4 comments:

  1. hi Hitender,

    It is a great blog related to sharepoint.

    ReplyDelete
  2. I need to change master page in WSS3.0.Can i able to change by using feature whatever you suggest here. Write down the steps and code for my request to change master pages in WSS. Please

    ReplyDelete
  3. I need to change master page in WSS3.0.Can i able to change by using feature whatever you suggest here. Write down the steps and code for my request to change master pages in WSS. Please

    ReplyDelete
  4. hi Deepu,

    I guess the following code will solve your problem ,
    if (myweb.MasterUrl.Contains("default.master"))
    {
    myweb.MasterUrl = myweb.MasterUrl.Replace("default.master", "MySite.master");
    }
    myweb.Update();

    i hope it helps

    ReplyDelete