Introduction

In this blog we will about create a custom page in SharePoint 2013 using PowerShell method. We need to create many pages based on a Custom Page Layout in our Site Collection for our development purpose.

For that I have created a small PowerShell script to work on this.

Steps

  1. Start your windows PowerShell on your computer.
  2. Right click and select Run as administrator option.
  3. Paste the below script on the PowerShell window and click the enter button.
  4. Check your SharePoint site page will be created successfully.
  1. # Add the PowerShell Snapin
  2. $snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
  3. if ($snapin -eq $null)
  4. {
  5. Add-PSSnapin "Microsoft.SharePoint.Powershell"
  6. }
  7. # Get the SiteURL
  8. $SiteUrl = "https://Thegowtham.sharepoint.com"
  9. # Get the WebURL
  10. $WebUrl = "https://the gowtham.sharepoint.com/sites/TS"
  11. # Get the PageLayout
  12. $PageLayoutRelUrl = "/_catalogs/masterpage/CustomPageLayout.aspx"
  13. # Get the Page URL
  14. $PageName = "TestSite.aspx"
  15. # Get the Title of the Page which is going to get created
  16. $PageTitle = "SamplePage"
  17. # Initialize the Site Object
  18. $Site = Get-SPSite($SiteUrl)
  19. # Get the Publishing Site based on the SPSite
  20. $PublishingSite = New-Object Microsoft.SharePoint.Publishing.PublishingSite($Site)
  21. # Get the SPWeb Object
  22. $Web = Get-SPWeb $WebUrl
  23. # Initialize the PublishingWeb based on the SPWeb
  24. $PublishingWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($Web)
  25. # Get the PageLayouts Installed on the Publishing Site
  26. $Layouts = $ PublishingSite.GetPageLayouts($False)
  27. # Get our PageLayout
  28. $PageLayout = $Layouts[$PageLayoutRelUrl]
  29. # Create a new publishing page.
  30. $Page = $ PublishingWeb.AddPublishingPage($PageName, $PageLayout)
  31. # Assign the Title for the Page
  32. $Page.Title = $PageTitle
  33. # Update the Page
  34. $Page.Update();
  35. # Check in the Page with Comments
  36. $Page.CheckIn("Page Created Successfully")
  37. # Publish the Page With Comments
  38. $Page.ListItem.File.Publish("Page Created Successfully and Publish Comment")
Run the script with the required administrator privilege.

To verify that, go the Pages Library and make sure that new page available or not.