Tuesday, April 19, 2011

SharePoint Custom Publishing Layout feature

Scenario:
I am surprise that I have used this so many times but never blogged this. We often need a custom content type and custom page layout.

Solution:
Here's quick snippet of Element manifest.

Content Type Provisioning Element Manifest:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<!-- Parent ContentType: Page (0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF39) -->
<ContentType ID="0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900New_GUID_Here"
Name="Custom Page"
Description="Custom Page is the default content type for any Publishing Pages."
Group="SKN"
Inherits="TRUE"
Version="0">

<FieldRefs>
<FieldRef ID="{GetGUIDFromBelow}" Name="CustomColumn" />
</FieldRefs>
</ContentType>

<Field ID="{NewGUID}"
Name="CustomColumn"
DisplayName="Custom Column"
Type="Text"
Group="SKN">
<Default></Default>
</Field>

</Elements>
Page Layout Provisioning (with Default WP) Element Manifest:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Module Name="CustomPageLayouts" Url="_catalogs/masterpage" Path="PageLayouts" RootWebOnly="TRUE">
<File Url="CustomLayoutPage.aspx" Type="GhostableInLibrary">
<Property Name="Title" Value="Custom Page Layout Blank" />
<Property Name="MasterPageDescription" Value="$Resources:cmscore,PageLayout_BlankWebPartPage_Description;" />
<Property Name="ContentType" Value="$Resources:cmscore,contenttype_pagelayout_name;" />
<Property Name="PublishingPreviewImage" Value="~SiteCollection/_catalogs/masterpage/$Resources:core,Culture;/Preview Images/BlankWebPartPage.png, ~SiteCollection/_catalogs/masterpage/$Resources:core,Culture;/Preview Images/BlankWebPartPage.png" />
<Property Name="PublishingAssociatedContentType" Value=";#WH Page;#SAME_AS_CONTENT_TYPE_ID_ABOVE;#" />

<AllUsersWebPart WebPartOrder="0" WebPartZoneID="Header">
<![CDATA[<webParts>
<webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
<metaData>
<type name="CustomCode.DefaultWp, CustomCode, Version=1.0.0.0, Culture=neutral, PublicKeyToken=abcdcc1c43254ae6" />
<importErrorMessage>Cannot import Default WP Web Part.</importErrorMessage>
</metaData>
<data>
<properties>
<property name="Title" type="string">Default Information</property>
<property name="Description" type="string">Description for Default WP</property>
</properties>
</data>
</webPart>
</webParts>]]>
</AllUsersWebPart>

</File>
</Module>
</Elements>
Content Type Association Element Manifest:
  <ContentTypeBinding ContentTypeId="CONTENT_TYPE_ID_FROM_ABOVE" ListUrl="Pages" />
New Page Provisioning using Layout ( with Test WP ) Element Manifest:
 <File Path="SampleBlankWPPage.aspx" Url="Home.aspx" Type="GhostableInLibrary" NavBarHome="True">
<Property Name="Title" Value="Custom Home" />
<Property Name="PublishingPageLayout" Value="~SiteCollection/_catalogs/masterpage/CustomLayoutPage.aspx, Custom Layout Page" />
<Property Name="ContentType" Value="Custom Page" />

<AllUsersWebPart WebPartOrder="0" WebPartZoneID="Header">
<![CDATA[<webParts>
<webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
<metaData>
<type name="CustomCode.TestWp, CustomCode, Version=1.0.0.0, Culture=neutral, PublicKeyToken=abcdcc1c43254ae6" />
<importErrorMessage>Cannot import Test WP Web Part.</importErrorMessage>
</metaData>
<data>
<properties>
<property name="Title" type="string">Test Information</property>
<property name="Description" type="string">Description for Test WP</property>
</properties>
</data>
</webPart>
</webParts>]]>
</AllUsersWebPart>
</File>

1 comments:

Clayton DSouza,  November 1, 2011 at 2:44 AM  

In case of the following error during provisioning : "Cannot recognize the XML namespace of this Web Part"

Add the tag in the v3-version in the CDATA section.
In the v2-version declaration is enough in the CDATA section.