如何:创建最简单的母版页

配置 Microsoft Office SharePoint Server 2007 网站时必须完成的首要任务之一就是创建一个或多个母版页。母版页包含对可以在 Office SharePoint Server 2007 网站上多个网页之间共享的元素(如导航、搜索控件、登录控件和横幅图像)的引用。母版页还可包含用于定义网站总体外观的级联样式表 (CSS) 和 ECMAScript(JScript、JavaScript)引用。通常,网站集中的每个网站(因此每个网页)都使用相同的母版页,以便在整个网站集中呈现一致的用户体验。根据需要,可以对网站层次结构中的一个或所有网站使用不同的母版页,从而区分门户的各个区域。

母版页样式库

在 Office SharePoint Server 2007 中设置网站集时,系统将创建包含该网站集所有母版页和页面布局的母版页样式库。如果网站集使用发布门户或协作门户模板,则母版页样式库将包括随 Office SharePoint Server 2007 一起提供的若干母版页(如 BlueBand.master)。这些母版页位于 C:\Program%20Files\Common%20Files\Microsoft%20Shared\web%20server%20extensions\12\TEMPLATE\FEATURES\PublishingLayouts\MasterPages\ 路径中,其中还有其他示例 .master 页。可以按原样使用这些母版页,也可以完全自定义这些母版页,为网站创造独一无二的外观方案。

从最简单的母版页开始的原因

为了开始自定义 SharePoint 网站而创建和完成母版页的过程需要进行规划,并要花费一定时间。如果可以,要防止重写或取消母版页中不需要的代码。本主题介绍如何创建仅包括 Office SharePoint Server 2007 所需的最少功能的最简单 母版页,以建立一个稳定的平台,在上面生成自己的母版页。创建最简单的母版页有助于避免出现以下这些花费时间过多的过程:从预先存在的 .master 页(如 BlueBand.master)取消代码,或在自定义需求再次变化时删除功能然后重新生成该功能。

备注

本主题支持使用所述的最简单母版页作为 Office SharePoint Server 2007 中的网站母版页,不支持使用本主题中所述的最简单母版页作为 Office SharePoint Server 2007 中的系统母版页。不明确支持将此内容用于 Windows SharePoint Services 3.0。

当然,可以从头开始创建母版页。但是,一般建议不要这样做,因为真正空白的母版页不包括 Office SharePoint Server 2007 页模型正常运行所需的所有内容占位符。

以下过程中的示例代码仅包括 Office SharePoint Server 2007 页模型所需的内容—使用 Office SharePoint Server 2007 的默认安装中包括的页面布局所必需的内容占位符和控件。Office SharePoint Server 2007 要求母版页中包括标题、品牌打造、登录功能、搜索功能、痕迹功能和基本结构元素(如页面区域、分隔符、边框、控制台和说明占位符)。

以下过程使用 Office SharePoint Designer 2007 作为母版页设计环境。此外,还可以使用文本编辑器、Web 编辑器(如 Microsoft Office SharePoint Designer 2007)或集成开发环境 (IDE)(如 Microsoft Visual Studio 2005)创建母版页。

Office SharePoint Server 2007 随附的母版页基于 Windows SharePoint Services 中 SPWeb 类的 SPWeb.CustomMasterUrl 属性。

创建最简单的母版页

  1. 打开 SharePoint Designer。

  2. 在“文件”菜单上单击“新建”,指向“SharePoint 内容”,然后单击“页面”选项卡。

  3. 双击“母版页”创建新母版页。

  4. 单击“设计”以设计视图显示母版页。在母版页中应该看到页眉和左空白区以及若干内容占位符。

  5. 单击“代码”以代码视图显示母版页。

  6. 将以下代码复制到母版页中。

    <%-- Identifies this page as a .master page written in Microsoft Visual C# and registers tag prefixes, namespaces, assemblies, and controls. --%>
    <%@ Master language="C#" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <%@ Import Namespace="Microsoft.SharePoint" %>
    <%@ Register Tagprefix="SPSWC" Namespace="Microsoft.SharePoint.Portal.WebControls" Assembly="Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
    <%@ Register Tagprefix="SharePoint" 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="wssuc" TagName="Welcome" src="~/_controltemplates/Welcome.ascx" %>
    <%@ Register TagPrefix="wssuc" TagName="DesignModeConsole" src="~/_controltemplates/DesignModeConsole.ascx" %>
    <%@ Register TagPrefix="PublishingVariations" TagName="VariationsLabelMenu" src="~/_controltemplates/VariationsLabelMenu.ascx" %>
    <%@ Register Tagprefix="PublishingConsole" TagName="Console" src="~/_controltemplates/PublishingConsole.ascx" %>
    <%@ Register TagPrefix="PublishingSiteAction" TagName="SiteActionMenu" src="~/_controltemplates/PublishingActionMenu.ascx" %>
    <%-- Uses the Microsoft Office namespace and schema. --%>
    <html>
      <WebPartPages:SPWebPartManager runat="server"/>
      <SharePoint:RobotsMetaTag runat="server"/>
    
      <%-- The head section includes a content placeholder for the page title and links to CSS and ECMAScript (JScript, JavaScript) files that run on the server. --%>
      <head runat="server">
        <asp:ContentPlaceHolder runat="server" id="head">
          <title>
            <asp:ContentPlaceHolder id="PlaceHolderPageTitle" runat="server" />
          </title>
        </asp:ContentPlaceHolder>
        <Sharepoint:CssLink runat="server"/>
        <asp:ContentPlaceHolder id="PlaceHolderAdditionalPageHead" runat="server" />
      </head>
    
      <%-- When loading the body of the .master page, SharePoint Server 2007 also loads the SpBodyOnLoadWrapper class. This class handles .js calls for the master page. --%>
      <body onload="javascript:_spBodyOnLoadWrapper();">
        <%-- The SPWebPartManager manages all of the Web part controls, functionality, and events that occur on a Web page. --%>
        <form runat="server" onsubmit="return _spFormOnSubmitWrapper();">
          <wssuc:Welcome id="explitLogout" runat="server"/>
          <PublishingSiteAction:SiteActionMenu runat="server"/>  
          <PublishingWebControls:AuthoringContainer id="authoringcontrols" runat="server">
            <PublishingConsole:Console runat="server" />
          </PublishingWebControls:AuthoringContainer>
          <%-- The PlaceHolderMain content placeholder defines where to place the page content for all the content from the page layout. The page layout can overwrite any content placeholder from the master page. Example: The PlaceHolderLeftNavBar can overwrite the left navigation bar. --%>
          <asp:ContentPlaceHolder id="PlaceHolderMain" runat="server" />
            <asp:Panel visible="false" runat="server">
            <%-- These ContentPlaceHolders ensure all default SharePoint Server pages render with this master page. If the system master page is set to any default master page, the only content placeholders required are those that are overridden by your page layouts. --%>
    <asp:ContentPlaceHolder id="PlaceHolderSearchArea" runat="server"/>
    <asp:ContentPlaceHolder id="PlaceHolderTitleBreadcrumb" runat="server"/>
    <asp:ContentPlaceHolder id="PlaceHolderPageTitleInTitleArea"  runat="server"/>
    <asp:ContentPlaceHolder id="PlaceHolderLeftNavBar" runat="server"/>
    <asp:ContentPlaceHolder ID="PlaceHolderPageImage" runat="server"/>
    <asp:ContentPlaceHolder ID="PlaceHolderBodyLeftBorder" runat="server"/>
    <asp:ContentPlaceHolder ID="PlaceHolderNavSpacer" runat="server"/>
    <asp:ContentPlaceHolder ID="PlaceHolderTitleLeftBorder" runat="server"/>
    <asp:ContentPlaceHolder ID="PlaceHolderTitleAreaSeparator" runat="server"/>
    <asp:ContentPlaceHolder ID="PlaceHolderMiniConsole" runat="server"/>
    <asp:ContentPlaceHolder id="PlaceHolderCalendarNavigator" runat ="server" />
    <asp:ContentPlaceHolder id="PlaceHolderLeftActions" runat ="server"/>
    <asp:ContentPlaceHolder id="PlaceHolderPageDescription" runat ="server"/>
    <asp:ContentPlaceHolder id="PlaceHolderBodyAreaClass" runat ="server"/>
    <asp:ContentPlaceHolder id="PlaceHolderTitleAreaClass" runat ="server"/>
    <asp:ContentPlaceHolder id="PlaceHolderBodyRightMargin" runat="server" />
    </asp:Panel>
        </form>
      </body>
    </html>
    
  7. 在“文件”菜单上单击“另存为”,提供扩展名为 .master 的唯一文件名,然后将文件保存到网站集中的母版页样式库 (/_catalogs/masterpage)。

See Also

概念

页面布局和母版页

页面布局模型