How to create your own master page ,Include a example

最近在做sharepoint项目.项目中要求使用custome master page.于是就在网上找了很多资料.通过资料我们可以知道有两种方式制作masterpage,一种是通过sharepoint designer来开发(好像不便于deploy),另一种是通过feature.我们这里讲通过Feature的方式发布Masterpage.

Solutions:
1.First you can create a minimal master page Just includes all the share point server 2007 page model content placeholders (a empty master page can’t work well)
The following procedure includes all the basic content placeholders ,layouts ,controls that share point server 2007 can work .A default master page includes a title, branding ,logon functionality ,search functionality ,breadcrumb functionality ,and basic structural elements such as page areas ,separators ,borders ,consoles and description placeholders .

The master pages included with Office SharePoint Server 2007 are based on the SPWeb.CustomMasterUrl property of the SPWeb class in Windows SharePoint Services.

To create a minimal master page


1.    
Open SharePoint Designer.

2.     On the File menu, click New, point to SharePoint Content, and then click the Page tab.

3.     Double-click Master Page to create a new master page.

4.     Click Design to show the master page in design view. You should see header and left margin areas and several content placeholders in the master page.

5.     Click Code to show the master page in code view.

6.     Copy the following code into the master page.

Xml

Copy Code

<%-- 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.     On the File menu, click Save As, provide a unique file name with the .master extension, and then save the file to the master page gallery (/_catalogs/masterpage) in your site collection.

Follow this article you can work out a master page
This article refers to Microsoft Office SharePoint Server 2007 (MOSS 2007) Beta 2 Tech Refresh.   Details are subject to change in the RTM version.

One way master pages can be stored and used in MOSS 2007 sites is through creation in SharePoint Designer and storage in the Master Page Gallery.   This method will create a master page in the content database.  But what if you need to use one or two master page across multiple site collections?  For ease of updates and maintenance, we don't necessarily want to store a copy of the master page in each site collection. Instead we can create and store master pages on the file system as a SharePoint Feature and make it available for new and existing site collections.

If for some reason you don't want to create a custom Feature, you can manually work through this process by editing the default Publishing Layouts Feature that ships with SharePoint. See this article here for instructions.

A huge thanks to my friend Andrew Connell for showing me the light on how to accomplish this with a custom feature. His help was invaluable.

Create a Feature: Add Custom Master Pages to your Site Collections

  1. Navigate to the Features directory on your web server:
    Local Drive:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES
  2. Locate the PublishingLayouts Feature directory.  Copy this folder and paste it at the root of Features.  Rename the folder to a unique name of your choice.  For my example I used CustomMasterPages.
    Location of custom feature folder
  3. Open your new Feature directory. At this point you can leave all the default folders, or you can choose to clear out what you don't want to use. You only have to keep Feature.xml and ProvisionedFiles.xml. To create a clean file set, at a minimum clear out the contents of each sub folder in the directory (en-us (or your language), Images, MasterPages, PageLayouts, Styles).
  4. To start, we need at least one master page file in the Feature folder. You can copy one of the existing master pages, paste the copy in the directory, and rename the copy to your unique name, or alternately paste in your custom master page, or create a new blank file with the extension of .master.
    Feature folder contents

    Tip! To just get a master page going, copy the sample code from this article and paste it in your custom master page file.  That will get you up and running for this article and give you a good starting point for your customizations once the master page has been applied to your site.  If you do this, after pasting the code into your master page file, locate this line of code and add a missing quotation mark: <asp:ContentPlaceHolder id="PlaceHolderTitleBreadcrumb" runat="server"/>
  5. Next, open the ProvisionedFiles.xml file in Notepad or a similar editing application.
    Local Drive:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\YourFeature
  6. We are going to delete out all of the content and only keep the OSGMasterPages Module tag and the PublishingLayoutsPreviewImages Module tag.  As opposed to listing out what to strip out, here is the code you need in this file:
    <!-- _lcid="1033" _version="12.0.4407" _dal="1" -->
    <!-- _LocalBinding -->
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <Module Name="OSGMasterPages" Url="_catalogs/masterpage" Path="MasterPages" RootWebOnly="TRUE">
    <File Url="Sample.master" Type="GhostableInLibrary">
    <Property Name="ContentType" Value="My Sample Master Page" />
    <Property Name="PublishingPreviewImage" Value="~SiteCollection/_catalogs/masterpage/$Resources:core,Culture;/Preview Images/sample.png, ~SiteCollection/_catalogs/masterpage/$Resources:core,Culture;/Preview Images/sample.png" />
    <Property Name="MasterPageDescription" Value="This is my sample master page for use with collaboration or publishing sites." />
    </File>
    </Module>
    <Module Name="PublishingLayoutsPreviewImages" Url="_catalogs/masterpage" IncludeFolders="??-??" Path="" RootWebOnly="TRUE">
    <File Url="Sample.png" Name="Preview Images/Sample.png" Type="GhostableInLibrary">
    </File>
    </Module>
    </Elements>
  7. Update the properties to reflect the new master page details:
    1. Change the URL to your custom master page.:
      <File Url="Sample.master" Type="GhostableInLibrary">
    2. Change the Preview Images to a custom image:
      <Property Name="PublishingPreviewImage" Value="~SiteCollection/_catalogs/masterpage/$Resources:core,Culture;/Preview Images/Sample.png, ~SiteCollection/_catalogs/masterpage/$Resources:core,Culture;/Preview Images/Sample.png" />
      Add the custom image to the following directory:
      Local Drive:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\PublishingLayouts\en-us
    3. Update the Description.  Enter a description for your master page:
      <Property Name="MasterPageDescription" Value="This is my sample master page for use with collaboration or publishing sites." />
    4. Update the image names:
      <File Url="Sample.png" Name="Preview Images/Sample.png" Type="GhostableInLibrary">
  8. Save the ProvisionedFiles.xml file.
  9. Create a preview image in the language folder (in this case, en-us).
    Preview image location
  10. Next, open the Feature.xml file in Notepad or a similar editing application.
    Local Drive:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\YourFeature
  11. There are four things you need to edit in this file:
    1. Change the Feature ID to a unique GUID.  You can generate GUIDs in Visual Studio (Tools - Create GUID - Registry Format - Copy). Paste the new GUID for the ID and remove the curly brackets.
      <Feature Id="FDFCD4C4-F0D9-43b5-8739-A33681049657"
    2. Update the title:
      Title="Custom Master Pages"
    3. Update the description:
      Description="Custom master pages for use across multiple site collections."
    4. Change the Hidden state to False:
      Hidden="False"
  12. Save the Feature.xml file.
  13. Recycle the Application Pool for the site you plan to install the Feature on, or reset IIS (Command Prompt - IISRESET).
  14. You are now ready to install the new Feature on the web server. Open a Command Prompt and enter the following:
    1. Change directories to the BIN folder in the 12 hive so you can use STSADM.exe:
      cd C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN
    2. Launch STSADM and install the feature:
      stsadm -o installfeature -name YourFeatureDirectoryName
    Tip! To access STSADM quicker, refer to Andrew Connell's SharePoint Developer Tip.
  15. Now we will activate the feature on the site.
    1. Open your site and navigate to the Site Collection Features settings page. Site Actions - Site Settings - Modify All Site Settings - Site Collection Features (under the Site Collection Administration submenu).
    2. The new feature will be listed:
      Custom feature in Feature list
    3. Select Activate.
  16. The master pages are now ready to use on the site. Navigate to the Master Page settings by selecting Site Settings in the breadcrumbs and choosing Master Page. (Or Site Actions - Site Settings - Modify All Site Settings - Master page (under the Look and Feel submenu).
  17. Depending on your requirements, in either or both the Site Master Page setting and the System Master Page setting, select your drop down box and choose your custom master page. 
    Master page selection drop down
    1. Select OK.
  18. The master page is applied to the site.  Note that I manually added the "Sample Master Page" text to the Sample.master file. It will not appear on yours by default.
    Sample Master Page

 

Adding to our Feature: Adding More Custom Master Pages to your Feature

If you ever need to add new master pages to this feature, you can edit the Feature and redeploy it.  Alternately, you can create a new Feature for each custom master page. For that scenario, just follow the steps above for creating a new feature. The following outlines how to update your Feature with new master pages.

  1. Navigate to your custom master page Feature directory and add the appropriate files for your new master pages.
  2. Navigate to and open the ProvisionedFiles.xml file.  Add a FILE tag block for each new master page for both the OSGMasterPages Module and the PublishingLayoutPreviewImages Module.  Save the file.
  3. Recycle the Application Pool for the site you plan to install the Feature on, or reset IIS (Command Prompt - IISRESET).
  4. You are now ready to reinstall the updated Feature on the web server. Open a Command Prompt and enter the following:
    1. Change directories to the BIN folder in the 12 hive so you can use STSADM.exe:
      cd C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN
    2. Launch STSADM and install the feature:
      stsadm -o installfeature -name YourFeatureDirectoryName -force
    Tip! To access STSADM quicker, refer to Andrew Connell's SharePoint Developer Tip.
  5. Now we will deactivate and reactivate the feature on the site.
    1. Open your site and navigate to the Site Collection Features settings page. Site Actions - Site Settings - Modify All Site Settings - Site Collection Features (under the Site Collection Administration submenu).
    2. Your existing feature will be listed:
      View of Active Feature
    3. Select Deactivate.
    4. A warning message will appear.  Select Deactivate the feature.
    5. The Feature list will reload.  Select Activate next to your custom feature.
  6. Your new master pages are ready for use.

 

Use the following cmd to install and uninstall
stsadm –o installfeature –name yourfolder name (AACustomeMasterPage)
stsadm –o uninstallfeature –name yourfolder name (AACustomeMasterPage)

I do a example you can find it

Example

参考
1:How to create your own master page ,Include a example.
http://www.heathersolomon.com/blog/archive/2007/01/26/6153.aspx
http://msdn.microsoft.com/en-us/library/aa660698.aspx
http://www.heathersolomon.com/blog/articles/servermstpageforsitecollect_feature.aspx
http://msdn.microsoft.com/en-us/library/ms476046.aspx
Microsoft Website
http://msdn.microsoft.com/en-us/library/bb727372.aspx
SharePoint Magazine
http://sharepointmagazine.net/technical/customisation/developing-a-custom-master-page-master-pages-and-sharepoint-part-3-of-6
http://www.mindsharpblogs.com/PaulS/archive/2007/06/18/1903.aspx

posted @ 2009-01-22 16:37  莫贝特(MBetter)  阅读(1298)  评论(0编辑  收藏  举报