Posted on:
Categories: PowerShell;SharePoint
Description:

There doesn't seem to be a direct way to set the SharePoint 2013 result page settings shown below via PowerShell.

result page settings

I was grateful to find a post, by jshidell, showing how to do this in SharePoint 2010, but the property names for SharePoint 2013 are different. The three settings above are held in the web's Properties collection in a property named SRCH_SB_SET_WEB, where they are described as a JSON object:

{"Inherit":false,"ResultsPageAddress":null,"ShowNavigation":true}

One other gotcha is that the property key is case sensitive. If you try to add it via the Add method of the properties collection, the key is added but automatically converted to lower case and therefore ignored. The SPWeb AddProperty method succeeds because it does not do the auto convert to lower case.

Here is a function that will set that property correctly.

function SetSearchSettingsForWeb
{
 Param
 (
  [Microsoft.SharePoint.SPWeb]$web=$(Throw "SPWeb parameter missing: 'web'"),
  [bool] $inherit=$(Throw "Boolean parameter missing: 'inherit'"),
  [string]$resultsPageAddress=$(Throw "String parameter missing: 'resultsPageAddress'"),
  [bool]$showNavigation=$(Throw "Boolean parameter missing: 'showNavigation'")
 )

 try
 {
  Write-Host "Assigning search settings for web " -ForegroundColor Yellow -NoNewline

  $SEARCH_SETTINGS_PROPERTY_NAME = "SRCH_SB_SET_WEB"

  # Express the settings as a JSON object

  $settingsObj = @{}
  $settingsObj.Inherit = $inherit
  $settingsObj.ResultsPageAddress = $resultsPageAddress
  $settingsObj.ShowNavigation = $showNavigation

  $settingsObjAsJSON = ConvertTo-Json $settingsObj -Compress

  # Add or update an entry in the web properties collection

  if (!$web.AllProperties.ContainsKey($SEARCH_SETTINGS_PROPERTY_NAME))
  { 
   Write-Host "- adding new entry " -ForegroundColor Yellow -NoNewline
   $web.AllowUnsafeUpdates = $true
   $web.AddProperty($SEARCH_SETTINGS_PROPERTY_NAME, $settingsObjAsJSON)
   $web.Update()
  }
  else
  {
   Write-Host "- updating entry " -ForegroundColor Yellow -NoNewline
   $web.AllowUnsafeUpdates = $true
   $web.SetProperty($SEARCH_SETTINGS_PROPERTY_NAME, $settingsObjAsJSON);
   $web.Update()
  }    

  Write-Host $("Done") -ForegroundColor Green
 }
 catch
 {
  Write-Host " .. " $_.Exception.Message -BackgroundColor DarkRed -ForeGroundColor White
 }
}

Call the above function as follows:

SetSearchSettingsForWeb -web $web -inherit $false -resultsPageAddress $null -showNavigation $true