Saturday, March 14, 2015

Set search settings in all site collections of Sharepoint web application via PowerShell

In site collection’s search settings page (Site settings > Search settings) we may set the following parameters:

  • Search center url: when you've specified a search center, the search system displays a message to all users offering them the ability to try their search again from that Search Center;
  • Search results page: page where search queries should be sent to.

Page looks like that:

image

If we have a lot of site collections where search settings should be set it will have sense to use PowerShell script which will do the job. Here is the script which iterates through all site collections in web applications and changes search settings of each site collection:

   1: param(
   2:     [string]$webAppUrl,
   3:     [string]$searchCenterUrl,
   4:     [string]$resultsPage
   5: )
   6:  
   7: function Set-Search-Settings($site)
   8: {
   9:     Write-Host "Set search settings on site" $site.Url
  10: -foregroundcolor green
  11:     $web = Get-SPWeb $site.Url        
  12:     $web.AllProperties["SRCH_SB_SET_SITE"] = 
  13: '{"Inherit":false,"ResultsPageAddress":"' + $resultsPage + '","ShowNavigation":false}' 
  14:     $web.AllProperties["SRCH_ENH_FTR_URL_SITE"] = $searchCenterUrl
  15:     $web.Update()
  16:     Write-Host "    Search settings were successfully set"
  17: -foregroundcolor green
  18: }
  19:  
  20: if (-not $webAppUrl)
  21: {
  22:     Write-Host "Specify web app url in webAppUrl parameter"
  23: -foregroundcolor red
  24:     return
  25: }
  26:  
  27: if (-not $searchCenterUrl)
  28: {
  29:     Write-Host "Specify web app url in searchCenterUrl parameter"
  30: -foregroundcolor red
  31:     return
  32: }
  33:  
  34: if (-not $resultsPage)
  35: {
  36:     Write-Host "Specify web app url in resultsPage parameter"
  37: -foregroundcolor red
  38:     return
  39: }
  40:  
  41: Start-Transcript -Path "output.log" -Append -Force -Confirm:$false
  42:  
  43: $wa = Get-SPWebApplication $webAppUrl
  44: $wa.Sites | ForEach-Object { Set-Search-Settings $_ }
  45:  
  46: Stop-Transcript

Script has 3 parameters:

  • webAppUrl - url of web application where search settings should be changed;
  • searchCenterUrl - url of search center where search query will be redirected to;
  • resultsPage - results page where search query will be redirected to.

Here is example of usage:

.\SetSearchSettings.ps1 -webAppUrl http://example1.com -searchCenterUrl http://example2.com/search/Pages -resultsPage "{SearchCenterURL}/results.aspx"

Note that if {SearchCenterURL} token is used in resultPage parameter it is necessary to enclose parameter’s value into double quotes, otherwise instead of “{SearchCenterURL}/results.aspx” it will be set to “SearchCenterURL” value which is incorrect.

No comments:

Post a Comment