Tuesday, May 26, 2009

Adding master page to a SharePoint web page

In SharePoint, when we add a new page from browser by going to Site Actions and select Create Page then there we will select the page layout and create the page. So we are selecting the page layout, its always binds to a master page. Whenever you change the default or custom master page, then the changes will be applied to all the page layouts by default. Because SharePoint won’t give direct master page url or name any where. it will be ~masterurl/custom.master for custom master page and ~masterurl/default.master for the default master page.

But when we add a new ASPX page from the SharePoint designer, there we are not having any option to select page layout and inherit it.  So, we will create a simple ASPX page where nothing is added by default to the page other than the head, body tags. When we try to add the master page to the current page then we need to follow some steps to apply master page to the current page. Below are the steps to follow to add master page to the aspx page we just added.

  • By default you will create the ASPX page in pages folder if your site is using publishing site template otherwise create page in the root of the web site.
  • You can create site by right click on the pages folder or on the root site and then select New –> ASPX page. The empty ASPX page when we add will look like as shown below.
  • Empty_ASPX_Page_No_Master
  • We need to perform below steps to add master page to the current page.

1. Add master page reference to the page.

2. Add Placeholders required to the page depends on the master page.

1. Adding master page reference to the page.

  • Now, we need to add the reference to the master page. We can do this by adding below tags.
  • For publishing site template pages, we need to add two entries.
  1. reference master page

    <%@ Reference VirtualPath="~masterurl/custom.master" %>

  2. Adding <%@ Page tag which will inherits from the publishing page.

    <%@ Page language="C#" Inherits="Microsoft.SharePoint.Publishing.PublishingLayoutPage,Microsoft.SharePoint.Publishing,Version=12.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" meta:progid="SharePoint.WebPartPage.Document" %>

  • But for team web site we need to add page directive as follows.

    <%@ Page language="C#" MasterPageFile="~masterurl/default.master"    Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage,Microsoft.SharePoint,Version=12.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" meta:progid="SharePoint.WebPartPage.Document" %>

2. Add Placeholders required to the page depends on the master page.

  • This is the very crucial and important step we need to perform. Because placeholders are not same for all site templates. This again will depends on the master page as well. If you customized the master page then you need to add placeholders depends on the placeholders you define.
  • Here i will tell you the main or default placeholders we need to add to a page.

    <asp:Content ContentPlaceHolderId="PlaceHolderPageTitle" runat="server">
    </asp:Content>
    <asp:Content ContentPlaceHolderId="PlaceHolderPageTitleInTitleArea" runat="server">
    </asp:Content>
    <asp:Content ContentPlaceHolderId="PlaceHolderTitleAreaClass" runat="server">
    </asp:Content>
    <asp:Content ContentPlaceHolderId="PlaceHolderPageDescription" runat="server">

    </asp:Content>
    <asp:Content ContentPlaceHolderId="PlaceHolderBodyRightMargin" runat="server"> </asp:Content>

    <asp:Content runat="server" ContentPlaceHolderID="PlaceHolderMain"></asp:Content>

Placeholder “PlaceHolderPageTitle” is for setting the page title of the current page.

Placeholder “PlaceHolderMain” is the main and important placeholder where we keep all the content on the page. Place all the content of your web page in this placeholder.

Note: This blog post is only for letting you know about how to convert sample ASPX page to SharePoint web page by applying the master page to it. All the placeholders and the content is not 100% match your master page. If you have any problems in applying any step then please provide a comment, so that i will look into it.

2 comments:

  1. There is no point of using aspx pages in that way on a SharePoint site by SharePoint Designer. The process you are describing is error prone because it is so such an easy thing to forget a contentplaceholder of some kind or some other part of needed kode.

    An editor should always create new SharePoint Content Page and select an allowed Page Layout type in that process. That way will ensure that all contentplaceholders and other needed code is present. They could also easly switch to Design mode and populate the newly page with some content.

    ReplyDelete
  2. This was very helpful. Saved me quite a few minutes. There is a point in using ASPX pages in the way you described. If you wish to create a page easily without having to interact with SharePoint Designer and you aren't doing anything specific to SharePoint in the first place!

    If you forget a placeholder, it's not a big deal. You can just add it!?

    ReplyDelete