Thursday, May 7, 2015

How to Configure SharePoint Search Center with result types through Powershell script

When we think of deployment scenarios onto multiple servers, we always try to find better way of doing the deployment, especially if there are lots of configuration changes to be done in the environments. Power shell scripts are smarter ways to get the Job done with minimal manual intervention. The power shell script helps to deploy result types for Search Center Configuration.

The Configuration XML file looks something like this ConfigureSearchCenter.xml which can be highly configured to meet requirement for deployment by providing values of simple parameters -


<?xml version="1.0" encoding="utf-8"?>
<Configuration>
<SearchCenterUrl>http://SearchCenterUrl</SearchCenterUrl>
<SearchServiceApplication>Search Service Application</SearchServiceApplication>
<ResultTypes>
  <resulttype filteron="ContentSource" filteroperator="IsEqual" filtervalue="FilterContent" itemtemplate="Custom_Default.js" itemtemplatepath="Search" name="Custom Result Type"></resulttype>
</ResultTypes>
</Configuration>
 


Powershell script for configuring search center through result type -

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

Write-Host "Configure Search Center begins...."

$testResultTypeName = "Custom Result Type"

$rootScriptPath = Split-Path -parent $MyInvocation.MyCommand.Definition

[xml]$config = Get-Content ($rootScriptPath + "\ConfigureSearchCenter.xml")
$searchCenterUrl = $config.Configuration.SearchCenterUrl
$resultTypeNodes = $config.SelectNodes("//ResultTypes/ResultType")

try {
$sspApp = Get-SPEnterpriseSearchServiceApplication -Identity $config.Configuration.SearchServiceApplication
$web = Get-SPWeb $searchCenterUrl
$searchObjectOwner = Get-SPEnterpriseSearchOwner -Level SPWeb -SPWeb $web

if ($sspApp -ne $null -and $web -ne $null -and $searchObjectOwner -ne $null) {
foreach($resultTypeNode in $resultTypeNodes) {
#check to make sure it doesn't already exist
$resultItemType = Get-SPEnterpriseSearchResultItemType -Owner $searchObjectOwner -SearchApplication $sspApp | Where Name -EQ $resultTypeNodes.Name

#Remove the Result Type if it exists
if ($resultItemType -ne $null) {
Write-Host -ForegroundColor Yellow -BackgroundColor Black "Result Item Type " $resultTypeNode.Name " exists, removing it...."
Remove-SPEnterpriseSearchResultItemType -Owner $searchObjectOwner -SearchApplication $sspApp -Identity $resultItemType -Confirm:$false
}

$rule = Get-SPEnterpriseSearchPropertyRule -PropertyName $resultTypeNode.FilterOn -Operator $resultTypeNode.FilterOperator
$rule.AddValue($resultTypeNode.FilterValue)
$ruleCollection = Get-SPEnterpriseSearchPropertyRuleCollection
$ruleCollection.Add($rule)
$url = "~sitecollection/_catalogs/masterpage/Display Templates/" + $resultTypeNode.ItemTemplatePath + "/" + $resultTypeNode.ItemTemplate
$item = New-SPEnterpriseSearchResultItemType -Owner $searchObjectOwner -SearchApplication $sspApp -Name $resultTypeNode.Name -Rules $ruleCollection -RulePriority 1 -DisplayTemplateUrl $url -OptimizeForFrequentUse $true

Write-Host $resultTypeNode.Name " Result Type added"
}

$web.Close()
$web.Dispose()
} else {
Write-Host -ForegroundColor Red "Unable to retrieve pre-requisite objects to configure Search Center (Search Service App, SPWeb, or Search Owner)"
}
}
catch {
Write-Host -ForegroundColor Red "An error occurred configuring Search Center: " $_.Exception
return $_.Exception
}

This will make lot of ease while deploying your search center configuration scripts on other servers.