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
- Start your windows PowerShell on your computer.
- Right click and select Run as administrator option.
- Paste the below script on the PowerShell window and click the enter button.
- Check your SharePoint site page will be created successfully.
- # 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://Thegowtham.sharepoint.com"
-
- # Get the WebURL
- $WebUrl = "https://the gowtham.sharepoint.com/sites/TS"
-
- # Get the PageLayout
- $PageLayoutRelUrl = "/_catalogs/masterpage/CustomPageLayout.aspx"
-
- # Get the Page URL
- $PageName = "TestSite.aspx"
-
- # Get the Title of the Page which is going to get created
- $PageTitle = "SamplePage"
-
- # Initialize the Site Object
- $Site = Get-SPSite($SiteUrl)
-
- # Get the Publishing Site based on the SPSite
- $PublishingSite = New-Object Microsoft.SharePoint.Publishing.PublishingSite($Site)
-
- # Get the SPWeb Object
- $Web = Get-SPWeb $WebUrl
-
- # Initialize the PublishingWeb based on the SPWeb
- $PublishingWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($Web)
-
- # Get the PageLayouts Installed on the Publishing Site
- $Layouts = $ PublishingSite.GetPageLayouts($False)
-
- # Get our PageLayout
- $PageLayout = $Layouts[$PageLayoutRelUrl]
-
- # Create a new publishing page.
- $Page = $ PublishingWeb.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("Page Created Successfully")
-
- # Publish the Page With Comments
- $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.