MOSS 2007 入门(3) : 建立一个最精简的母版页
本文翻译自MSDN : Create a Minimal Master Page .
当你配置一个MOSS站点时,第一步中通常会建立一个或多个母版页。使用母版页可以包含很多MOSS站点中的元素,例如导航栏、搜索文本框、登陆控件等等,也包含了对定义站点统一样式的CSS文件。MOSS的站点和站点中所有的页面都是通过母版页来实现统一的用户体现和风格。根据你的需要,也可以对站点集中不同的站点使用不同的母版页来区分每个站点所处于门户中的区域。
当你在MOSS中定义一个站点级时,系统会自动地创建一个母版页库包含了所有的母版页和站点集的页面布局。如果这个站点集用到了发布门户或是协作门户模板,则母版页库中会包含几个不同的母版页,你可以选择使用不同的母版页使站点看起来更加个性化。
在MOSS页面定制中建立母版页是最重要的第一步,它将花费很多时间才能完成。本文将讲述如何建立一个满足MOSS要求的最精简功能的母版页,后面的代码会提供一个仅能保证MOSS页面控件正常布局的母版页。
你可以随便的建立一个空的母版页,但是我们不推荐这样做,因为一个完全空白的母版页中并不包含能使MOSS页面模型正常工作的Content PlaceHolder。而下面的示例代码中会包含所有必须的Content PlaceHolder。
你可以使用记事本,类似于Sharepoint 2007 Designer的Web编辑器,类似于VS 2005的IDE来编辑母版页,本文使用的是Sharepoint 2007 Designer 。
MOSS 要求一个母版页必须包含标题、图标、登陆、搜索等区域和一些基础结构元素包括页面区域、分格栏、边框、控制台和描述文字PlaceHolder。
MOSS提供的母版页需要基于WSS的SPWeb类下的SPWeb.CustomMasterUrl。
建立一个最精简的母版页步骤如下。
step(1): 打开Sharepoint Designer.
step(2): 在文件菜单点击New,选择Sharepiont Content,选择Page 标签。
step(3): 双击母版页来创建一个母版页。
step(4): 选择在设计模式下查看母版页,母版页覆盖了头部和左边的一些区域,几个content placeholders是可见的。
step(5): 选择在代码模式下查看母版页。
step(6): 把下面的代码拷贝并粘贴到母板页中。
<%@ 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 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, MOSS 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 the page content should go 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 are only necessary to ensure all out of the box MOSS 2007 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:Panel>
</form>
</body>
</html>
step(7): 将文件另存为一个.master后缀名的母版页文件到站点集的母版页库(/_catalogs/masterpage)。
翻译说明:实际上在下面的visible=false的pannel中的placeholder都是可选的,根据需要设置这些placeholder的可见性就可以,如果就是一个空白的站点,那么这些placeholder一个都不需要。但要保证其它的结构是正确的。