In this post i would like to share an easy way to set your custom masterpage each time a new site collection is created. That means you already has a custom masterpage and you also have also a solution which contains the deployment and feature activation for your custom masterpage. If not please look at my last post, cause this post will be the second part, so i won’t repeat the things.
In my last post i talked about feature stapling. Let me explain some things:
Feature stapling means that you create two features. Feature A manages that Feature B is activated at a defined site definition. So Feature A staples Feature B. That means if you want to apply a custom master or custom theme or even a custom css to all team sites site definitions. It does not work for already created sites (site collections).
Okay, let’s see how we can use it in order to apply our custom master page to team sites and blank sites site definition:
Step 1: Add a feature called “MasterPageStapler” and set its scope to “farm”
I renamed the feature and set its scope to farm level.
Step 2: Add an empty Element to you solution.
In the Elements.xml file of the empty element we add the tags FeatureSiteTemplateAssociation with two attributes.
Id: this is the id of your feature for the custom masterpage. How you can get it, i explained below.
TemplateName is the name of the site definition with “#” and a number. The number is the id of the site definition. If you looking at the directory of C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\1033\XML you’ll find the WEBTEMP.xml file. There you can find entries like:
<Template Name=”STS” ID=”1″>
<Configuration ID=”0″ Title=”Team Site”….
The STS + “#” + ID is important for your attribute TemplateName. Below you find a powershell script which displays all site definitions.
How to get the feature id?
Open your feature for the custom masterpage and look into the properties. There you find the feature id.
How to get the template name?
- use Powershell with this function: Get-SPWebTemplate
- or look at this post
Step 3: Check your features
This step is important cause you added your StaplerDefinition element which should not be in the custom masterpage feature but in the MasterPageStapler feature which is farm scoped.
Step 4: Deploy and create a site collection
After deployment you see in the System Settings – manage farm features of your central administration that your stapler feature is activated. So now you can create a new site collection and you will see the result:
That’s it, now you can use create new site collections and your custom masterpage will be used for the defined site definitions. It should also work for custom site definitions. Similiar to the custom master page it should also work for custom themes and custom css files. So hope this helps you.
..:: I LIKE SHAREPOINT ::..The article or information provided here represents completely my own personal view & thought. It is recommended to test the content or scripts of the site in the lab, before making use in the production environment & use it completely at your own risk. The articles, scripts, suggestions or tricks published on the site are provided AS-IS with no warranties or guarantees and confers no rights.
He is Consultant for SharePoint Solutions for collaboration, communication and business processes. He provides his customers applications based on standard features as well as development or combining the power of several microsoft tools like Dynamics CRM. In his free time he tries to collect tipps and worthy experience in this blog.