How to display values from custom managed properties in search results – option 2


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:

  • Display values from two custom managed properties
  • Apply hit highlighting to the two custom managed properties
  • Get automatically improved relevancy for our search results

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:

How to think about hit highlighting

  1. The managed properties that are listed in the Hit-highlighted properties (JSON) section of the Search Results Web Part and the "magical summary" property are passed to the HitHighlightedProperties property.
  2. All values of the HitHighlightedProperties property are passed to the HitHighlightedSummary property. 
  3. A truncated version of the values in HitHighlightedSummary is displayed in the Search Results Web Part with three dots at the end.

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.

Reference to Item_CommonItem_Body display template

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:

  • Add the custom managed properties that we want to display in our search results to the Hit-highlighted properties in the Search Results Web Part.
  • Add the custom managed properties to an item display template.
  • In the item display template, create a new variable that will be used by the property HitHighlightedSummary to display our two custom managed properties with hit highlighting.
  • In the item display template, leave the reference _#=ctx.RenderBody(ctx)=#_ so that the Item_ComonItem_Body display template will render the search result. This ensures that we get automatically improved relevancy.

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:

  1. On the Search results page, choose the Settings menu, and then choose Edit Page.
  2. In the Search Results Web Part, choose Web Part Menu –> Edit Web Part.
  3. In the Web Part tool pane, choose to expand the Display Templates section and then select Use a single template to display items. This will allow you to make changes to the Hit-highlighted properties (JSON) field.

Select Use a single template to display items

  1. In the Hit-highlighted properties (JSON) field, use the following format to add the custom managed properties that you want to add hit highlighting to:
    "<Managed property name>"

    In my scenario, I wanted to apply hit highlighting to the ContentSummaryOWSMTXT and the owstaxIdTechnicalSubject managed properties.

Added two properties to the Hit-highlighted properties section

  1. Choose Apply to save the changes. This will close the Display Templates section.
  2. Choose Display Templates to reopen the section, and choose Use result types to display items.

Select Use result types to display items

  1.  Choose OK and save the page.

Then you need to add the custom managed properties to an item display template. Here’s what you should do:

  1. Open the item display template that belongs to the result type for which you want to customize search results.

    In my scenario, this was TechNet content.

  2. In the item display template, in the ManagedPropertyMapping tag, use the following syntax to add the custom managed properties that you want to display:
    '<Current item property name>':<Managed property name>'

    In my scenario, I wanted the values from the managed properties ContentSummaryOWSMTXT and owstaxIdTechnicalSubject to be displayed in the search result. To make the file easier to maintain, I named the current item properties the same as the managed properties.

Two managed Properties added to display template

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:

  1. Because you have no guarantee that the values of your custom properties will contain any of the entered query words, that is, hit highlighting will not be used, you have to create variables that ensure that the value of your custom properties will be displayed regardless of hit highlighting.

    The following screenshots show how I created two such variables for my custom properties ContentSummaryOWSMTXT and owstaxIdTechnicalSubject.

Variables for custom properties

  1.  In addition, I added a similar variable for the Title property. If you don’t add this, the search results will not be rendered.

Variable for Title property

  1. The last step you have to do in the item display template is to create a variable that will override the HitHighlightedSummary property used to display the values.

Variable for  HitHighlightedSummary property

  1. Save the item display template.
  2. NOTE: You do not need to do this step if you are using SharePoint Online.
    Go to Site settings –> Search Result Types. Notice that a Property Sync alert is displayed.

Property Sync alert on Managed Result Type page

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.

Update Properties from result types

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:

  1. A custom icon.
  2. The value of Title with hit highlighting.
  3. The value of ContentSummaryOWSMTXT with hit highlighting.
  4. The value of owstaxIdTechnicalSubject. The query words did not match the property value, however because of the variable that I created in step 10, the value is displayed.
  5. A link to the item in the list

Custom search result

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:

Updated variable for HitHighlightedSummary

Notice that I added a slightly different color to the text "Technical Subject:". With this addition, the final search result is displayed like this:

Updated custom search result

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:

Final customized search results page

 

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 series
How to display values from custom managed properties in the hover panel

Comments (48)

  1. Anonymous says:

    Hi Bella,

    Thanks for sharing a great post.

    I have tried it out and everything looks fine in my development except for my contents are not truncated after passing it into ctx.CurrentItem.HitHighlightedSummary. Do you have any idea why it is not truncating? Thanks in advance.

  2. Anonymous says:

    Hi where exactly are you suppose to place the variables in the Item template? The link to your file does not work

  3. Bella_Engen says:

    Hi DanTheManXX,

    Am not quite sure I completely understand your scenario. Do you just want to add two managed properties to your people search results? If so, you should add these to the display template that you use to display your people search results, and refresh your People result source.

    Bella

  4. Bella_Engen says:

    Hi,

    Which plan do you have?
    To understand your situation a bit better, can you describe what you mean by managed properties coming back as "undefined"? Do you get an error message, or is it the case that you can't see any managed property values?

    Bella

  5. Ed (DareDevil57) says:

    thanks for sharing.

  6. DanTheManXX says:

    Hi great post. I have a related question

    Say I have my custom display template for people search My_Item_Person. which I have successfully amended to add the user's email and work phone. Now I want amend this to display two custom AD properties propa and propb which are both mapped to individual term sets in the term store. replicating In your example you added to Hit-highlighted properties . Hence you are using srch.u.getHighlightedPropeties() as opposed to the $htmlEncode(ctx.CurrentItem.RockBandsLiked);.

    You also switch from selecting the displaytemplate to Results Type in the people search web part settings.

    Do I need to do this for the people search.

  7. Bella_Engen says:

    Hi Santiago,

    If I understand your scenario correctly, you want to add the ability for users to be able to Like a specific search result. This functionality is not available by default, so you’ll have to implement custom code.

    Bella

  8. Bella_Engen says:

    Hi Gopal,

    It’s difficult to say what can be the problem, but have you double checked that you don’t have any syntax errors or spelling mistakes in your display template?

    Bella

  9. Anonymous says:

    Hi Bishwadeep,

    The Item_CommonItem_Body display template also has a sorting, where the property HitHighlightedSummary is displayed between the title and the link.  So, by not displaying any properties through the custom display template (in my scenario, TechNet content), but instead creating a variable that can be used by the Item_ComonItem_Body display template to display HitHighlightedSummary,  the sorting is done in by Item_ComonItem_Body display template .

    Hope this helps.

    Bella

  10. Bella_Engen says:

    Hi Charles,

    I’m not quite sure I understand your scenario. I’m not aware of a content type named "Publishing Images", but there is a managed property named "Publishing Image". Is that what you mean?

    The default display templates used by the Search Results Web Part use different managed properties than the display templates used by the Content Search Web Part. So, to show an image in your search results, you’ll have to modify the display templates used
    by the Search Results Web Part. I describe how you can do that in another blog series:

    http://blogs.technet.com/b/tothesharepoint/archive/2013/09/03/how-to-change-the-way-search-results-are-displayed-in-sharepoint-server-2013.aspx

    Hope this helps,
    Bella

  11. Bella_Engen says:

    Hi,

    First, sorry for the late answer.
    Not sure if I completely understand your scenario, but if you created a variable, then that variable, as you have defined it, will be displayed.

    Hope this helps.

    Bella

  12. Anonymous says:

    I'm seeing this bug on my SPO tenant where if I use results types in my search results web part, none of my custom managed properties work in my display template. They simply come back as undefined. Is there any way around this?

  13. Bella_Engen says:

    Hi Leslie,

    Happy to hear that you found it helpful πŸ™‚

    Bella

  14. Bella_Engen says:

    Hi Rich,

    Yes, I am aware of this behavior. At the moment I don’t know how to get around this.

    Bella

  15. Bella_Engen says:

    Hi Angelina,

    In Option 2 I created variables for my custom properties (http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-66-12/3683.TwoVariables.png).
    Then I created another variable that contains the values of my custom properties (http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-66-12/7181.SummaryVariable.png).
    This variable is then passed on and rendered by the Item_CommonItem_Body display template.

    But, I do need to sync the properties on the Manage Result Types page. Otherwise, the values of my custom properties will not be displayed.

    Hope this helps.

    Bella

  16. Bella_Engen says:

    Hi Bryn,

    The URL to the list item is displayed by the Item_CommonItem_Body display template. You can modify this display template so that "your" URL is shown.

    Hope this helps,
    Bella

  17. Bella_Engen says:

    Hi Von,

    I’m not quite sure what you mean by “Office” info. But you can use the same approach that you used to include Work Phone and Email Address. You just have to use the managed properties that contain the “Office” info, and modify the display template that you use to display your People Search results

    Hope this helps.

    Bella

  18. Bella_Engen says:

    Hi Ramirito,

    I suggest that you contact Microsoft Support with this issue.

    Bella

  19. Bella_Engen says:

    Hi,

    Thanks for letting me know that the file dosen’t work. I have deleted them until we can fix the issue.

    You can add the variables inside the blocks that begin with <!–#_ and end with _#–>.  JavaScript code is used inside these blocks.

    Hope this helps,
    Bella

  20. Bella_Engen says:

    Hi Corey,

    You can customize your search results in SharePoint Online as well, but there are a few differences between what I describe in this blog series. Could you provide some more details about your scenario in SharePoint Online?

    Bella

  21. Stonecoldice says:

    This is a really useful post – Thanks Bella!!

  22. Ramirito says:

    Hello Bella,

    I have a issue with search feature when I add the WorkPhone property.

    I could see the search results in the server, and even in clients when I use the SharePoint manager account, I could also see the search results when I use an user account in the server, but when I use an user account in the clients shows a text that says "Error."
    without description or correlation ID.

    I created another search people page and I copied the "Search Result Type" Person, changed the Item_Person.html and copied as Item_Person2.html, changed to display just the WorkPhone, and selected the Item_Person2.html as Display template URL with similar behavior.

    How could I grant any active directory user to watch the phone numbers?

    Thank you.

  23. Anonymous says:

    Hi JWerner,

    Sorry, I’m not sure about the answer to your question πŸ™ I suggest you contact Microsoft Support.

    Bella

  24. Anonymous says:

    Hi Koen,

    Sorry, one year later and I still haven’t found an answer to this exact issue. I suggest that you contact Microsoft Support with your question.

    Thanks,
    Bella

  25. Anonymous says:

    Hi Roland,

    No, I don’t know of anything specific that you need to do for managed metadata.
    I suggest that you contact Microsoft Support with your question.

    Bella

  26. Anonymous says:

    Hi Roland,

    Glad to hear that you figured out your issue with managed metadata πŸ™‚

    In SharePoint Server 2013, you can’t control stemming on managed properties.
    For more information, see these articles:
    https://technet.microsoft.com/en-us/library/jj219499.aspx

    https://technet.microsoft.com/en-us/library/gg549987.aspx#BKMK_DisableStemming

    Hope this helps,
    Bella

  27. Morgan Roach says:

    I appreciate this post!

    http://www.desertedroad.com

  28. Bishwadeep says:

    Hello Bella,

    Just wanted to know, in the strategy, how the problem of pushing the custom properties, between title & link is getting sorted, if we continue to use the _#=ctx.RenderBody(ctx)=#_ reference for the Item_CommonItem_Body context

  29. Christian says:

    Hi Bella,

    I'm struggeling with Office 365 SharePoint Online and tried to follow your steps, but with no success. I only found JS files and no HTML files in the SharePoint Designer. As soon as I modify the JS file anyway, I get "Display Error" in the Search Result Web Part.

    Is there any special trick to access the HTML files related to the item display template?

    Thanks in advance

    Christian

  30. Bella_Engen says:

    Hi Christian,

    Have you mapped your network drive? (as described here:
    blogs.technet.com/…/stage-6-upload-and-apply-a-new-master-page-to-a-publishing-site.aspx
    )  If so, you should see both the HTML and JS files in Windows Explorer.

    Also, when you modify display templates, you should only modify the HTML files. When you save the HTML file, the changes will automatically be updated in the corresponding JS file.

    Hope this helps.

    Bella

  31. Christian says:

    Hi Bella,

    thank you for your reply. I'm not provided with the Design Manager in my site collection, because it's based on a team site template not a Publishing site – as I'd like to modify the Intranet search.

    When opening the site collection with SPD 2013 I only see the JS files. I copied and modified one file and (now) it works perfectly, I don't see any other way to modify some display templates within site collections not based on a Publishing Site Template.

    Are there any reasons not to modify the JS file, if no HTML page exists (MS Support, etc.)?

    Many thanks in advance

    Regards

    Christian

  32. Bella_Engen says:

    Hi Christian,

    If you got it to work, then great! πŸ™‚ The reason to modify the HTML file over the JS file is simply because it is easier.

    Btw, you can enable the Design Manager by activating two features:

    • SharePoint Server Publishing Infrastructure (this is a Site Collection feature)
    • SharePoint Server Publishing (this is a Site feature)

    (office.microsoft.com/…/enable-publishing-features-HA102802294.aspx)

    Maybe you can try this and let me know you it goes?

    Thanks,

    Bella

  33. Christian says:

    Hi Bella,

    I'm aware of the Publishing Feature. But we don't need any publishing functionality instead of the Design Manager, therefore it wouldn't be the right approach in the customer's special context.

    Thank you and best regards

    Christian

  34. Great blog series about customizing the search result in SharePoint 2013

  35. Corey Roth says:

    To confirm, I have found no solution for this in SharePoint Online. Since the sync button is not present on the result types page, custom managed properties do not work at all with result types enabled in the search center. They do not have a value. I've never seen it work. The only way to get them to work is to disable result types.

  36. Gopal Nayak says:

    Hi Bella,

    I have one site column. Made a full crawl then mapped it to newly created managedproperty. The property is now available in refiners but when i am using it in display template its not showing. Please help

  37. Von says:

    I was able to modify the html to include Work Phone and Email Address. How to display "Office" info for People Search result? Is this possible?Thanks.

  38. Angelina1 says:

    Hello,

    Thanks for these great tutorials.

    One question, with Option 2 don't we need to add in the

    the
    _#=ctx.CurrentItem.CustomProperty=#_ ? Are the managed properties automatically retrieved?

    Thank you in advance.

  39. Rich B says:

    Hi Bella, thanks the information it has helped a great deal. Have you ever had to retrieve managed metadata from a term set into a results page / hover panel? We have the refinement panel working well for this, but if we try to pull the terms into results they show the GUIDs and | : type information.
    cheers
    Rich

  40. Leslie M says:

    Hello Bella,

    I cannot thank you enough for your clear, concise and comprehensive review of the customization of result types. Your work helped me to deliver on a client request. Couldn't have done it without you!

    Thanks!!!!!

    Leslie

  41. Bryn says:

    Bella,

    Thank you so much for your tutorial! I'm currently overwriting links to particular list items with a link field from those list items. (It's basically a list full of links and instead of taking users to the list item details page I want to take them directly
    to the link.)

    This part is working great. The part I'm hung up on is when the results page is rendered it gives me the title and then underneath it shows the URL to the list item instead of the URL of where clicking the title will take you. I cannot figure out how to overwrite
    this too. Any suggestions?

  42. charles says:

    Hello Bella,

    First, thanks for all this information. Very helpful!

    Then, I have an issue related with managed properties mapped to "Publishing Images" content types. It works well using the CSWP but when using the Search Results Web Part, variable is empty. Any thoughts for me?

  43. Santiago says:

    Hi Bella, I have a bit of a strange problem which I cannot find the solution to… I wanted to place the Likes a page receives on the Display Template, the property is a managed property by default as LikesCount, so I've added the Managed Property "LikesCount"
    to the Display Template but for some reason the Likes are only showing up when I do an empty search (I leave the Search Box empty and hit Search), but when I search for something the Likes disappear… I've been looking around but I just cannot find the issue…
    Debugging the JavaScript I've noticed that the property is empty when I do the search and it has the correct value when I do the empty search (which doesn't add anything since those are the results that I'm seeing on the Search Results web part it self)…
    Do you have any idea why this might be happening?

    Thanks,
    Santiago.

  44. MARIE says:

    This is awesome, was just what I needed for the tasks I needed to complete.I learned alot. THANK-YOU, THANK-YOU, THANK-YOU!

  45. Koen Reynaert says:

    Hi Bella, Almost one year later and I have the same question Rich have asked you.
    "Have you ever had to retrieve managed metadata from a term set into a results page / hover panel?"

    I hope you solved this one, πŸ™‚

    Regards,
    Koen

  46. JWerner says:

    Great post! Is it possible to change the title from the default to a custom managed property and maintain the hit highlighting? It looks like the title is always loaded from Srch.U.getHighlightedProperty(id, ctx.CurrentItem, "Title"); within the Item_CommonItem_Body.html
    display template so I'm not sure if there is a way to override that.

  47. adam says:

    Hi – I have a potential solution for the metadata question. I did this:

    var MyPropertyValue = "" + $getItemValue(ctx, "owstaxIdMyPropertyValue");

    This just displays the value without the GUIDs and so forth. It needs the "" to convert the item to a string.

  48. roland says:

    Hi Bella,

    Is there anything specific needed for displaying a managed property that is a managed metadata value? When using the Search Query Tool I get the value correctly back, but the display templates (item and hover) don't show the value?

    Thanks,
    Roland