Tuesday, February 14, 2012

SharePoint 2010 Master Page Inheritance

Master page inheritance is useful in SharePoint sites where we need multiple master pages with the same global layout structure. For instance a SharePoint site with all sub-sites having a unique right column for each sib-site can use master page inheritance. Here one option is to copy the entire HTML into all the master pages, but it is not a good option if we want to modify something.

Following example promotes the reuse of the right column content within a site.



Parent Master Page (Site_Main.master)Site_Main.master is the base master page and it represents the main master page area. Site_Main.master contains the header/ footer content and three Content Place Holders for main content.

<%@ Master language="C#" %>
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="wssuc" TagName="Welcome" Src="~/_controltemplates/Welcome.ascx" %>
<%@ Register TagPrefix="wssuc" TagName="DesignModeConsole" Src="~/_controltemplates/DesignModeConsole.ascx" %>
<%@ Register TagPrefix="wssuc" TagName="MUISelector" Src="~/_controltemplates/MUISelector.ascx" %>
<%@ Register TagPrefix="PublishingNavigation" Namespace="Microsoft.SharePoint.Publishing.Navigation"
       Assembly="Microsoft.SharePoint.Publishing, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<html id="Html1" xmlns="http://www.w3.org/1999/xhtml" lang="<%$Resources:wss,language_value %>" dir="<%$Resources:wss,multipages_direction_dir_value %>" runat="server" __expr-val-dir="ltr">
<head id="Head1" runat="server">....head>
<body>
<form id="Form1" runat="server">
.
.
.
<div>
  <asp:ContentPlaceHolder id="PlaceHolderPageTitleInTitleArea" runat="server" />        
  <asp:ContentPlaceHolder id="PlaceHolderPageDescription" runat="server"/>
  <asp:ContentPlaceHolder id="PlaceHolderMain" runat="server"/>
<div>
.
.
.
<form>
<body>
<html>

Inherited Master Page (Site_Inner_x.master)Site_Inner_x.master represents the subsite master page area for a specific subsite. Subsite specific right hand side content is populated in these master pages. Here the main thing to notice is the MasterPageFile value, which is the actual reference to the base masterpage.

<%@ Master language="C#"  MasterPageFile="~SiteCollection/_catalogs/masterpage/Site_Main.master"%>

<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="SharePointWebControls" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="PublishingWebControls" Namespace="Microsoft.SharePoint.Publishing.WebControls" Assembly="Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="PublishingNavigation" Namespace="Microsoft.SharePoint.Publishing.Navigation" Assembly="Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register tagprefix="WebPartPages2" namespace="Microsoft.SharePoint.WebPartPages" assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<asp:Content ID="Content1" ContentPlaceholderID="PlaceHolderMain" runat="server">
  <asp:ContentPlaceHolder id="PlaceHolderMain" runat="server"/>
  <asp:ContentPlaceHolder id="RightWebPartArea" runat="server">
    <%--Adding a webpart--%>
    <WebPartPages2:ContentEditorWebPart ID="ContentEditorWebPart2" webpart="true" runat="server" __WebPartId="{BB74483B-C04F-4C48-BDD1-3C6D5F159266}">
      <WebPart xmlns="http://schemas.microsoft.com/WebPart/v2">
        <FrameType>NoneFrameType>
        <PartImageLarge>/_layouts/images/mscontl.gifPartImageLarge>
        <ID>g_47ae1a6a_638e_4874_8a91_e9482c5f63f0ID>
        <ContentLink xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor" />
        <Content xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor">
          [CDATA[<div class="webpart">
           <h3>Webpart 1 Titleh3>
              <div class="webpart_content">                                                             Webpart 1 content
              div>
              div>]]>
        Content>
        <PartStorage xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor" />
       <WebPart>
     <WebPartPages2:ContentEditorWebPart>
    <asp:ContentPlaceHolder>
<asp:Content>

<%--Adding all other place holders in the parent master--%>
<asp:Content runat="server" ID="PlaceHolderPageTitleInTitleAreaContent6" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea">
  <asp:ContentPlaceHolder id="PlaceHolderPageTitleInTitleArea" runat="server"></asp:ContentPlaceHolder>
<asp:Content>

<asp:Content runat="server" ID="PlaceHolderPageDescriptionContent2" ContentPlaceHolderID="PlaceHolderPageDescription">
  <asp:ContentPlaceHolder id="PlaceHolderPageDescription" runat="server"></asp:ContentPlaceHolder>
<asp:Content>


Page Layouts
PlaceHolderMain, PlaceHolderPageTitleInTitleArea and PlaceHolderPageDescription content areas will be overridden in page layouts. But RightWebPartArea will not be overridden in page layouts.

<%@ Page language="C#"   Inherits="Microsoft.SharePoint.Publishing.PublishingLayoutPage,Microsoft.SharePoint.Publishing,Version=12.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePointWebControls" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="PublishingWebControls" Namespace="Microsoft.SharePoint.Publishing.WebControls" Assembly="Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="PublishingNavigation" Namespace="Microsoft.SharePoint.Publishing.Navigation" Assembly="Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<asp:Content ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server">
  <SharePointWebControls:FieldValue id="PlaceHolderTitle" FieldName="Title" runat="server"/>
asp:Content>

<asp:Content ContentPlaceholderID="PlaceHolderPageDescription" runat="server">
  <SharePointWebControls:FieldValue id="PlaceHolderDescription" FieldName="Page Description" runat="server"/>
asp:Content>

<asp:Content ContentPlaceholderID="PlaceHolderMain" runat="server">
  <PublishingWebControls:editmodepanel runat="server" id="editmodepanel1">
    <SharePointWebControls:TextField FieldName="Title" id="txtPageTitle" runat="server">SharePointWebControls:TextField>
    <SharePointWebControls:NoteField FieldName="Page Description" id="txtDescription" DisplaySize="58" Text="" runat="server"/>
  PublishingWebControls:editmodepanel>

  <WebPartPages:webpartzone id="wpz1" runat="server" Visible="true"><ZoneTemplate>ZoneTemplate>WebPartPages:webpartzone>
asp:Content>

No comments: