This is a blog post in the series "How to change how search results are displayed." To demonstrate how you can customize your search results, I'll use examples from an internal Microsoft Search Center.
For an overview of the blog post in this series, go to How to change the way search results are displayed in SharePoint Server 2013.
In the previous blog post, I showed you a simple method for adding a custom icon and values from two custom managed properties to your search results. In this blog post, I'll show you a somewhat more complete method for changing the way your search results are displayed that includes if statements and hit highlighting. We’ll learn:
Strategy for killing three birds with one stone - search results version
First, let’s state what we want to achieve:
Before I show you how to do this, let's look at the strategy we want to follow to accomplish this. If it gets a bit complicated, please try to hang in there. Hopefully it'll be clear by the end of this blog.
First, remember how we can think about hit highlighting:
Also remember that each item display template contains a reference to the Item_CommonItem_Body display template, and that this template contains an onlick method that will result in automatically improved relevance based on the end-user’s click behavior.
So our strategy is simply this: create variables in the item display template that will be passed on and rendered by the Item_CommonItem_Body display template.
Specifically, that means that we have to do the following:
OK, now let’s take it step-by-step, with examples of how I did this for my scenario.
How to display values from custom managed properties with hit highlighting, and get automatically improved relevancy
First you have to find the managed property names that correspond to the custom site columns that you want to use. I showed you how to do this in a previous blog.Then you have to do some configuration on the Search Results Web Part. Here's what you should do:
Then you need to add the custom managed properties to an item display template. Here’s what you should do:
Then you need to create variables in the item display template that will be used and rendered by the Item_Common_Item_Body display template. Here's what you should do:
This alert is displayed because we added managed properties to an item display template (what we did in step 9). To update the result types with the newly added managed properties, choose Update.
IMPORTANT! If you don't do this update, the newly added managed properties will not display in your search results.
After I made these changes, when users entered a query in the Search Center, the search result included:
I wanted to make one little change to how the value for owstaxIdTechnicalSubject was displayed. I wanted to give users a bit more context as to what this value represents, so I decided to add the text "Technical Subject:" before the value. Also, because this value is not always present for all list items, I decided that it should only display when a value was present.
To do this, I made a change to the variable that overrides the HitHighlightedSummary property:
Notice that I added a slightly different color to the text "Technical Subject:". With this addition, the final search result is displayed like this:
In our planning, we had decided that we wanted 6 different result types. After creating the TechNet content result type and display template, it was very easy to copy this work over to the other 5 result types.
And here’s the result:
So now that we have changed the way search results are displayed, the next step is to change the values that are displayed in the hover panel.
Next blog post in this seriesHow to display values from custom managed properties in the hover panel