Apply a MOSS / WSS Theme via Feature Stapling

I recently wanted to apply a theme for a SharePoint Site via Feature. I wanted to achieve this with a SharePoint solution so that it helps our team development / Continuous integration approach . WSS allows you to attach features to site definitions via feature stapling so that we don’t have to touch the the site definition files or artefacts to enhance site definition. This is the best practice for modifying out of the box site definitions.

In this post I am going to go through the steps I have taken to achieve this task with VSeWSS 1.3 toolkit and visual studio 2008 .

1. Create a new Empty SharePoint project

image

2. Select Full Trust level so that your assembly get deployed to GAC during deployment.

image

3. Visual studio creates an empty project. Now we need to add our theme files to the solution. Theme files get deployed to 12 hive templates/Themes folder.  Therefore Add a new item to the project and select “Template” item as shown below. Once template folder is created you may delete the sample .txt file.

image

4. Create a Themes folder inside the Templates folder and add your theme folder along with theme files as shown below. During deployment this folder hierarchy get created inside the 12 hive.

image

5. Switch to WSP View. It reflects logically how things will get packaged into the WSP solution package.

image

6. Now we need to add a new feature for the theme. Therefore click new feature in WSP view and select “Web” scope with a Feature Receiver. This will create a feature receiver class for us to override feature event handing.

image

7. Once feature get created rename the feature name in WSP view. I called mine “iTimeMax Theme”. Note that when you rename features in WSP View it does not get reflected in the solution view. You will need to rename it in the solution view manually.

image

8. Update your feature receiver class as follows.When this feature get activated we need the theme get applied to the underlying site and when deactivated we need the theme removed from the underlying site.

FeatureReceiver class
[SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]


public override void FeatureActivated(SPFeatureReceiverProperties properties)


{


SPWeb web = properties.Feature.Parent as SPWeb;


web.ApplyTheme("iTimeMax");


}


[SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]


public override void FeatureDeactivating(SPFeatureReceiverProperties properties)


{


SPWeb web = properties.Feature.Parent as SPWeb;


web.ApplyTheme("none");


}


9. Update Feature.xml file and specify where it should find the feature receiver class as shown below. Use the sn.exe util with –Tp switch to generate the public key token for your assembly.

Feature.xml

<?xml version="1.0" encoding="utf-8"?>


<Feature Id="32de8e0c-8a20-469b-b40a-edb47710de42" Title="iTimeMaxTheme" Scope="Web"


Version="1.0.0.0" Hidden="FALSE" DefaultResourceFile="core"


ReceiverAssembly="iTimeMax.RI.UpdateTheme, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f4da00116c38ec5"


ReceiverClass="iTimeMax.RI.UpdateTheme.ApplyThemeFeatureReceiver" xmlns="http://schemas.microsoft.com/sharepoint/">


<ElementManifests />


</Feature>


10. now we need a second farm level feature for feature stapling. This feature will attach our theme feature to the underlying site definition. Once create rename the feature and element manifest file  as required. I call it iTimeMaxFeatureThemeStapler.

image

11.Update the Element file with FeatureSiteTemplateAssociation element where we specify which feature should be stapled with which site definition. In this case we need our  iTimeMax Theme feature with feature id “32de8e0c-8a20-469b-b40a-edb47710de42" staple to Standard team site definition “STS#0”

FeatureElement.xml

<?xml version="1.0" encoding="utf-8"?>


<Elements Id="0140e1af-0c03-4297-b7d9-4444d7d04f5d" xmlns="http://schemas.microsoft.com/sharepoint/" >


<FeatureSiteTemplateAssociation

Id="32de8e0c-8a20-469b-b40a-edb47710de42" TemplateName="STS#0">

</FeatureSiteTemplateAssociation>


</Elements>


12.  Now our Solution View file structure and WSP view feature organisation should reflect as below.

image image

13. All Done! Now we need to deploy this feature. Right Click the project and Select “Deploy” option to deploy the WSP solution to the SharePoint site. WSeWSS deploy option build, package and deploy the WSP solution.

image

14. Note that our Farm level feature is activated

image

15. Now we can go a head and provision a new team site from the standard team site definition.

image

16. Once site provisioning is complete, our custom theme get applied as shown below. Also if you go the site settings page of the newly created site you will see that our “Web” scope feature is automatically activated.

image

Feature stapling is particularly useful when you want to add functionality to standard site definitions because editing standard site definition files is not a recommended practice. Also it gives you the ability to reuse your customizations across many site definitions in a loosely coupled manner and reduce administrative overheads.

You can download the Visual Studio Solution associated with this post here You will need VSeWSS toolkit 1.3 installed to open the solution.


2 Comments on “Apply a MOSS / WSS Theme via Feature Stapling”

  1. George says:

    Well explained article. Cheers

  2. […] feature receiver code yourself. This will also allow a great deal of flexibility going forwards. Here is a simple example of how can apply a theme via a […]


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