Skip to main content
edited tags
Link
Dikesh Gandhi
  • 7.2k
  • 4
  • 31
  • 58

Creating Create different types of pages through powershellPowerShell in SharePoint 2016 Standard

Source Link
Batman
  • 1.5k
  • 4
  • 26
  • 46

Creating different types of pages through powershell

I'm trying to create a Search Results and Articles (Body only) page through powershell. This is what I've trying:

# 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://collaboration-dvlp2.canadacouncil.ca"  
   
 # Get the WebURL  
 $WebUrl = "https://collaboration-dvlp2.canadacouncil.ca/grants/home/"  
   
 # Get the PageLayout  
 $PageLayoutRelUrl = "/_catalogs/masterpage/SearchResults.aspx"  
   
 # Get the Page URL  
 $PageName = "SearchPage.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]  
 $PageLayout = $PublishingSite.GetPageLayouts($false)| ? {$_.Name -eq "SearchResults.aspx"}

 # 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")  

 # Set as homepage
 $RootFolder = $Web.RootFolder
 $RootFolder.WelcomePage = "Pages/" + $PageName
 $RootFolder.Update();
 $Web.Dispose()

I keep getting this error:

Exception calling "AddPublishingPage" with "2" argument(s): "Value does not fall within the expected range." At line:45 char:2

  • $Page = $PublishingWeb.AddPublishingPage($PageName, $PageLayout)