Feature Stapling

Overview : Feature Stapling allows you to create a feature and then associate it with any site definition without ever touching the site definition files themselves. Your feature will be executed when the site is being provisioned.

Feature Stapling is achieved through the creation of another feature that defines the association of your regular Feature and the site definition you want to “staple” it too. In short, you need to create two Features to achieve a complete Feature Stapling implementation.

Steps to Create Feature Stapling :

(1) Open VS 2008
(2) Create New Class Library Project
(3) Add Reference : Microsoft.SharePoint in Reference tag from Solution Explorer
(4) Add Two Namespaces : using Microsoft.SharePoint; using Microsoft.SharePoint.Navigation;
(5) Inherit SPFeatureReceiver for Class
(6) Create stubs from Inherited SPFeatureReceiver

(7) In “FeatureActivated” method apply below code:
try
{
SPSite spSite = properties.Feature.Parent as SPSite;
SPWeb web = spSite.RootWeb;

web.Lists.Add(“Stappling List”, “Stappling list sample”, SPListTemplateType.DocumentLibrary);

SPDocumentLibrary spdStapplingList = (SPDocumentLibrary)web.Lists[“Stappling List”];
spdStapplingList.NoCrawl = true;
spdStapplingList.OnQuickLaunch = true;
spdStapplingList.Update();

}
catch (Exception Exc) { throw Exc; }

(8) Now Give StrongKey name to this class library from : Project –> Project Properties
(9) Click on Signing from left menu –> Select Sign the assembly check box –>Select New option from drop down –> Give Key Name –> Uncheck the checkbox for protection and click on OK button.
(10) Now build the project
(11) Put the Dll in GAC
(12) Now Create folder with the namespace name which you have used in your project under “C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\yourfoldername
(13) Create one “Feature.xml” file your class project.

(14) Apply below code in your Feature.xml file
<?xml version=”1.0″ encoding=”utf-8″ ?>
<Feature Id=”C99662D1-3B2A-42d7-8F12-1567C7D2043F”  Title=”Create Custom List Feature” Description=”This feature is used to create custom list” Version=”1.0.0.0″ Scope=”Site” ReceiverAssembly=”FeatureStp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5f9c89c76ede2694″
ReceiverClass=”FeatureStp.FeatureStp”
xmlns=”http://schemas.microsoft.com/sharepoint/”&gt;
</Feature>

(15) Now install this feature through stsadm command as below:
STSADM.EXE -o installfeature -filename yourfoldername\feature.xml -force

(16) After installing feature for your class library project create another folder in 12 hive to stapple the features (eg. MyFeatureStp)

(17) Now create Feature.xml file in this folder and apply below code
<?xml version=”1.0″ encoding=”utf-8″ ?>
<Feature xmlns=”http://schemas.microsoft.com/sharepoint/&#8221;
Id=”2F3A22DE-A10E-4193-89B9-094D15512DBD”
Title=”Custom List creator feature stapler”
Description=”This feature staples with create Custom list feature to create a custom list whenever a new site is created” Scope=”WebApplication” Hidden=”False” Version=”1.0.0.0″>
<ElementManifests>
<ElementManifest Location=”elements.xml” />
</ElementManifests>
</Feature>

(18) Now create one elements.xml file in same folder with below code
<?xml version=”1.0″ encoding=”utf-8″ ?>
<Elements xmlns=”http://schemas.microsoft.com/sharepoint/”&gt;
<FeatureSiteTemplateAssociation Id=”C99662D1-3B2A-42d7-8F12-1567C7D2043F” TemplateName=”STS#0″/>
<FeatureSiteTemplateAssociation Id=”C99662D1-3B2A-42d7-8F12-1567C7D2043F” TemplateName=”STS#1″ />
<FeatureSiteTemplateAssociation Id=”C99662D1-3B2A-42d7-8F12-1567C7D2043F” TemplateName=”STS#2″ />
</Elements>

(19) Now made changes for feature id. Which is written in step number 14.
You have to associate the feature id as per step no. 14.

(20) now to associate this …. we have to install the newly created feature with following command
stsadm -o installfeature -name MyFeatureStp\Feature.xml

All Done!!!!!!!!!!!

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s