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&#8221;
Get-XmlForPageLayout  -name “homepage.aspx”

Now all you need to do is cut and paste that output from your Powershell command into Visual Studio.

SPEvo slides

SharePoint Evolution was a blast, and even though a lot of speakers didn’t make it over, everyone pulled together. All credit to the Combined Knowledge team, especially Steve Smith and Zoe Watson for not breaking under the pressure of #ashtag.

Like many others (especially Spence Harbar and Eric Shupps), I did a number of extra sessions, and I’ve uploaded three of the decks to Slideshare.

Building high scale, highly available websites in SharePoint 2010

Introduction to the Client OM in SharePoint 2010

Build a SharePoint website in 60 minutes