Wednesday, February 26, 2014

How to use custom managed properties in display templates in Sharepoint 2013

When create a new display template for search results page or for Content by search web parts, you may need to use your own custom managed properties inside template. First of all you need to create these properties in Search service application and map it to some crawled property. After that it is possible to use it in display template. In order to do it you need to add new mapping to <mso:ManagedPropertyMapping>…</mso:ManagedPropertyMapping> tag inside display template:

<mso:ManagedPropertyMapping msdt:dt="string">'Link URL'{Link URL}:'Path','Line 1'{Line 1}:'Title','Foo'{Foo}:'Foo'</mso:ManagedPropertyMapping>

In this example we added new mapping for managed property Foo, so it will be possible to get value of this property inside display template like this:

   1:  var foo = $getItemValue(ctx, "Foo");

and then display it to end user. This technique will help you to use not only OTB managed properties like Title or Path, but also custom managed properties in display templates.

No comments:

Post a Comment