This is the fourth article in a six-part series on ASP.NET Master Pages and SharePoint.
In the previous article of this series we discussed how to build a customized master page in SharePoint Designer. At the end of the previous article we discussed that making the modifications directly in SharePoint Designer is not the most deployable solution for production life cycle environments.
This article will discuss the other option to customize and deploy a master page for a production ready system.
Features are SharePoint’s way to activate and deactivate certain code on particular site and/or site collection, within a SharePoint website. Features can contain things like - what webparts should be available, what lists should be available, what actions should be on list menus, etc…
Features can also deploy master pages to the master page gallery of a SharePoint site and/or site collection. Using a feature to deploy the master pages gives us reusable files that can be deployed to any SharePoint environment through SharePoint commands. This means that we can create build scripts to deploy our master page to a master page gallery. This is much better than the copy and paste method of SharePoint Designer we showed in the previous article.
Features contain two main files:
Features can be installed to a SharePoint environment through stsadm commands. Stsadm is the command line tool for SharePoint and is used for multiple administrative and development operations in SharePoint.
Once a feature is installed on a SharePoint machine, it can be activate or deactivated on a particular SharePoint site or site collection. Every feature.xml file contains a “Scope” attribute. If the scope of a feature is “Web”, that means that the feature can only be activated at the SharePoint site level. If the scope of a feature is “Site”, that means the feature can only be activated at the SharePoint site collection level. Site Collections contain all the sites in a SharePoint tree, thus if a feature is activated at a site collection, it is activated for all the sites in the site collection. If a feature is activated at a site, it is activated for that site only.
To activate a feature you can go to a site and/or site collection and then go to the Site Settings. In the site settings you will see a link for “Site features”. If you are on a top level site in the site collection, you will also see a link for “Site collection features”.
When you click on the “Site features” and/or “Site collection features” link you will go to the feature activation page for the site and/or site collection respectively. From this page you can activate or deactivate sites for the site and/or site collection.
To customize a SharePoint master page with a feature, you need to create a custom master page file. This file gets uploaded to the master page gallery in SharePoint through the feature.
There are multiple ways to create your own master page files. Here are a few techniques I recommend:
Features are created in Visual Studio and then moved into the SharePoint 12 hive %Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES. After the feature is in the 12 hive, stsadm commands can be used to deploy the feature to the SharePoint environment. Then the feature needs to be activated on the SharePoint site.
Steps to create a feature for Master Page deployment:
Note: We will not be using any tools like Visual Studio Extensions for WSS or WSPBuilder for this walkthrough. However, I recommend using these tools to wrap the features in solutions. We will talk more about this in the last article in this series.
<Feature Id=”95F25D4A-D256-4158-96FE-010F599149CC” Title=”Demo Master Page”
Scope=”Site” Version=”1.0.0.0″ Hidden=”FALSE” DefaultResourceFile=”core” xmlns=http://schemas.microsoft.com/sharepoint/ Description=”This Feature contains the demo master page”>
<ElementManifests>
<ElementManifest Location=”elements.xml” />
<ElementFile Location=”MasterPages\demomasterpage.master” />
</ElementManifests>
</Feature>
<Elements xmlns=”http://schemas.microsoft.com/sharepoint/”>
<Module Name=”DemoMasterPage” Url=”_catalogs/masterpage” Path=”MasterPages” RootWebOnly=”FALSE”>
<File Url=”demomasterpage.master” Type=”GhostableInLibrary” />
</Module>
</Elements>
The final folder/file structure should look like this:
There are 2 steps to deploy a feature:

Also, you probably won’t be able to copy from Visual Studio. So, go to windows explorer to do the copy of the folder.
What’s wrong with this picture?
Any developer will say this is not correct. Developers don’t want to copy files manually and paste them in a folder. Yes, I understand this. I am showing this technique to lead up to the last article in this series on solution deployment. The solution will take care of moving the files to the 12 hive and running the commands for you. However, it is important to understand what solutions do before jumping right into them. Thus, I am showing you this manual way so you can get an idea of what the solution is doing behind the scenes.
The feature we just created placed the new master page in the master page gallery. However, it did not tell SharePoint to use this new master page on your SharePoint site. There are multiple ways to do this including: custom site definitions, programmatically setting the default master page, using SharePoint Designer, or using the MOSS master page selection page.
If you are using WSS, please refer to article 3 in this series http://sharepointmagazine.net/technical/customisation/developing-a-custom-master-page-master-pages-and-sharepoint-part-3-of-6 in order to see how to set the master page in SharePoint designer.
If you are using MOSS, you can go to your site collection and then go to site settings. Then under “Look and Feel” click on the “Master Page” link. You can now choose the demomasterpage.master to be the master page for your site collection.
| Please Note: This assumes you have the Publishing feature turned on in MOSS. The Publishing feature might be turned on and might not be turned on based on the site definition you used to create your site. If the Publishing feature is not turned on in MOSS you will not see the “Master Page” link under “Look and Feel” in the Site Settings page. To turn the Publishing feature on go to Site Settings, then look under the “Site Collection Administration” column and then click the “Site collection features” link. Find the “Office SharePoint Server Publishing Infrastructure” column and click Activate. Now, when you go back to the Site Settings - “Look and Feel” column you will see the “Master Page” link. |
Now the Master Page has been installed to the SharePoint farm and activated on the site collection. So, what if you want to make further customizations to the master page? Do you have to go through the process of re-installing and re-activating? The answer is no. Here comes the power of ghosting. Please refer to article 3 http://sharepointmagazine.net/technical/customisation/developing-a-custom-master-page-master-pages-and-sharepoint-part-3-of-6 for more information about Ghosting. Since the master page is ghosted, the SharePoint site keeps a reference to the 12 hive version of it. So, if you want to make a change to your master page, all you have to do is update the demomasterpage.master file in the feature folder of the 12 hive (%Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEA)TURES.
Sometimes there are changes you need to make to a feature that are not ghosted. Examples of these types of changes are things in the feature.xml file. You cannot change the id of a feature.xml file and expect to be able to just re-install and re-activate the feature. Instead, you have to deactivate the feature and uninstall the feature first. Here are the commands to do this.
Using features to deploy the master page to the master page gallery is a reusable way to deploy master pages in SharePoint. However, there are still manual processes involved (including copying files to the 12 hive). Thus, using features is not the complete story when dealing with SharePoint deployments. In the last article in this series we will discuss SharePoint solutions (WSPs). WSPs are basically CAB files that wrap up features in SharePoint for easy deployment. The WSP file will move the feature files to the 12 hive for you. However, I think it is important to understand how this is done before jumping into creating WSP solutions. Plus, we can reuse the feature we created in this article when we build the WSP solution out in the final article of this series.
I am currently a Project Manager at RDA Corporation (don’t worry – PMs at RDA do a lot of architectural work too). In addition I am also the IW/Collaboration Evangelist Team Leader at RDA. The IW/Collaboration Evangelist Team at RDA is dedicated to the growth and knowledge of SharePoint and other collaboration tools within Microsoft. I have been working with SharePoint since 2001 and have been involved in numerous custom application efforts centered around SharePoint during my employment at RDA. I have been coding in .net since it came out and am Microsoft Certified – MCPD, MCSD, MCAD, MTS (SQL Server 2005), MTS(SharePoint).
Contact the author | Other Posts by ggalipeau (6) | Author's Website