Monday 4 June 2012

Custom Page Layout with Content Type

How to Create a Custom Page Layout with it's own Content Type while displaying the Meta Data as Page Content.

First Step Create some site columns that you're going to use inside your content type:
<?xml version="1.0" encoding="utf-8"?>
<!-- Custom Site Columns -->
<Elements xmlns="">
  <Field ID ="{F8B5DA47-CC24-498B-8D80-8B2AC7774AB2}"
       Name ="Custom_EffectiveDate"
       DisplayName ="Effective Date"
       Type ="DateTime"
       Group="Custom Site Columns"
       Format="DateOnly" />
  <Field ID="{B18BC51D-3ADF-4EBD-8AE7-924EF70252CA}"
       Name ="Custom_Opinion"
       Group="Custom Site Columns"/>
  <Field ID ="{57C17546-3D1E-485B-9F8F-9B61E1544245}"
       Name ="Custom_Priority"
       DisplayName ="Priority"
       Group="Custom Site Columns">
Once you have your Site Columns set up, it's time to create your content type:
Make sure you inherit from type Page.
With your content type created, just add your Custom FieldRefs from your custom site columns:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="">
  <!-- Parent ContentType: Page (0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF39) -->
  <ContentType ID="0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900d1ea0c66dfaf42bc9ae1d6475d2e5161"
               Group="Custom Content Types"
               Description="My Content Type"
      <FieldRef ID ="{F8B5DA47-CC24-498B-8D80-8B2AC7774AB2}" Name ="Custom_EffectiveDate"/>
      <FieldRef ID ="{B18BC51D-3ADF-4EBD-8AE7-924EF70252CA}" Name ="Custom_Opinion"/>
      <FieldRef ID ="{57C17546-3D1E-485B-9F8F-9B61E1544245}" Name ="Custom_Priority"/>
With your custom content type ready it's time to make your page layout, this is more or less a three part effort
  • add a module
  • change sample.txt to pagelayout.aspx
  • provision the page with the elements file 
To get started lets add a module, navigate in your solution explorer to where you want to add your page layout and hit (ctrl+shift+A)
make sure you have SharePoint 2010 selected in the left hand pane, Module selected in the middle Pane, then give your page layout a unique yet descriptive name; finally hit the add button.

Inside your module you should have an elements.xml and a sample.txt file.
First open the sample.txt file and paste the following code over top the sample text

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" 
             Assembly="Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" 
             Assembly="Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="PublishingWebControls" Namespace="Microsoft.SharePoint.Publishing.WebControls" 
             Assembly="Microsoft.SharePoint.Publishing, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="PublishingNavigation" Namespace="Microsoft.SharePoint.Publishing.Navigation" 
             Assembly="Microsoft.SharePoint.Publishing, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Page language="C#" masterpagefile="~masterurl/custom.master"
         Inherits="Microsoft.SharePoint.Publishing.PublishingLayoutPage,Microsoft.SharePoint.Publishing,Version=,Culture=neutral,PublicKeyToken=71e9bce111e9429c" %>
<asp:Content ContentPlaceholderID="PlaceHolderPageTitle" runat="server">
 <SharePoint:FieldValue id="PageTitle" FieldName="Title" runat="server"/>
<asp:Content ContentPlaceholderID="PlaceHolderMain" runat="server">

Now rename your sample.txt file to an .aspx one, I named mine  layout01.aspx.

With your files renamed you are now ready to design your actual Page Layout; Inside the Main Content Place holder add your Layout HTML.

<asp:Content ContentPlaceholderID="PlaceHolderMain" runat="server">
    <div id="PageContent">    
        <table id="DetailsTable">
                <SharePoint:TextField ID="TitleField" FieldName="Title" runat="server" />
                <SharePoint:DropDownChoiceField ID="PolicyProcedureField" FieldName="Custom_Priority" runat="server" />
                <td class = "FirstColumn" >Effective Date: </td>
                <td class = "SecondColumn"><SharePoint:DateTimeField ID="Custom_EffectiveDate" 
                    FieldName="Custom_EffectiveDate" runat="server" /></td>
                <td class = "ThirdColumn"><SharePoint:NoteField ID = "DescriptionField" 
                    FieldName="Custom_Opinion" runat = "server" /></td>
 <div id="MainContent">
   <PublishingWebControls:RichHtmlField FieldName="PublishingPageContent" PrefixStyleSheet="ccw-" 
                                                      HasInitialFocus="True" MinimumEditHeight="400px" runat="server"/>
    <div id = "RelatedColumn">
        <WebPartPages:WebPartZone ID="MainContentWebPart" Title="Web Part Zone" runat="server" FrameType="TitleBarOnly">
I've created simple layout that consists of an area to fill in your meta data a place to put your content and finally a web part zone if you wanted to add a web part.

Next you have to setup the elemets file to deploy your Page Laout, for this Open the Elemets.xml file
Above is what you should have as default we are going to make the following changes:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="">
  <Module Name="CustomPageLayout" Url="_catalogs/masterpage">
    <File Path="CustomPageLayout\layout01.aspx" Url="layout01.aspx" Type="GhostableInLibrary" IgnoreIfAlreadyExists="TRUE">
      <Property Name="Title" Value="Policy page Layout" />
      <Property Name="MasterPageDescription" Value="This page layout For Policy Pages" />
      <Property Name="ContentType" Value="$Resources:cmscore,contenttype_pagelayout_name;" />
      <Property Name="PublishingPreviewImage" Value="~SiteCollection/_catalogs/masterpage/$Resources:core,Culture;/Preview Images/DefaultPageLayout.png, ~SiteCollection/_catalogs/masterpage/$Resources:core,Culture;/Preview Images/DefaultPageLayout.png" />
      <Property Name="PublishingAssociatedContentType" Value=";#CustomPage;#0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900fdd825c8803c43d1bd98bd0306fa8971;#" />
Notice that in the Property PublishingAssociatedContentType we get the values from our Custom Content type 
at the start of this post. 
Before you deploy, go into the feature of your project and make sure that you provision:
  • site columns
  • content type
  • page layout 
it should resemble the following

deploy your project and all should be well.