I'm trying to add a page to my search centre in sharepoint 2013. Trying out cSOM too. Here is my code, I am getting a bug Exception calling "ExecuteQuery" with "0" argument(s): "The remote server returned an error: (400) Bad Request.
function New-SearchResultsPage()
{
param(
[Parameter(Mandatory=$true)][string]$siteurl,
[Parameter(Mandatory=$false)][System.Net.NetworkCredential]$credentials,
[Parameter(Mandatory=$false)][string]$PageTitle,
[Parameter(Mandatory=$false)][string]$PageLayoutName
)
try
{
# Authenticate with site.
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteurl)
$ctx.Credentials = $credentials
# Get the SearchResults page layout item needed for setting the
# page layout list item property for new publishing pages.
$rootWeb = $ctx.Site.RootWeb
$mpList = $rootWeb.Lists.GetByTitle('Master Page Gallery')
$camlQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
$camlQuery.ViewXml = '<View><Query><Where><Eq><FieldRef Name="FileLeafRef" /><Value Type="Text">'+$PageLayoutName+'</Value></Eq></Where></Query></View>'
$items = $mpList.GetItems($camlQuery)
$ctx.Load($items)
$ctx.ExecuteQuery()
$pageLayoutItem = $items[0]
$ctx.Load($pageLayoutItem)
# Get publishing web object
#
$web = $ctx.Web
$pubWeb = [Microsoft.SharePoint.Client.Publishing.PublishingWeb]::GetPublishingWeb($ctx, $web)
$ctx.Load($pubWeb)
# Setup for blog post creation using CSV file as input.
#
$pagesList = $ctx.Web.Lists.GetByTitle('Pages')
Write-Host ("Creating custom result page")
Write-Output("Creating custom result page")
# Create page information instance, update properties,
# add it, check it in, publish it, and approve it.
#
$pubPageInfo = New-Object Microsoft.SharePoint.Client.Publishing.PublishingPageInformation
$pubPageInfo.Name = $PageTitle.Replace(" ", "-") + ".aspx"
$pubPageInfo.PageLayoutListItem = $pageLayoutItem
$pubPage = $pubWeb.AddPublishingpage($pubPageInfo)
$pubPage.ListItem.File.CheckIn("", [Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)
$pubPage.ListItem.File.Publish("")
#$pubPage.ListItem.File.Approve("")
$ctx.Load($pubPage)
$ctx.ExecuteQuery()
Write-Host ("Custom result page created !") -ForegroundColor Cyan
Write-Output("Custom result page created !")
Write-Host ("Preparing to update page title")
Write-Output("Preparing to update page title")
# Page added. Now retrieve list item, check it out,
# update title and page content, and check back in.
#
$listItem = $pubPage.get_listItem()
$ctx.Load($listItem)
$ctx.ExecuteQuery()
$file = $listItem.File
$file.CheckOut()
$ctx.Load($file)
$ctx.ExecuteQuery()
$listItem.Set_Item("Title", $PageTitle)
$listItem.Update()
$listItem.File.CheckIn("", [Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)
$listItem.File.Publish("")
#clear$listItem.File.Approve("")
$ctx.Load($listItem)
$ctx.ExecuteQuery()
Write-Host ("Search result page created successfully !") -ForegroundColor Cyan
Write-Output ("Search result page created successfully !")
}
catch
{
Write-Host ("Error while creating search result page. Error -->> " + $_.Exception.Message) -ForegroundColor Red
Write-Output("Error while creating search result page. Error -->> " + $_.Exception.Message)
}
}