Create Publishing Page using Custom PageLayout by PowerShell in SharePoint 2013

Sathish Nadarajan
 
Solution Architect
December 11, 2013
 
Rate this article
[Total: 0    Average: 0/5]
 
Views
28034

I was come up with an interesting requirement stating that, we need to create some huge numbers of pages based on a Custom PageLayout in our Site Collection. For that, I was planning to write a PowerShell with some configuration values. So that, on the execution, the script will create those pages. Thought of sharing this to the community.

Basically it is going to be a simple straight forward script. We have a publishing portal and the custom PageLayout has been deployed on the Publishing Portal.

The script is self-explanatory and doesn’t require much explanation I guess.

 # Add the PowerShell Snapin
 $snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
 if ($snapin -eq $null) 
 { 
     Add-PSSnapin "Microsoft.SharePoint.Powershell" 
 }
 
 # Get the SiteURL
 $SiteUrl = "https://MySiteCollectionURL"
 
 # Get the WebURL
 $WebUrl = "https://MyWebURL"
 
 # Get the PageLayout
 $PageLayoutRelUrl = "/_catalogs/masterpage/CustomPageLayout.aspx"
 
 # Get the Page URL
 $PageName = "Test.aspx"
 
 # Get the Title of the Page which is going to get created
 $PageTitle = "Test"
 
 # Initialize the Site Object
 $Site = Get-SPSite($SiteUrl)
 
 # Get the Publishing Site based on the SPSite
 $PubSite = New-Object Microsoft.SharePoint.Publishing.PublishingSite($Site)
 
 # Get the SPWeb Object
 $Web = Get-SPWeb $WebUrl
 
 # Initialize the PublishingWeb based on the SPWeb
 $PubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($Web)
  
 # Get the PageLayouts Installed on the Publishing Site
 $Layouts = $PubSite.GetPageLayouts($False)
 
 # Get our PageLayout
 $PageLayout = $Layouts[$PageLayoutRelUrl]
  
 # Create a new publishing page.
 $Page = $PubWeb.AddPublishingPage($PageName, $PageLayout)
 
 # Assign the Title for the Page
 $Page.Title = $PageTitle
 
 # Update the Page
 $Page.Update();
 
 # Check in the Page with Comments
 $Page.CheckIn("Test Comment")
 
 # Publish the Page With Comments
 $Page.ListItem.File.Publish("Test Publish Comment")
 

That’s it. Run the script with the required privilege. To verify that, go the Pages Library and make sure that you are seeing Test.aspx there.

Happy Coding.

Sathish Nadarajan.

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
[Total: 0    Average: 0/5]
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 13+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment