The 100% way of automatically updating Result Types (with PowerShell)

This will be a short and sweet post about how to update result types after display templates have changed. It’s a common scenario to update a display template with changes to managed properties. If your result types are using the display templates, you need to go to the result types and click update. Quite a cumbersome tasks if you are deploying changes to many display templates/result types.

Fortunately my superstar colleage Mikael Svenson has written about this before. This post is another way of doing the same as he is doing, but with PowerShell instead of server side code.

The process is quite simple:

  1. Get the result type names from search configuration file
  2. Get the display template used in the result type
  3. Get the managed properties used in the display template
  4. Update the result type with the display template properties

The relevant PowerShell snippet for this is as follows

function UpdateResultItemTypes([string]$Url, [string]$PathToSearchConfig) {
    Write-Host "Updating result type items at $Url"
    $site = Get-SPSite $Url
    [void] [Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.Search") 
    $sspApp = Get-SPEnterpriseSSA
    if ($sspApp -eq $null) {
        Write-Error "Unable to get an instance of the Search Service application or proxy"
    $fedManager = New-Object Microsoft.Office.Server.Search.Administration.Query.FederationManager($sspApp)
    $searchOwner = New-Object Microsoft.Office.Server.Search.Administration.SearchObjectOwner([Microsoft.Office.Server.Search.Administration.SearchObjectLevel]::SPSite, $site.RootWeb)

    Get-ResultItemTypeNames -PathToSearchConfig $PathToSearchConfig | % {
        $resultItemName = $_
        Write-Debug "Updating result item $resultItemName"
        $resultType = Get-SPEnterpriseSearchResultItemType -Owner $searchOwner -SearchApplication $sspApp | ? {$_.Name -eq $resultItemName}
        if ($resultType -ne $null) {
            $updatedProperties = GetResultItemUpdatedProperties -site $site -resultType $resultType
            if ($updatedProperties -ne $null) {
                Set-SPEnterpriseSearchResultItemType -Identity $resultType -SearchApplication $sspApp -Owner $searchOwner -DisplayProperties $updatedProperties

function GetResultItemUpdatedProperties([Microsoft.SharePoint.SPSite]$site, $resultType) {
    $masterPageGallery = [Microsoft.SharePoint.SPList]$site.GetCatalog([Microsoft.SharePoint.SPListTemplateType]::MasterPageCatalog)
    $displayTemplateName = [System.IO.Path]::GetFileName($resultType.DisplayTemplateUrl)

    $displayTemplate = $masterPageGallery.Items | ? {$_.Name -eq $displayTemplateName}

    if ($displayTemplate -ne $null) {
        $properties = $displayTemplate["ManagedPropertyMapping"]

        [string]$propFormatted = ""
        $propArray = $properties.Split(",")
        $propArray | % {
            $pair = $_.Replace("'", "").Split(":")
            $propFormatted += $pair[$pair.length - 1] + ","
        return $propFormatted.TrimEnd(",")
    return $null

1 thought on “The 100% way of automatically updating Result Types (with PowerShell)”

Leave a Reply

Your email address will not be published. Required fields are marked *