Feature Stapling


WSS v3 supports the powerful concepts called Feature stapling. This helps the developer to staple a feature to the existing Out of the box Site Definition. Customizing the existing or out of the box site definition is not recommended by Microsoft because it is owned by Microsoft so at any time when new SP is released they might change the Site Definition. So one possible way is to copy the existing Site Definition and customize it for our application so that SP release will not update our Site Definition.

Feature stapling allows the developer to extend the existing Out of the box site definition. It can be activated globally for all Site Definitions or to a specific Site Definition.

Basically Feature stapling is a feature which activates a feature using an id to a specific template. In this blog i will create a simple Announcement Feature with 1 item to a web and associate it to the Team Site definition. Whenever a Site is created using the Team Site definition an Announcement Feature is activated. When site other than Team site is created the Announcement feature will not be activated.

For creating a feature i am using the VS 2008 extension for WSS 1.3. This allows easier to create feature xml and the element tags.

Creating an Announcement Feature

Open Visual Studio 2008, i assume that vse 1.3 is already installed in the machine. Click on File New Project and select SharePoint on the left and Select Empty Project on the right and enter the name Stapling.

image

Press Ok and the Security Window pops up select Full Trust and press Ok.

This will create an Empty project called Stapling in the VS 2008.

Now right click on the Project and select Add-> New Item, select List Instance on the right pane and enter the name MyAnnouncement and press Ok.

image

Select the Announcements in the Create List Instance.

image

This will create a folder with the MyAnnouncement name and the instance.xml to that folder as given below

image

Open the Instance.xml and add the below line of code to the xml to create a row when an announcement list is created

<Data>
      <Rows>
        <Row>
          <Field Name="Title">Welcome to Feature stapling site</Field>
          <Field Name="Body">If you can see this you can staple the feature </Field>
          <Field Name="Expires">&lt;ows:TodayISO/&gt;</Field>
        </Row>
      </Rows>
    </Data>

Open the WSP View to see the feature.xml file for the Announcement. To open the WSP View go to View Menu on the top of VS 2008 and select Other Windows and select WSP View. Click On refresh button image  to view the folder structure.

image

Open the Feature.xml to view the xml file. The xml file will have the feature GUID, title and the elementmanifest which will point to instance.xml. Note this GUID in my case it is 37a79999-0591-4b6a-bb97-7d14388ae7c0. This GUID will be used in the stapling feature to activate this announcement feature while creating a Site. Make sure the scope for the feature is set to Web. Because the Announcement instance can be created only in the web level. Now we have a basic simple Announcement Feature is ready. Next step is to create a Staple Feature to staple the Announcement Feature

Create a Staple Feature

Right click on the Project and Select Add->New Item. In the New Item window select Module. Enter a Name StaplingFeature.

image

This will create a folder Called StaplingFeature and a Module.xml file and the Sample.txt file. Delete the sample.txt file because we are not going to deploy any file.

Open the Module.xml file and remove the below mentioned tag

<Module Name="StaplingFeature">
    <File Path="sample.txt" Url="sample.txt" />
  </Module>

Add the below tag under the <Elements> tag.

<FeatureSiteTemplateAssociation Id="37a79999-0591-4b6a-bb97-7d14388ae7c0" TemplateName="STS#0"/>

The GUID mentioned in this tag is the Announcement feature GUID which we noted in the above steps. So after editing the Module.xml the file will look like below

<?xml version="1.0" encoding="utf-8"?>
<Elements Id="27d021a2-010c-4fd1-8c7e-6906ca2bf93f" xmlns="
http://schemas.microsoft.com/sharepoint/">
  <FeatureSiteTemplateAssociation Id="37a79999-0591-4b6a-bb97-7d14388ae7c0" TemplateName="STS#0"/>
</Elements>

Now open the WSP View and refresh. Now navigate to StaplingFeature->Feature.xml. Open the feature.xml and make sure the scope is in Farm. Finally the WSP View will look like below

image

Deploying the WSP to SharePoint

Deploying the solution is much easier in the VSE1.3. Open the Solution explorer view. Right click on the project and select properties. On the project properties select debug tab. In the Start Brower URL type the SiteCollection URL to be deployed

image

Click Save and close the window. Now Right Click on the Project and Select Deploy. This will build and deploy the wsp and activate the feature.

Testing the Feature Stapling

Now Open the SharePoint Site and create a Team Site using the Site Creation option from Site Settings. When a Team Site is created My Announcement Instance will be automatically created and will 1 item in it once the site is created.

If a definition other than Team Site Definition is used to create a site, the My Announcement will not be created but the feature will be present in the site under site features.

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s