Justin Kobel's Blog

Microsoft Collaboration (SharePoint, OneDrive, and Other Related Technologies)

Updating Display Templates in SharePoint 2013 Search

For those of us who have previously worked with SharePoint 2010 (and earlier) search experiences, we quickly grew to loathe XSLT and the complexities in modifying the search experience.  Luckily for us, SharePoint 2013 now allows for us to utilize HTML and JavaScript to customize search results for certain queries, result types, or user types.  Really very powerful stuff.  If you’re looking for an end-to-end walkthrough of how to set this up, I doubt that I could do better than Steve Peschka’s blog post on this over at Share-n-dipity

However, one thing that I was caught by was making updates to the display template and them not being reflected in the search results.

Long story short: updates to the “body” of the Display Item Template are immediately reflected.  By this I mean any HTML, JavaScript or otherwise that you include within the first “<div>” of the Display Template are immediately reflected when updating them.  However, updates to the metadata about the Display Item template, such as the mso:ManagedPropertyMapping sections:

<mso:ManagedPropertyMapping msdt:dt="string">'Title':'Title','Path':'Path','Description':'Description','EditorOWSUSER':'EditorOWSUSER',
'LastModifiedTime':'LastModifiedTime','ContosoDocumentId':'ContosoDocumentId','ContosoCaseNumber':'ContosoCaseNumber',
'ContosoReceivedDate':'ContosoReceivedDate','ContosoScannedDate':'ContosoScannedDate'</mso:ManagedPropertyMapping>

 

Were not being reflected within my search results.  After assuming that I had screwed something up (which is normally the case) for an hour or two, I decided to browse back to my Result Types page on the site (_layouts/15/manageresulttypes.aspx?level=site) and I then of course noticed a big yellow bar indicating that my properties needed to be resynchronized.
image

Of course, I didn’t initially see this since SharePoint makes it so easy to update these files in the editor of your choosing, and saving them directly back to SharePoint (which generates the corresponding .js assets that the results pane actually uses).

Clicking this “Update” button then does as we would expect, and renders the additional properties in the search results. 

Of course, this is fine for now, but what about when I’m deploying this change from one environment to another?  I could ask my deployment team to manually navigate to this page, but I’d prefer not to; especially if I’m having to make this change across multiple site collections, sites, or search service applications. So PowerShell to the rescue!  Note that in my sample cmdlet below, I’m updating the element at the SPWeb level, but this could be updated at the SPSite or ServiceApplication level, depending on where your result type originated from.

Add-PSSnapin Microsoft.SharePoint.PowerShell
 
$ssa = Get-SPEnterpriseSearchServiceApplication #note that in this farm I only have one SSA; if we need more than one
 
$tenantOwner = Get-SPEnterpriseSearchOwner -Level SPWeb -SPWeb "https://dev.contoso.com/search/"
 
Get-SPEnterpriseSearchResultItemType -Owner $tenantOwner -SearchApplication $ssa
 
$ResultType = Get-SPEnterpriseSearchResultItemType -SearchApplication $ssa -Owner $tenantOwner | where-object { $_.Name -eq "ContosoResultType" }
 
$ssa.UpdateResultItemType($ResultType)
 
*Huge thanks to Martin below for calling out that I had copied the wrong function into here initially. 
Note that this script won't work when you don't have direct access to the $ssa (e.g. in a dedicated search farm topology).

Comments (4) -

  • Martin

    6/27/2013 5:47:14 PM | Reply

    I would like to achieve this as well via powershell, but
    Get-SPEnterpriseSearchResultItemType returns a ResultItemType msdn.microsoft.com/.../...ultitemtype_methods.aspx

    It doesn't seem to have an Update() method. Is there an error in the code above?

  • justinkobel

    6/27/2013 8:13:35 PM | Reply

    Thanks for noting that Martin; the above script should actually work now.  Apparently I had too many copies of this floating around.  

    • Martin

      6/27/2013 10:15:34 PM | Reply

      Thanks for the update Smile

      Have you found that running this code makes the "Property Sync" required message dissapear from the manageresulttypes.aspx page?
      I've added my Display Templates at the Site collection level and I've called $SSA.UpdateResultItemType for all non BuiltIn ResultItemTypes, but after doing it still says I need to Update on the page.

      • Matthias

        12/14/2014 9:24:04 AM | Reply

        Hi Martin,

        I had the same problem: Updating a Display Template with the UpdateResultItemType-Method does not work for individual ResultTypes on the Site-Collection level.

        I worked around with the following script. It simply clicks on the "Update" button on the site via PowerShell. Tecnically it calls the embedded JavaScript function "SyncProperties()".

        In my case i had to move through all webapplications and all sitecollections. So be carefully with copy&paste.


        $manageresulttypes = "/_layouts/15/manageresulttypes.aspx?level=sitecol"
        $WebApps = Get-SPWebApplication

        foreach($webApp in $WebApps) {
          foreach($site in $webApp.Sites){
            $ie = New-Object -com "InternetExplorer.Application"
            $Siteurl = $site.url
            $completeurl = "$siteurl$manageresulttypes"
            Write-Host "Klicking on" $siteurl
            $ie.navigate($completeurl)
            while($ie.ReadyState -ne 4){Sleep -milliseconds 20}
            $link=$ie.Document.getElementsByTagName('A') | ?{$_.href -eq 'javascript:SyncProperties()'}
            $link.click()
            $ie.dispose
            $site.dispose
          }
        $webapp.dispose
        }

Pingbacks and trackbacks (1)+

Add comment

Loading