Automatic SharePoint 2010 webpart provisioning right into a page layout

fix

Thursday, April 8th, 2010

If  you worked with MOSS or SharePoint 2010 publishing template – you know that you can provision page layouts that can be used by other pages to conform to page layout zones. However, all of the content, such as webparts and views, you’d use your page  XML file and provision definitions in there.

Here is the scenario: you need to let users create a new pages of a certain type – say products page, that will have few webparts provisioned to it by default.

Here are some options: Well, since your page is a list item (not subsite with a page) you could trigger an event handler that will provision your webpart on the page as XML; however there is a simpler approach. Embed your webpart definition right onto a page layout so that every time the page of that layout is created – you get a basic set of webparts you always want there.

Here is how to do it. When you provision your custom page layouts to the site using a Site scoped feature, you define an XML file that says where would each page layout go to, here is a sample:

<?xml version=”1.0″ encoding=”utf-8″ ?>
<Elements xmlns=”http://schemas.microsoft.com/sharepoint/“>

  <!– page layouts –>
  <Module Name=”PageLayouts” Url=”_catalogs/masterpage” Path=”PageLayouts” RootWebOnly=”TRUE”>

    <File Url=”MyPublishingPage.aspx” Type=”GhostableInLibrary”>
      <Property Name=”Title” Value=”My Publishing Page Layout” />
      <Property Name=”MasterPageDescription” Value=”Generic Publishing Page Layout with 2 Columns, Navigation on Left” />
      <Property Name=”ContentType” Value=”MyPublishingPage” />
      <Property Name=”PublishingAssociatedContentType” Value=”;#MyPublishingPage;#0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900D77FE7A4A3034DB1B096C291194075CDAA;#” />
    </File>
</Module>
</Elements>

Well, to provision your webpart into this custom page layout you would insert it’s XML right after the <Property> definition like this:

<AllUsersWebPart WebPartZoneID=”MyWebpartZone” WebPartOrder=”1″>
<![CDATA[
...
]]>
</AllUsersWebPart>

That’s it – the webpart will be provisioned for every instance the page is created. Enjoy!

news

Any information posted on this blog does not reflect views of respective product vendors unless explicitely stated.

featured