Script to provision SharePoint 2010 publishing pages

In this post, I’d like to share a method of provisioning many publishing pages automatically using a PowerShell script.
Page Content

 

In this post, I’d like to share a method of provisioning many publishing pages automatically using a PowerShell script.

An example of when this is handy, is when you need to migrate old company press releases to an archive section. Another bulk provisioning scenario may be when you need to provision a site structure along with pages so end users populate the content on pages when they’re ready. You could do the same with a SharePoint 2010 solution package and deploy your pages as modules. In this example, however, we’ll just be using a script, so no deployment or down time required on the server while our provisioning happens.

I’ll assume you’re running afully configured development environment with SharePoint 2010 publishing infrastructure enabled.

I will be using http://www.contoso.com as my test URL, so if you’re using another host name – replace the respective variables in the script.

Open SharePoint 2010 Management Shell from the Start menu of your development server and execute the following script which provisions one publishing page to the root of your site:

01 $SiteUrl = "http://www.contoso.com"
02 $pageFileName = "TestPage.aspx"
03 Add-PSSnapin "Microsoft.SharePoint.Powershell"
04
05 $SPSite = Get-SPSite | Where-Object {$_.Url -eq $SiteUrl}
06   if($SPSite -ne $null)
07   {
08   $RootWeb = $SPSite.RootWeb
09   $pubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($RootWeb)
10
11   $pageLayout = $pubWeb.GetAvailablePageLayouts() | Where-Object {$_.Title -eq "Blank Web Part page"}
12
13   $page = $pubWeb.GetPublishingPages().Add($pageFileName, $pageLayout)
14
15   $page.Title = $PressReleaseFileName
16   $pageItem = $page.ListItem
17   $pageItem["Comments"]="New page description"
18   $pageItem["PublishingContactName"]="Brad"
19   $pageItem["PublishingContactEmail"]="brads@contoso.com"
20   $page.Update()
21
22   $page=$pubWeb.GetPublishingPages() | Where-Object {$_.Name -eq $pageFileName}
23   $page.CheckOut()
24   $pageItem = $page.ListItem
25   $pageItem["Comments"]="Changed description"
26   $page.Update()
27
28   $page.CheckIn("Checked in by PowerShell script")
29   $page.listItem.File.Publish("Published by PowerShell script")
30   }
31 $SPSite.Dispose()

We start with defining variables and loading a PowerShell snap in.

Next, we get a hold of the current site and create an instance of a publishing site object to work with. We retrieve the Blank Web Part page page layout to create a new page with. Once created, we make changes to some of the page’s properties such as contact name of the creator, etc.

Next, we look at how you can get a hold of the existing page to make changes to it. In our case we connect to the same page instance, which looks a bit redundant.

Advertisements

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: