Create custom page layouts using Custom content type using CAML

At times, we come across a requirement to have custom page layouts and use those page layouts to create pages in the site. This needs to be done using CAML.
Lets take an example where you are creating a site definition for a customer, and the site should have a home page and some internal pages. Admin can create more internal pages with same layout as of the existing internal pages. In this scenario, we can create 2 custom page layouts within the site definition say Home Page & Inner Page, and then create new pages based on these page layouts.
Here is how we achieve this:
Open Visual Studio 2010 and create a new project (Empty SharePoint project)
Step 1:
Create Custom content type derived from Article page content type.
  •    Right click the project and click Add -> New Item
  • Select Content Type from the list.
  • Give a suitable name for the content type.
  • Click Add.
  • Then it will ask, which base content type should your custom content type be inherited from. Select Article page from the list (In our case, it is article page. This can be inherited from any other type based on requirements)
  • Click Finish.
  • A feature with a content type is created in your solution. Open the Elements.xml file and you can update the properties of the content type like Name, Group, Description.
  • Notice the Content type ID generated by Visual Studio. This is a unique ID and will be used when we create page layouts using this content type.
 <?xml version=1.0 encoding=utf-8?>
  <!– Parent ContentType: Article Page (0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D) –>
  <ContentType ID=0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D00f2f3fc2629204643b44911a79fa95814
               Group=Custom Content Types
               Description=My Custom Content Type
  • Now the content type is created and on deploying this, we can see out custom content type in the content type gallery on the site.
Step 2:
Create custom page layouts based on the custom content type:
  • Add a new module to the solution, and name it PageLayouts. 
  • Delete the Sample.txt file created automatically within the module.
  • Add 2 aspx pages (page layouts) with the name HomePage & InnerPage. The way to do this is by Clicking Add -> New Item -> Text File and then put the name as Home.aspx
  • Similarly add InnerPage.aspx.
  • Register the required namespaces and assemblies and then add web part zones on this page accordingly.
  • You can download these pages here.
  • Update the elements.xml file accordingly as shown below:

<?xml version=1.0 encoding=utf-8?>

  <Module Name=PageLayouts Url=_catalogs/masterpage>
    <File Path=PageLayouts\HomePage.aspx Url=HomePage.aspx Type=GhostableInLibrary >
      <Property Name=Title Value=Home Page />
      <Property Name=ContentType Value=$Resources:cmscore,contenttype_pagelayout_name; />
      <Property Name=PublishingPreviewImage Value=~SiteCollection/PublishingImages/HomePage.png />
      <Property Name=PublishingAssociatedContentType Value=;#$Resources:cmscore,contenttype_articlepage_name;;#0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D00f2f3fc2629204643b44911a79fa95814;# />
    <File Path=PageLayouts\InnerPage.aspx Url=InnerPage.aspx Type=GhostableInLibrary >
      <Property Name=Title Value=Inner Page />
      <Property Name=ContentType Value=$Resources:cmscore,contenttype_pagelayout_name; />
      <Property Name=PublishingPreviewImage Value=~SiteCollection/PublishingImages/InnerPage.png />
      <Property Name=PublishingAssociatedContentType Value=;#$Resources:cmscore,contenttype_articlepage_name;;#0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D00f2f3fc2629204643b44911a79fa95814;# />
  •  Deploy the solution. Activate the feature. Go to Master page and page layouts gallery. You can see HomePage and InnerPage Page layouts deployed there.

  • These custom page layouts can now be used to create new pages.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s