Creating a custom site Definition with a custom master pagePosted: December 23, 2009
Site definition can be considered as a blueprint of a SharePoint site. When developing business solutions on WSS platform sometimes we will have to create our own site definitions to incorporate functional business modules. Site Definitions gives you lots of flexibility in terms of maintaining and upgrading your solutions compared to Site Templates. Its also gives us the flexibility of choosing custom layouts and the ability to pre program the sequence of the feature activation order when you create a new site based on the definition.
In this post I will outline the steps that we have to follow when Creating a custom Site definition with a Custom master page. I am a fan of WSPBuilder as a development tool by Carsten Keutmann because it light to use over WSeWSS 1.3 and does not abstract too much from how the solution artifacts get deployed to your farm environment. Also its great for team development plays nice with TFS and Nightly Build Automation with MSBuild.
I will not be able to cover the details of how to create a custom master page here. So I will be using one of the out of the box default master page as a sample.
Lets get Started.
1. Create a WSPBuilder Project
I am using Visual studio 2008 with WSPBuilder extensions 1.0.6. Once extensions are installed VS will light up with new WSPBuilder project types as shown
below. Select WSPBuilder project and give your project a name.
2. Create 12 hive folder Structure within your solution.
One of the nice feature I like about WSPBuilder is that we can develop against 12 hive folder hierarchy in dev environment.
Standard 12 hive folder structure is shown in left. The folders that we are interested for this task of deploying a custom site definition are highlighted. All standard and custom site definitions with their default pages/schemas lives in SiteTemplates folder. All Features and feature manifests resides in FEATURES folder. So we will create this structure within our solution relative to 12 hive holder (C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\)
Once folders are created inside the solution it should look as follows.
3. Create the site definition.
Create a sub folder inside SiteTemplates folder. This folder will contain all out custom site definition files. I call mine DevSiteTemplate. Instead of creating the definition files from scratch i will copy standard team site definition from 12 hive (C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\SiteTemplates\sts) into my DevSiteTemplate folder. You can delete Defaultdws.aspx file as we do not need it for our requirement.
4. Update master page reference in default.aspx file
We are going to use our own master page in our site definition. Therefore we need to change default.master -> custom.master in our default.aspx page. Our custom master page will be created little later and SharePoint will replace custom.master reference with our master page during runtime page rendering based on the site definition schema (onet.xml) which we defined later on.
5. Update Configurations in onet.xml file
A Site definition can have many configurations. Because we have used Standard Team site definition as our base definition we can remove unused configurations. Std. Team site definition contains three configurations
0 : Default 1:Blank 2:DWS
We are going to use the Default Configuration (0) . Therefore we can remove Blank and DWS Configurations as well as their module sections
Following code segment shows the original onet.xml file contents
After removing the configurations onet.xml file should look as follows
6. Create the Custom Master page Feature
We need to add new feature to apply the master page to our site definition. The advantage of creating a separate feature for master page instead of depend on the site definition is that it can be operated independent from our site definition. In other words its all about loose coupling and reusability.
Right Click the project and Add a new “Web” Scope blank feature. I call it DevSiteMaster. WspBuilder will create feature folder for us with in the FEATURES folder along with the feature.xml file and element.xml file.
feature.xml describes the feature and its id,scope and metadata. feature.xml file reference the element.xml file which describes the real work load for the feature.
Create a folder named MasterPages inside the feature folder to add our custom master page. Now we need to add our custom master page to our solution. For this demo I will be using one of the out of the box master page (default.master) from 12 hive Global folder (C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\GLOBAL). Copy default.master page into the MasterPages folder. Then rename default.master to devsite.master.
Now our solution folder structure should look like below.
Now we need to add our custom master page to our solution. I will be using one of the out of the box master page (default.master) for this example from 12 hive Global folder (C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\GLOBAL).
7. Update Feature.xml and Element.xml files
Update the WspBuilder generated feature file with the settings required for our master page feature. Add the ElementFile section which reference our custom master page.
<?xml version="1.0" encoding="utf-8"?> <Feature Id="a349b379-6fda-41bf-9fe2-cfad70c99f0b" Title="DevSiteMaster" Description="Custom master page feature for DevSiteMasters" Version="22.214.171.124" Hidden="FALSE" Scope="Web" DefaultResourceFile="core" xmlns="http://schemas.microsoft.com/sharepoint/"> <ElementManifests> <ElementManifest Location="elements.xml"/> <ElementFile Location="MasterPages\devsite.master"/> </ElementManifests> </Feature>
Update element.xml file and a module element to copy our custom master page from our MasterPages folder path to master page gallery of the underlying web. Now when DevSiteMaster Feature get activated the job defined in our element.xml file get executed. As a result our custom master page get added into the master page gallery of the underlying sub web.
<?xml version="1.0" encoding="utf-8" ?> <Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <Module Name="DevSiteMasterPage" Url="_catalogs/masterpage" Path="MasterPages" RootWebOnly="FALSE"> <File Url="devsite.master" Type="GhostableInLibrary"/> </Module> </Elements>
8. Update onet.xml file and add the DevsiteMasterPage feature into the list WebFeatures.
The Features specified in the SiteFeatures and WebFeatures section that get activated as part of site provisioning process based on the site definition. Also they get activated in the order specified. We need to define our custom master feature under WebFeatures. If our master page feature had been a “Site” scoped feature we would specify under siteFeatures section.
<WebFeatures> <Feature ID="00BFEA71-4EA5-48D4-A4AD-7EA5C011ABE5" /> <!-- TeamCollab Feature --> <Feature ID="F41CC668-37E5-4743-B4A8-74D1DB3FD8A4" /> <!-- MobilityRedirect --> <!-- devsite custom master page--> <Feature ID="a349b379-6fda-41bf-9fe2-cfad70c99f0b" /> </WebFeatures>
9. Update onet.xml default configuration to use our Custom master page.
Update default configuration and specify the that our confiugration should used our custom master page by setting the CustomMasterUrl and MasterUrl points to our custom master page living in the master page galley. By setting these values we effectively telling SharePoint that when ever .aspx refers to custom.master page replace that with devsite.master page which is our custom master page.
<Configuration ID="0" Name="Default" CustomMasterUrl ="_catalogs/masterpage/devsite.master" MasterUrl="_catalogs/masterpage/devsite.master">
10. Create WEBTEMP*.xml file for the Custom SiteDefinition.
WebTemp*.xml add our custom site definition into the catalogue of site definitions so that we can use pick the site defintion to provision from create site and workspace application pages. It is important that the Template Name exactly match our site definition name as shown below. Create a folder called xml inside 1033 folder and create webtemp*.xml file inside the folder named xml. This file name must begin with “webtemp” prefix as required by SharePoint. In this demo I call it WebTempDevSite.xml
<?xml version="1.0" encoding="utf-8"?> <Templates xmlns:ows="Microsoft SharePoint"> <Template Name="DevSiteTemplate" ID="75999" SetupPath="SiteTemplates\DevSiteTemplate" > <Configuration ID="0" Title="Dev Site" Hidden ="false" ImageUrl="/_layouts/images/stsprev.png" Description="A Custom Site Definition based on Team site" DisplayCategory="Custom Templates" > </Configuration> </Template> </Templates>
Our final solution structure should now look as follows.
All done! Our solution is now ready for package up into .wsp solution file and deploy into the SharePoint Farm. WSPBuilder will take the heavy lifting from here including creating manifest files,DDF files and packaging up our solution. Therefore Right Click our project and select Deploy option from WSPBuilder menu.
This action will build and package our solution, add it to the SharePoint solution store and deploy the solution by invoking STSADM.
Once succesfully deployed we should be able to create site based on our custom site definition as follows.
The visual studio solution associated with this post can be downloaded from here. Hope this helps with creating a custom site Definitions with a custom master page using WSPBuilder.