Monthly Archives: August 2009

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.


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.


Select the Announcements in the Create List Instance.


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


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

          <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>

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.


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.


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" />

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

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


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


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.

Wrap UserControl into SharePoint WebPart


Creating a webpart now is easier with VS 2008. But the difficulty is, there is no designer for the webpart so every time we would know only when we deploy. Its time consuming and also difficult to visualize it at runtime. So the cool feature of webpart is we can directly load a user control to it. Although some of the third party tool ( allows to load the user control directly in the WebPart, i have mentioned a code based approach below to load the user control. Most the developers might be aware of it, may be my blog will help a novice sharepoint developer.

Below are the steps to be done to create a webpart which loads the user control. I am using the latest Visual Studio 2008 extensions for SharePoint v1.3.

Creating Webpart Project

Create a new project, select sharepoint option from the list and select WebPart project from the right pane. Enter HelloWorld for the project name


Select the full Trust option.


Rename the folder to HelloWorld, which will automatically rename all the webpart component. Only the latest VS extension will do the rename automatically.



Creating a ASP.NET User Control

Right click on the Helloworld solution and add New Project and Select ASP.NET Web Project and name it to HelloWorldControl


Delete the default.aspx file and Add New Item and select Web User Control from the list and Name it to HelloWorldUC.ascx


Remove the CodeBehind attribute in the HelloWorldUC.ascx.

Add a reference to Microsoft.SharePoint dll in the project. To do it Right Click on the project Add reference and Select Windows SharePoint Services.

Navigate to the Design view for the HelloWorldUC.ascx and from the toolbox add the Panel to the Control and change the ID from the Property window to HelloPanel.

In the HelloworldUC.ascx.vb add the following code to show the username. Also declare a property Web to get the SPweb context to the user control

Private myWeb As SPWeb

Public Property Web() As SPWeb
            Return myWeb
        End Get
        Set(ByVal value As SPWeb)
            myWeb = value
        End Set
    End Property

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim currentUser As String = Me.Web.CurrentUser.Name
            HelloPanel.Controls.Add(New LiteralControl(String.Format("Hello {0}! ", currentUser)))
        Catch ex As Exception
            HelloPanel.Controls.Add(New LiteralControl(String.Format("Error {0}! ", ex.Message)))
        End Try
    End Sub

Build the ASP.NET Control(HelloWorldControl) project to get the HelloWorldControl Assembly. Before building make sure you enable signing from the Project Settings to sign the Assembly. Failing to sign might get error cannot emit Assembly.


Adding Folder and Control to Webpart Project

Now go to the WebPart Project right click and Add New Item and select SharePoint and Select Templates. This will Add a Templates Folder and Template.txt file to the folder. Delete the txt file from the project. Right click on the Templates folder and Add New Folder and rename the folder to ControlTemplates. Right click again on the ControlTemplates folder and Add Folder and name it to Helloworld (to deploy project specific files).


Now right on the HelloWorld folder Add Existing Item and select the HelloWorldUC.ascx file and from the bottom select the Add As Link. If the code behind file (HelloWorldUC.ascx.vb) is added remove it. Because might get error while compiling.


Once everything is done the Project structure will look like the below image


Now add the reference to the HelloWorldControl.dll in the WebPart Project. In the CreateChildControls add the code pasted below

Protected Overrides Sub CreateChildControls()

                ‘ Loads a user control
                Dim myUserControl As HelloWorldControl.HelloWorldUC = CType(Page.LoadControl("~/_controltemplates/HelloWorld/HelloWorldUC.ascx"), HelloWorldControl.HelloWorldUC)
                myUserControl.Web = SPContext.Current.Web
                ‘ Adds it to the controls collection of the Web Part

            Catch ex As Exception
                Me.Controls.Add(New LiteralControl("<br />An unexpected error occurred loading Web Part. " + ex.Message))
            End Try
        End Sub

Build the solution and it should build without error.

Deploying the Solution to SharePoint

To deploy the webpart go to Webpart Project right click on the project and Properties. In the Properties select the debug and enter the SharePoint URL to be deployed.

Now copy the Helloworld.dll and the Helloworldcontrol.dll to the C:\inetpub\wwwroot\wss\VirtualDirectories\8080\bin coz in my case the site is in 8080 port. The directory will change depends on the port selected

Now Right Click on the WebPart project (HelloWorld) and click on deploy solution. This will deploy the webpart to the site and Adds it to the webpart gallery.


To see the webpart gallery Navigate to SiteActions->Site Settings, Under Gallery section select the Web part. Now in the list you should be able to see the HelloWorld.webpart. Click on the webpart will show the preview. It also shows any error occurs


The WebPart is now successfully deployed to the Server.