Deploying a custom master page in SharePoint 2010

Lately I’ve been playing around with Visual Studio 2010 tools for SharePoint. I’ve been trying to come up with examples on how to use each of the available item templates.
Today I created a Module. A module can be used to deploy files to your SharePoint environment and the scenario I came up with was to use a module to deploy a master page.

  • I started out by creating an Empty SharePoint project. This can have any name you like and it can be both a farm and a sandboxed solution
  • Next add an new item to the project and choose a module
  • You can rename the module if you want to, I will leave the name at Module1
  • After the module is created it contains an elements.xml file and a sample.txt file. The sample.txt file can be renamed to sample.master.

Project structure
Project structure after adding a module and renaming the sample file to sample.master

I’m not a designer, so I wasn’t interested in creating a brilliant new design (good job, because I wouldn’t be able to do that!).
For the purpose of this example I just took the standard SharePoint 2010 master page contents.

  • Open up SharePoint Designer and open the site you are working on
  • Choose Master Pages on the left side
  • Open v4.master

Don’t let SharePoint fool you. There is a default.master, but this is the old SharePoint 2007 master page that is used if you perform a visual upgrade. When creating a site in SharePoint 2010 the master page that will be used by default is v4.master. I know it’s a bit confusing, but if you have been working with SharePoint for a while you won’t be surprised by this.

  • Copy the contents of v4.master and past them in your sample.master file in Visual Studio. Make the changes you want to.
  • Note how the elements.xml file has changed to reference the sample.master rather than the sample.txt

Contents of the elements.xml file 
The contents of the elements.xml file

  • Make sure the module is added to a feature in your project that is scoped at the site (= site collection) level

You could stop here and this solution will deploy your sample.master master page to the master page gallery of your site collection.
This will not apply the master page to the site collection when the feature gets activated though. It will only make it available for selection.

  • If you the master page to be applied when the feature gets activated you right click on the Feature1 and you add an event receiver. This will add a feature receiver file to the feature.
  • In this file we will add some code that will apply the sample.master master page when the feature gets activated and it will apply the v4.master when the feature gets deactivated.

Code used in the feature receiver
Code used in the feature receiver

Now if you activate the feature the code in the feature receiver will run and will apply sample.master to the site. As you can see we change both the MasterUrl and the CustomMasterUrl. The MasterUrl is used on all pages that are not publishing pages. This means it is used on the pages in the sitepages library and on the pages in the _layouts directory like the settings page for instance. The CustomMasterUrl is only used on pages that are stored in the Pages library. This library is created when the SharePoint Server Publishing Infrastructure features is activated on the site collection and the SharePoint Server Publishing feature is activated on the site.

You can also use the SharePoint user interface to apply a new master page. The Site Master Page is the equivalent of the CustomMasterUrl and the System Master Page is what the object model calls the MasterUrl.

Changing the master page using the SharePoint UI
Changing the master page using the SharePoint user interface

Print | posted @ Friday, March 26, 2010 12:33 AM

Comments on this entry:

Gravatar # re: Deploying a custom master page in SharePoint 2010
by Andries den Haan at 3/26/2010 7:12 AM

Great post Mirjam, how about rolling back the masterpage customization upon feature de-activation. I like to keep a minimum foot-print for custom development where possible :-)
  
Gravatar # re: Deploying a custom master page in SharePoint 2010
by Balaji at 3/31/2010 5:41 AM

Great Post !!!!
  
Gravatar # re: Deploying a custom master page in SharePoint 2010
by Sachin Pawar at 4/16/2010 2:18 PM

Hi,

Thanks Mirjan. It helped me to deploy master page successfully. However I had to remove 'Type' attribute in File to successfully activate the master page.

Thanks,
Sachin
  
Gravatar # re: Deploying a custom master page in SharePoint 2010
by Sachin Pawar at 4/19/2010 6:37 AM

Sorry, it was my bad.

Thanks,
Sachin
  
Gravatar # re: Deploying a custom master page in SharePoint 2010
by susan at 5/18/2010 4:15 PM

Hey Mirjan,

This is great! I was just curious how you would add your CSS and images file to the project in order to deploy them as well??
  
Gravatar # re: Deploying a custom master page in SharePoint 2010
by Jenny at 6/10/2010 8:51 AM

Great post very helpfull! Just like Mirjan I would also like to know how to add css and images to the project.
  
Gravatar # re: Deploying a custom master page in SharePoint 2010
by Bryan at 6/16/2010 8:48 PM

Once I deploy, and refresh my site, i get a file not found error. It appears I have to manually set the default master page via SPD. What am I doing wrong?

b
  
Gravatar # re: Deploying a custom master page in SharePoint 2010
by Deanna at 6/17/2010 7:49 PM

I'm having the same issue as Bryan. Any ideas what is wrong?
  
Gravatar # re: Deploying a custom master page in SharePoint 2010
by Deanna at 6/18/2010 3:01 PM

Okay - I figured it out. There were 2 issues:
1. The article doesn't explicitly state to copy all the info in the elements.xml file - you need to do that.
2. You need to make sure that the URL and Path parameters in the elements.xml match what you're actually using. So, if you rename Module1, then make sure you update your file element.
  
Gravatar # re: Deploying a custom master page in SharePoint 2010
by Anil at 6/22/2010 5:36 PM

I have an issue. I cannot see my master in list. If I activate the feature, I get a file not found error
  
Gravatar # re: Deploying a custom master page in SharePoint 2010
by mirjam at 7/1/2010 4:08 PM

Hi Anil,

All I can say without more information is that you can check whether the name of the Module matches the file paths mentioned in the elements.xml file.

Also check whether the master page is actually deployed to the SharePoint root folder (14 hive) on the file system and the Master Page gallery in SharePoint. If the file is not there something went wrong in your deployment.

Hope that helps.

Mirjam
  
Gravatar # re: Deploying a custom master page in SharePoint 2010
by Randy at 7/23/2010 8:15 PM

Hi Mirjam,

How do you handle custom images and custom css files for the master page you just created? Do you create a separate module? How do you reference them?

Cheers!
  
Gravatar # re: Deploying a custom master page in SharePoint 2010
by Rodolfo at 8/5/2010 5:13 PM

Hi Mirjam,

How do you handle custom images and custom css files for the master page you just created? Do you create a separate module? How do you reference them?

Thanks

Rodolfo
  
Gravatar # re: Deploying a custom master page in SharePoint 2010
by Jamie at 8/20/2010 6:04 PM

Help....can you create a master page in designer 2007 without having the sharepoint server 2007? We are using this for basic web access but would like to create a frame that stays on all pages.

  
Gravatar # re: Deploying a custom master page in SharePoint 2010
by mirjam at 9/1/2010 9:59 AM

Hi Jamie,

You can only use SharePoint Designer 2007 if you are using SHarePoint 2007.
  
Gravatar # re: Deploying a custom master page in SharePoint 2010
by digitalslavery at 9/8/2010 3:56 PM

Mirjam,

Great post! Would be awesome if you added how to include custom CSS and image files as most people would be needing to deploy those to the server when using a solution like this, but it is a great starting point!
  

Your comment:

Title:
Name:
Email:
Website:
 
Italic Underline Blockquote Hyperlink
 
 
Please add 1 and 6 and type the answer here: