0

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)

2
  • Which SharePoint version are you using? – Dikesh Gandhi May 4 '17 at 6:00
  • SharePoint 2016 Standard – Batman May 4 '17 at 6:18
0

Please try below code for creation of publishing page as it worked for me before:

Use SharePoint 2016 Management Shell.

$PageLayoutUrl = "/_catalogs/masterpage/BlankWebPartPage.aspx"  

#  
Get site object and create publishing site object  
$SPSite = Get - SPSite("http://sharepoint2016")  
$PublishingSite = New - Object Microsoft.SharePoint.Publishing.PublishingSite($SPSite)  

# Get web object and create publishing web object  
$SPWeb = Get - SPWeb "http://sharepoint2016"  
$PublishingWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($SPWeb)  
$LayoutsColl = $PublishingSite.GetPageLayouts($False)  


# Get the page layout object and create Publishing Page based on it  
$SPPageLayout = $LayoutsColl[$PageLayoutUrl]  
$Page = $PublishingWeb.AddPublishingPage("WelcomePage.aspx", $SPPageLayout)  
$Page.Title = "Welcome"  
$Page.Update();  
$Page.CheckIn("Page Created")  
$Page.ListItem.File.Publish("Page Checked in and Publishing Completed")

Hope this will help you!

Update

Please try to create page manually in browser using your particular page layout.

You will not find "searchresults.aspx" page layout in new page creation part in Pages library.

Now, when you go to the http://<server-name>/_catalogs/masterpage/Forms/AllItems.aspx you will find "SearchResults.aspx" and when you edit the properties of the page layout you will find that its content type is Master page instead of page layout.

enter image description here

So, when PowerShell command tries to find that page layout it will get null and that's why it may gives this error when code runs with null to create page.

7
  • This should work with any page template right? So if I change the layout to "/_catalogs/masterpage/SearchResults.aspx" would that work? – Batman May 4 '17 at 13:48
  • Sorry. I hadn't checked your whole code. Mine and yours code are same in some manner. I pasted the code that I had used before. I was working fine in my environment. And yes, it should work with any page layout with correct page layout url. – Dikesh Gandhi May 5 '17 at 3:56
  • I tried but it won't work with other page layouts – Batman May 5 '17 at 5:40
  • I have updated the answer. – Dikesh Gandhi May 5 '17 at 5:57
  • So it cannot be done through powershell? – Batman May 5 '17 at 6:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.