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.

Advertisements
Previous Post
Leave a comment

1 Comment

  1. Page Layouts and Powershell « Sladescross's Blog

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

%d bloggers like this: