Pages

Search This Blog

Tuesday, February 21, 2012

Create custom page layouts using Custom content type using CAML

Requirement:
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.

Scenario:
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.

Solution:
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"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <!-- Parent ContentType: Article Page (0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D) -->
  <ContentType ID="0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D00f2f3fc2629204643b44911a79fa95814"
               Name="MyCustomContentType"
               Group="Custom Content Types"
               Description="My Custom Content Type"
               Inherits="TRUE"
               Version="0">
    <FieldRefs>
    </FieldRefs>
  </ContentType>
</Elements>
  • 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:
<!--[if gte mso 9]> <![endif][if gte mso 9]> Normal 0 false false false EN-US X-NONE X-NONE <![endif][if gte mso 9]> <![endif][if gte mso 10]> <!--><?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <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>
    <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;#" />
    </File>
</Module>
</Elements>
  •  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.

No comments:

Post a Comment