Creating SharePoint Page Layouts as features in 2010
You have hacked together a proof of concept in SharePoint Designer, and now you need to start working for real in SharePoint features. In 2007, you had little choice other than knowing the syntax and working out the values for the associated content type.
What we are looking for is something like:
<File Path=”MyPageLayout.aspx” Url=”MyPageLayout.aspx” Type=”GhostableInLibrary”>
<Property Name=”Title” Value=”My Page Layout”/>
<Property Name=”ContentType” Value=”$Resources:cmscore, contenttype_pagelayout_name”/>
<Property Name=”PublishingAssociatedContentType” Value=”#;MyContentType;[a really long id];#”/>
</File>
Ideally, we’d like some nice powerful mechanisms from within the Visual Studio IDE for generating this stuff. I’m working on that as part of a submission to the CKS:DEV team, but in the meantime we do have another solution. In 2010, you now have Powershell. And look what it can do for you…
First, we need to be able to get the page layout in question from a supplied SPWeb and name.
function Get-SPPublishingPageLayout(
[Microsoft.SharePoint.SPWeb] $web,
[string] $name)
{
$pubWeb = Get-SPPublishingWeb -web $web
return $pubWeb.GetAvailablePageLayouts() | ? { $_.Name -eq $name}
}
Next, we need to format all those bits into the right structure:
function Get-XmlForPageLayout([string] $name)
{
$layout = Get-SPPublishingPageLayout -web (Get-SPWeb $siteurl) -name $name
return (@”<File Path=”{0}” Url=”{0}” Type=”GhostableInLibrary”>
<Property Name=”Title” Value=”{1}”/>
<Property Name=”ContentType” Value=”$Resources:cmscore, contenttype_pagelayout_name”/>
<Property Name=”PublishingAssociatedContentType” Value=”#;{2};{3};#”/>
</File>
“@ -f “PublishingModule\$name”, $layout.Title, $layout.AssociatedContentType.Name, $layout.AssociatedContentType.Id)
}
This now makes it really easy to get the format you need for your elements.xml file. All you need to do now is call this function:
$siteurl = “http://www.fabrikam.com”
Get-XmlForPageLayout -name “homepage.aspx”
Now all you need to do is cut and paste that output from your Powershell command into Visual Studio.

[...] Page Layouts and Powershell December 6, 2010 Filed under: Page Layout,Page Layout Powershell — sladescross @ 4:49 pm http://sharepointtales.wordpress.com/2010/04/27/creating-sharepoint-page-layouts-as-features-in-2010… [...]
Page Layouts and Powershell « Sladescross's Blog
December 6, 2010 at 3:49 pm