Creating A Default Custom MOSS Publishing Page Feature(Page Template)
When working with MOSS publishing sites its often a requirement to create a new custom publishing page and also set a new instance of that page as the default page for the site. In this exercise we're going to create a new feature that will cover the steps needed to accomplished this.
Step 1: Create the Feature that will handle all the necessary plumbing:
<Feature xmlns="http://schemas.microsoft.com/sharepoint/" Id="9C8E73EA-C313-49e3-A8EB-365EF5251AB9" Title="Custom MOSS Publishing Infrastructure" Description="Custom MOSS Publishing Infrastructure" Scope="Site" Hidden="False" Version="1.0.0.0" ReceiverAssembly="YourAssembly, Version=1.0.0.0, Culture=neutral,PublicKeyToken=a18ab9ca6202044d" ReceiverClass="YourAssemblylFeatureReceiver"> <ElementManifests> <ElementManifest Location="SiteColumns.xml"/> <ElementManifest Location="ContentTypes.xml"/> <ElementManifest Location="ProvisionFiles.xml" /> </ElementManifests> </Feature>
Step 2: Create the custom Site Columns that will be used in the custom publishing page (SiteColumn.xml):
In our scenario we've added a couple new site columns that will be used in the new page template.
<Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <Field Type="HTML" DisplayName="Introduction" RichText="TRUE" RichTextMode="FullHtml" Required="FALSE" Group="Custom Page Column" ID="{89AEE769-5F51-4608-B389-50C1B36C4FA8}" StaticName="Introduction" Name="Introduction" /> <Field Type="Image" DisplayName="WelcomeImage" RichText="TRUE" RichTextMode="FullHtml" Required="FALSE" Group="Custom Page Column" ID="{49500A41-5AD3-44b8-BC52-FF19B4AF4888}" StaticName="WelcomeImage" Name="WelcomeImage" /> </Elements>
Step 3: Create the ContentType that will be used by the custom page (ContentTypes.xml):
Defining the custom content type is pretty straight forward. This particular content type ID is the base page content type id plus our custom portion of the ID which are separated by 00. I personally use a GUID with the hyphen's removed to ensure uniqueness although this makes for often very long content type id. 0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900916CECA7C77446059633C4287903AA2A (red text is the base page content type, green is my custom id added, 00 is used as delimiter). For more information on defining content types check out the MSDN article at http://msdn2.microsoft.com/en-us/library/aa543822.aspx .
<Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <ContentType ID="0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900916CECA7C77446059633C4287903AA2A" Name="CustomWelcomePageTemplate" Description="Custom Home Page Template" Group="Custom Pages"> <FieldRefs> <FieldRef ID="89AEE769-5F51-4608-B389-50C1B36C4FA8" Name="Introduction" DisplayName="Introduction" ShowInDisplayForm="TRUE" ShowInFileDlg="TRUE" ShowInListSettings="TRUE" ShowInNewForm="TRUE" ShowInEditForm="TRUE" ReadOnlyClient="FALSE" /> <FieldRef ID="49500A41-5AD3-44b8-BC52-FF19B4AF4888" Name="WelcomeImage" DisplayName="Welcome Image" ShowInDisplayForm="TRUE" ShowInFileDlg="TRUE" ShowInListSettings="TRUE" ShowInNewForm="TRUE" ShowInEditForm="TRUE" ReadOnlyClient="FALSE" /> </FieldRefs> </ContentType> </Elements>
Step 4: Create the custom page template (CustompageLayout.aspx):
This is one possible template that makes use of the new site columns plus one of the base columns (Title).
<%@ Page language="C#" Inherits="Microsoft.SharePoint.Publishing.PublishingLayoutPage,
Microsoft.SharePoint.Publishing,Version=12.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c"
meta:webpartpageexpansion="full" meta:progid="SharePoint.WebPartPage.Document" %>
<%@ Register Tagprefix="SharePointWebControls" Namespace="Microsoft.SharePoint.WebControls"
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" %>
<asp:Content ContentPlaceholderID="PlaceHolderPageTitle" runat="server">
<SharePointWebControls:FieldValue id="PageTitle" FieldName="Title" runat="server"/>
</asp:Content>
<asp:Content ContentPlaceholderID="PlaceHolderMain" runat="server">
<div id="WelcomePageImage">
<PublishingWebControls:RichImageField ID="RichImageField1" FieldName="WelcomeImage" runat="server"></PublishingWebControls:RichImageField>
</div>
<div id="WelcomePageContent">
<PublishingWebControls:RichHtmlField ID="RichHtmlField1" FieldName="Introduction" runat="server"></PublishingWebControls:RichHtmlField>
</div>
</div>
</asp:Content>
Step 5: Provision the page template (ProvisionFiles.xml):
<Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <Module Name="PageTemplates" Url="_catalogs/masterpage" Path="Files" RootWebOnly="TRUE"> <File Url="CustomPageLayout.aspx" Type="GhostableInLibrary" IgnoreIfAlreadyExists="true"> <Property Name="ContentType" Value="$Resources:cmscore, contenttype_pagelayout_name;" /> <Property Name="PublishingAssociatedContentType"
Value=";#CustomWelcomePageTemplate;#0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900916CECA7C77446059633C4287903AA2A;#" /> </Module> </Elements>
Step 6: Create a Feature Receiver to create default page instance and setting the default welcome page:
Place this function inside your feature receiver assembly.
public override void FeatureActivated(SPFeatureReceiverProperties properties) { using (SPWeb web = properties.Feature.Parent as SPWeb) { //Create & Set the default page if (PublishingWeb.IsPublishingWeb(web)) { //Get references to the publishing site PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web); PublishingSite site = new PublishingSite(web.Site); //ensure that the page isn't present yet (in case the feature was activated and deactivated) if (publishingWeb.GetPublishingPages()["Pages/welcome.aspx"] == null) { //Create the default page SPContentTypeId contentTypeID =
new SPContentTypeId("0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900916CECA7C77446059633C4287903AA2A"); PageLayout[] layouts = publishingWeb.GetAvailablePageLayouts(contentTypeID); PageLayout welcomePageLayout = layouts[0]; PublishingPage welcomePage = publishingWeb.GetPublishingPages().Add("welcome.aspx", welcomePageLayout);
welcomePage.ListItem["Introduction"] = "This is some introduction default text"; welcomePage.Update(); //Set the default page SPFile welcomeFile = web.GetFile(welcomePage.Url); publishingWeb.DefaultPage = welcomeFile; publishingWeb.Update(); } } } }
This posting is provided "AS IS" with no warranties, and confers no rights.
浙公网安备 33010602011771号