Google+

Feature Stapling

Leave a comment

July 23, 2012 by Ruaan Harmzen

In our environment we have a custom master page for our main intranet site and we want to extend this look and feel to the MySite implementation. So as mentioned before in Sharepoint 2010 there are 2 site collections related to MySites. The first is the main site collection which hosts the profile\newsfeeds\colleagues etc. The second is a site collection that hosts the content for each users, thus if you have 5000 users there will be 5000 site collections for their content.

To enable the master page and theme to the first site collection is easy. All you do is select the master page and theme and let it update all sites below that site collection.

The second part is a but more tricky but easily achievable through something called Feature Stapling. Feature stapling allows us to install additional features when a specific feature is deployed when a site is created i.e. we staple on feature to another.

To use Feature Stapling your master page needs to be included in a Sharepoint solution. I will not detail how to do this as there are loads of resource available on line. For this example let’s say our main master page feature is called MainMasterPageFeature.

So first thing is to create our stapler feature.  Create a new element in your project and call it MainMasterPageFeatureStapler. Remove all files under this element except the elements.xml. Edit the file to contain the following:

<?xml version=”1.0″ encoding=”utf-8″?>
<Elements xmlns=”http://schemas.microsoft.com/sharepoint/&#8221; >
<FeatureSiteTemplateAssociation Id=”<MainMasterPageFeature GUID>” TemplateName=”SPSMSITEHOST#0″/>
<FeatureSiteTemplateAssociation Id=”<MainMasterPageFeature GUID>” TemplateName=”SPSPERS#0″/>
<FeatureSiteTemplateAssociation Id=”<MainMasterPageFeature GUID>” TemplateName=”SPSMSITE#0″/>
</Elements>

The TemplateName in each XML node refers to the built in template names of the MySite templates. So in short this configuration tells the feature that when a site is created using one of these templates then automatically activate the feature specified by the Id (which in our case is the MainMasterPageFeature). Make sure that this element is included in your package content.

Next is the theme:

Add a new module to your project and name it the same as your custom theme (once again I am not detailing how to create a theme). Drag your .thmx file to the module and then edit the Elements.xml as follows:

<?xml version=”1.0″ encoding=”utf-8″?>
<Elements xmlns=”http://schemas.microsoft.com/sharepoint/”&gt;
<Module Name=”MyThemeName” Url=”_catalogs/theme” RootWebOnly=”True” Path=”MyThemeName”>
<File Type=”GhostableInLibrary” Url=”MyThemeName.thmx” IgnoreIfAlreadyExists=”true”/>
</Module>
</Elements>

This will install the the theme when the feature is activated. The last thing we need to do is to set this as the active theme. Add a new event receiver to your main feature (MainMasterPageFeature) and implement the FeatureActivated event. The code below will active your theme when the main feature is activated:

SPSite site = (SPSite)properties.Feature.Parent;
SPWeb sweb = site.OpenWeb();

ReadOnlyCollection<ThmxTheme> managedThemes = null;
managedThemes = ThmxTheme.GetManagedThemes(site);
foreach (ThmxTheme theme2 in managedThemes)
{
if (theme2.Name == “MyTheme”)
{
theme2.ApplyTo(sweb, true);
break;
}
}

Now all you need to do is package the feature and deploy. Enjoy!

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

%d bloggers like this: