Customisation, Technical
October 6, 2008

Developing a Custom Master Page (Master Pages and SharePoint part 3 of 6)



Enlarge Image

Written by: ggalipeau

Introduction

This is the third article in a six-part series on ASP.NET Master Pages and SharePoint.

  1. Introduction to Master Pages.
  2. Examining the out of the box Master Pages in SharePoint.
  3. Developing a custom Master Page for SharePoint.
  4. Options to deploy a Master Page for a production ready system.
  5. Customizing the Application.master Page.
  6. Incorporating a Master Page into a SharePoint site definition

In the previous two articles of this series we discussed master pages and how SharePoint uses them to create it’s out of the box pages. Now that we have an understanding of how master pages work and how SharePoint uses them, we can start showing how to develop custom master pages.

There are two supported scenarios for customizing master pages in SharePoint:

  1. Edit the master page in SharePoint Designer
  2. Create a custom master page and upload it to the master page gallery

Please notice that modifying the existing master page file is not one of the supported options. Microsoft rarely supports changing files in SharePoint unless it is done from SharePoint Designer.

In this article we are going to concentrate on the “Edit the master page in SharePoint Designer” option. This will show us how to develop a custom master page. In the next article we will learn how to deploy this custom master page for a production ready system.

UnGhosting (Customizing)

When a new site in SharePoint is created it uses the default.master for its master page, which is located at %Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\Global.

Note: This is true for WSS team sites and standard MOSS team sites (MOSS publishing sites might use a different master page to start with).

SharePoint keeps a reference to this one file no matter how many sites are created. This is a process called Ghosting, or Uncustomized. Thus, every site collection and/or site in SharePoint point to one file.

If a master page is customized in SharePoint Designer, SharePoint stores a modified version of the master page in the content database (this is a technique called “Unghosting” or “Customizing”). This process basically breaks the reference SharePoint keeps to the default.master file on the 12 hive. The reason for this is that changes will not affect the other content pages that refer to it.

SharePoint Designer can’t modify the default.master page in the 12 hive because it would modify a file that multiple site collections and/or sites use. Instead, it goes through the “UnGhosting”, or “Customizing”, process. This is the process of removing the link to the 12 hive and making a copy of the default.master in the content database.

SharePoint Designer

SharePoint Designer knows about the SharePoint architecture. Since the SharePoint architecture is a mixture of physical files (on the 12 hive) and content (from the database), a web design tool for SharePoint must know about this architecture. SharePoint Designer is the only tool currently available that knows about this architecture. SharePoint Designer knows how to render the structure of the SharePoint site and how to allow for changes using the UnGhosting, or Customizing, techniques.

To modify a master page in SharePoint Designer first open SharePoint Designer on your SharePoint box. Next open the SharePoint site by going to File - Open Site. In the Site Name field put the url to the home page of your SharePoint site, without the actual page in it (i.e.: don’t keep default.aspx in the string).

After you open a site in SharePoint Designer, Designer will create the folder structure for the SharePoint site:

Create a new master page

The master page gallery is located in the _catalogs folder. Within the master page gallery you will find the default.master page. You can modify this file directly, but it is good practice to make a copy of this file and modify the copied file. I am going to call this copied master page “customDefault.master”.

After you make a copy of the master page, the content pages will not know that this is the master page they should use. So, default.aspx is the content page. And if you open default.aspx in SharePoint Designer, you will notice this in the upper right corner:

Notice that it is still pointing to the default.master page. If you want it to point to the new customDefault.master page, then right click on customDefault.master (in the Folder List) and choose “Set as Default Master Page”. After doing this, open the default.aspx page again and notice that the master page referenced changed:

So, why does this reference pointer change, but the MasterPageFile still points to default.master (MasterPageFile=”~masterurl/default.master”). The reason is dynamic tokens. SharePoint replaces “masterurl/default.master” with the dynamic token value at run time. The reference pointer we are seeing is that dynamic token. So, when you click “Set as Default Master Page” in SharePoint Designer, you are changing the dynamic token for the SharePoint site. For more information on dynamic tokens, refer to the second article in this series - Examining SharePoint Master Pages.

Customize the customDefault.master

Open up the new customDefault.master page. If you are using MOSS, SharePoint Designer might ask you to check it out at this point. If you are in WSS, you shouldn’t have to check the file out.

SharePoint Designer has three different modes for customization:

  1. Design - WYSIWYG editor
  2. Code - html editor
  3. Split - keeps the design and code modes open

I prefer to keep SharePoint Designer in split or code mode. The reason is that when you customize a SharePoint master page you have to be careful where you put customizations. If you place a customization in a ContentPlaceHolder, there is a chance the customization won’t show on the final page because the implementing content page might take care of filling in the particular ContentPlaceHolder. So, if you make changes in design mode, it is beneficial to see what is happening in the html.

So, let’s make some customizations so you can see some things that can be done.

Create a footer

  1. Find the tag </Body> in your code view and put your cursor right before it.
  2. Type the some footer text - ex: Copywrite RDA all rights reserved 2008.
  3. Wrap the footer text in a div tag.
  4. Set a class to the div tag. For example purposes we can use an out of the box css class from SharePoint
  5. <div class=”ms-CommentFooter”>Copywrite RDA all rights reserved 2008</div>

  6. Save the file.
  7. Note: SharePoint Designer should give you a popup telling you that you are about to UnGhost, or Customize, your SharePoint master page. This is your warning that you are going to break that reference to the 12 hive and make a separate master page in the content database. Click Yes.

  8. Optional: if you are in MOSS, you might have to check the file back in at this point. Right click on the customDefault.master in the Folder list and choose “Check In”.
  9. Open your site in your web browser to see the change.

Create a new style

Our footer example uses the out of the box “ms-CommentFooter” for our css class. This example will show you how to change that to one of your own custom css classes.

  1. Change “ms-CommentFooter” to “custom-footer” in the code view
  2. <div class=”custom-footer “>Copywrite RDA all rights reserved 2008</div>

  3. Select the “new style” link from the Apply Styles toolbar on the right
  4. Set the properties of the style
    • Selector = .custom-footer
    • Font
      • Font-family = Verdana
      • Font-size = x-small
      • Font-style = italic
  5. Click Ok on the style popup
  6. Save the file
  7. Optional: if you are in MOSS, you might have to check the file back in at this point. Right click on the customDefault.master in the Folder list and choose “Check In”.
  8. Open your site in a web browser to see the changes

Modify a SharePoint Control

  1. Find the top menu in the code view
  2. <SharePoint:AspMenu
    ID=”TopNavigationMenu”sdgds

  3. Change the Orientation to Vertical
  4. Orientation=”Vertical”

  5. Save the file
  6. Optional: if you are in MOSS, you might have to check the file back in at this point. Right click on the customDefault.master in the Folder list and choose “Check In”.
  7. Open your site in a web browser to see the changes. Your menu control should be vertical now.

(Please not the intent of the above change is to show that the out of the box SharePoint controls can be changed. I understand that the actual change we made is not user friendly).

Modify out of the box css classes

  1. Find the text “ms-globalbreadcrumb” in the code.
  2. Right click on it.
  3. Click “Follow Code Hyperlink”
    • a. It will take you to one of the sections for this css class
    • b. The section right above it contains the code for the background color.
    • c. Change the background color to #000000
  4. .ms-globalbreadcrumb{
    font-size:8pt;
    text-align:right;
    background-color:#000000;
    padding:2px 10px 2px 5px;
    }

  5. Save the css file
  6. Note: SharePoint Designer should give you a popup telling you that you are about to UnGhost, or Customize, your css file. This is your warning that you are going to break that reference to the 12 hive and make a separate css file in the content database. Click Yes.

  7. Open your site in a web browser to see the changes. The top bar should be black now.

(Please not the intent of the above change is to show that the out of the box SharePoint controls can be changed. I understand that the actual change we made is not user friendly).

Be careful with your power

These are just a few examples of some customizations that can be done to the master page through SharePoint Designer. The customizations you can do are endless. However, you must be careful that you do not customize something that is required for SharePoint to work.

Some examples of controls you don’t want to remove are:

  • Site Actions
  • Search (if you want search capabilities)
  • Navigation menus (unless you want to replace them with your own navigation)
  • Etc…

Also, be careful where you make your changes. In the previous articles we talked about how making changes to content within ContentPlaceHolders might get overridden by the implementing content page. Thus, if you make a change to a ContentPlaceHolder, you might not see it on your page. My recommendation is to put SharePoint Designer into Split mode or Code mode so you can edit the html directly and you can be sure that you are not editing html within ContentPlaceHolders.

And remember; don’t remove any of the ContentPlaceHolders. The content pages in SharePoint already have references to these place holders. If you remove them the content pages might break.

Undo your Customizations

After making the change notice the default.master in the folder list on the left side. It now has a symbol next to it. If you mouse over that symbol it will tell you that the page has been customized. This is telling you that you have UnGhost, or Customized, this page at one point.

If you decide that you made a mistake by UnGhosting, or Customizing, your file - then you can undo your changes by “re-Ghosting” the file. To do this, right-click on the default.master file and choose “Reset to Site Definition”.

SharePoint Designer Pros

Easy - SharePoint designer provides an easy experience to customize SharePoint master pages. Any user with html experience should be able to use SharePoint designer to make changes to a master page. As the steps above show - you can just open your master page up, make your changes and then save your changes in SharePoint Designer.

UI experience - SharePoint designer gives you a WYSIWYG (what you see is what you get) UI experience. This means you can make changes to the website through an interface that looks like the website. The other techniques we are going to mention do not allow for this.

SharePoint Designer Cons

Unghosting/Customizing - the Unghosting, or Customizing, technique keeps all changes scoped to the respective site collection. There is no real way to point the master page to another site collection. This can hurt maintainability if you are creating multiple site collections on one SharePoint box and want to keep a consistent master page.

Not Deployable - SharePoint Designer forces you to make changes directly against a SharePoint site collection. This makes it hard to move from a development, to QA, to production environment. This also makes it so the customized master page cannot be used against multiple site collections.

Conclusion

SharePoint Designer provides us a quick way to customize master pages in SharePoint. This is a great tool to design master pages and/or customize an existing SharePoint site. However, SharePoint Designer does not provide a clear deployment path for projects using a development lifecycle. The next article in this series will show how to create custom master pages that can be uploaded to any SharePoint site for easy deployments.

This entry was posted on Monday, October 6th, 2008 at 10:30 am and is filed under Customisation, Technical. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
About the Author

Greg Galipeau

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
  • Janajit
    Hi I its a great article.I have one question however.I have designed a custom master page without using the designer.Now since I do not want users to be able to tinker with my custom master page in any way I want to disable the master page selection link in Site Actions>> Site Settings>>Look and Feel>> Master Page link.How can I do that? Could you please help me?
  • ggalipeau
    Hi Janajit,
    Sorry it took me so long to reply, I have been away for a few days. Anyways, I would suggest doing this with permissions. You shouldn't give anyone permissions to get to that link if you don't want them to do that. Even if you want to give people Admin permissions, you might want to consider making a custom Admin permission group that can do everything except this.
  • Janajit
    Hi Greg,

    Thanks a lot for your suggestion.I managed to hide the link in another way though.I modified custom action settings in the manifest files of SiteSettings,Publishing and Publishing resources features to drop the links.What do you think about this way and the next question that arises is Can I add the links back for a particular site collection once I have dropped them by modifying the aforesaid features.Could you please suggest a method to unhide the links which I dropped using customaction tags of Sitesettings, Publishing and PublishingResources features.
  • ggalipeau
    Hey Janajit,
    You said you modified the manifest files - do you mean you actually modified files in the 12 hive to do this? Or, did you accomplish this in another way (i.e.: wrote a new feature or something else)? The reason I ask is that you shouldn't modify files in the 12 hive. If you didn't do that then your approach is fine and you should be able to remove the custom action changes by changing the custom feature again.
  • Janajit
    Well I did modify the files in the features folder ie I modified the Publishing,PublishingResources and SiteSettings features(Changed tags in associated xml files) .So what you are suggesting is I create custom featurs and modify them?
  • ggalipeau
    Yes, you don't want to modify anything in the 12 hive directly. There are usually other ways to do these things through adding new custom features.
  • Janajit
    I am facing another problem Greg.This time its to do with custom groups and permission levels.I want to create custom groups and permision levels.In order to accomplish this I am using an XML config file for specifying custom groups and permission levels and I am using SPFeatureReceiver to create the custom stuff during portal creation by referring to this feature from my onet.xml. The problem is even though I am using Properties["vti_associatevisitorgroup"] = group.ID.ToString() to hide Visitors ,Members and Owners(default groups), these are persisting after the collaboration portal gets created.Whats really beats me is its all working finne with team sites.The problem is cropping up for the collaboration portal alone.Please help me out.I am in a bad stuck.
  • ggalipeau
    Hi Janajit,
    You might want to post these kind of issues to the msdn formuns for SharePoint: http://social.msdn.microsoft.com/Forums/en-US/c... . There are lots of MVPs and SharePoint enthusiasts like myself that answer questions there.
  • Janajit
    Hi Greg,
    I will bother you once more.All your previous suggestions were so helpful.I want to have certain fetaures activated by default when I create a site collection.Is there a way to do this?
  • ggalipeau
    Hi Janajit,
    The best way to do this is to create a custom site definition and activate it from there. So, when a new site is created from the site definition it will also activate the correct features. I will be showing this when I do the final article of the series.The basica process is to add the GUID of the feature to the onet.xml file in the site definition.
    However, you might want to attach a feature to a non-custom site definition (i.e.: one of the out of the box ones). Or to one you have already deployed and don't want to change. Well, there is a way to do this also. It is called feature stapling. I would suggest searching the internet for feature stapling. There are a lot of good articles on it.
    Hope this helps,
    Greg
  • Bikram K Bhuyan
    Hi Greg,

    I have the following requirement which might be very basic to you. But it will be good if you could provide some pointer to this.

    1. I have created a WSS site in which I have created a new master pages based on my sites's look and feel and created most of the pages. This is working fine.
    2. There are 10% of the pages which does not fall into the standard look and feel. Like many pages has content without the any header and footer etc.

    Could you please suggest how to create these 10% of the pages which does not follow the normal master page structure.

    I guess I need to create multiple master pages. But not sure how to use the other master pages as when I create a new pages using the "Site Setting" option it takes the default master page.

    Please provide some solution to this issues.

    Thanks,
    Bikram.
  • ggalipeau
    Hey Bikram,
    There are a few ways to do what you are suggesting. What I usually do is create custom site definitions for each kind of site. These site definitions can have references to different master pages. Then, when someone goes and creates a site, they just have to pick the correct site definition from the choice of templates on the create new site page. The last article in this series explains how to create site definitions. This might seem overkill, but you can get a lot of other benefits from using site definitions including turning features on or off depending on the site definition chosen. Anyways, that is what I typically do.
    Good luck,
    Gregt
  • Giles
    Hi Greg,
    I do not know if anyone can really help me with this one as it may be an internal network issue but in the hope you or any of your collegues have come across it before I have the following problem...

    I have been using SPD to customize the master pages in my site - all was going well and then we decided to make the portal accessible from outside the network on a SSL. We eventually gave up on that plan and went back to the option where users VPN into the netwrok to gain access.

    Our server farm was working fine and then the minute we did all this changing about with SSL trial certificates we lost the ability to save changes to the MasterPG. The SPD client simply comes up with a dialog box " Web File Properties" "Verify document properties..." and then eventually decides to tell me the Server connection has timed out...

    Any help on this one would be much appreciated.

    Giles
  • ggalipeau
    Hi Giles,
    I am sorry, I haven't seen this issue before. You might want to try the msdn forums for designer: http://social.msdn.microsoft.com/forums/en-US/s...
    There are a lot of good people there who have seen many different issues.
  • Giles
    It has managed to throw up an error that means something fianlly:

    ID: 7, Application Name: Microsoft Office SharePoint Designer, Application Version: 12.0.6211.1000, Microsoft Office Version: 12.0.6215.1000. This session was terminated unexpectedly.

    Anyone know what it means?
  • ggalipeau
    Hi Giles, sorry, haven't seen this one either. Sounds like there are some networking issues with your server that has Designer on it. Try the forum I mentioned to you before, it is a good place to ask questions.l
  • Fodi
    Well, I'm not going to ask for any help. I just wanted to say thank you :)
    I'm a SharePoint developer, studying for 70-541. Deploying Master pages via features is one of the topics covered in the exam and I needed a quick refresher. Your article was very helpful as I couldn't find this topic covered in the SDK.

    Cheers.
  • Anita
    I was wondering if anyone who has created a custom master page has had the experience where visually everything seems to work, but when you edit an article page within the new template....it does not save the changes made.

    I have the site actions button working, I click edit page, I type new content into the article, I choose exit edit mode. Looks good. But if I reload the page (navigate off and on to it again), the old version is still there.

    Is there any specific control that I might have hidden (placed them at bottom of the template in hidden panel anything not wanted to display on interface) ... that could be affecting saving of content ?
  • ggalipeau
    Hey Anita,
    I do the hidden panel thing all the time, so I don't think that is your issue. Unfortunately, I have never seen this issue before. A good place to ask questions to get maximum value is through the msdn forumns. There is a forumn specifically for designer located here: http://social.msdn.microsoft.com/forums/en-US/s...
blog comments powered by Disqus


SharePoint Magazine

SharePoint User Experience Week on SharePoint Magazine

Technical

SharePoint Farm configuring and deployment Part 6 - Post Deployment

Products

Review: Workflows with Nintex Workflow 2007

People

SharePoint Magazine chats with Paul Culmsee