




















































(For more resources related to this topic, see here.)
Both result types and design templates are new concepts introduced in SharePoint 2013. Kate Dramstad, a program manager from the SharePoint search team at Microsoft, describes both concepts in a single, easy-to-remember formula: result types + design templates = rich search experience .
When we perform a search we get back results. Some results are documents, others are pictures, SharePoint items, or just about anything else. Up until SharePoint 2010, all results, no matter which type they were, looked quite the same. Take a look at the following screenshot showing a results page from FAST for SharePoint 2010:
The results are dull looking, can't be told apart, and in order to find what you are looking for, you have to scan the results up and down with your eyes and zero in on your desired result.
Now let's look at how results are displayed in SharePoint 2013:
What a difference! The page looks much more alive and vibrant, with easy distinguishing of different result types and a whole new hover panel, which provides information about the hovered item and is completely customizable.
Search, and its related web parts, makes heavy use of display templates instead of plain old XSLT ( Extensible Stylesheet Language Transformations ). Display templates are basically snippets of HTML and JavaScript, which control the appearance and behavior of search results. SharePoint ships with a bunch of display templates that we can use out of the box, but we can also create our own custom ones.
Similar to master pages, it is recommended to copy an existing display template that is close in nature to what we strive to achieve and start our customization from it. Customizing a display template can be done on any HTML editor, or if you choose, even Notepad. Once we upload the HTML template, SharePoint takes care of creating the companion JavaScript file all by itself.
If we tear apart the results page, we can distinguish four different layers of display templates:
The layers are as follows:
Display templates are stored in a site's master page gallery under the Display templates folder.
Each one of these layers is controlled by display templates. But if design templates are the beauty, what are the brains? Well, that is result types.
Result types are the glue between design templates (UX—user experience) and the type of search result they template. You can think of result types as the brain behind the templating engine.
Using result types enables administrators to create display templates to be displayed based upon the type of content that is returned from the search engine. Each result type is defined by a rule and is bound to a result source. In addition, each result type is associated with a single display template.
Just like display templates, SharePoint ships with it a set of out of the box result types that match popular content. For example, SharePoint renders Word document results using the Item_Word.html display templates within any result source if the item matches the Microsoft Word type of content. However, if an item matches the PDF type of content, the result will be rendered using the Item_PDF.html display template.
Defining a result type is a process much like creating a query rule. We will create our first result type and display template towards the end of the article.
Both result types and display templates are used not only for search results, but also for other web parts as well, such as the Content Search Web Part.
The Content Search Web Part (CSWP) comes in handy when we wish to show search-driven content to users quickly and without any interaction on their side.
When adding a CSWP we have two sections to set: Search Criteria and Display Templates . Each section has its unique settings, explained as follows:
Since our content is not images, rendering the control as Slideshow makes no sense.
Display templates allow us to change the look of our results instantly. While playing around with the out-of-the-box display templates is fun, extending them is even better. If you look at the Two lines template that we chose for the CSWP, it seems kind of empty. All we have is the document type, represented by an icon, and the name of the document. Let's extend this display template and add the last modified date and the author of the document to the display.
As we mentioned earlier, the best way to extend a display template is to copy and paste a template that is close in nature to what we wish to achieve, and customize it. So, as we wish to extend the Two lines template, open SharePoint Designer, navigate to Master Page Gallery | Display Templates | Content Web Parts of the site you previously added the CSWP, and copy and paste the Item_TwoLines.html file into the same folder. Rename the newly created file to Item_TwoLinesWithExtraInfo.html. As soon as you save the new filename, refresh the folder. You'll notice that SharePoint automatically created a new file named Item_TwoLinesWithExtraInfo.js. The combination of the HTML and JavaScript file is what makes the magic of display templates come to life. Edit the Item_TwoLinesWithExtraInfo.html file, and change its title to Two Lines with Extra Info.
The first code block we should discuss is the CustomDocumentProperties block. Let's examine what it holds between its tags:
<mso:CustomDocumentProperties> <mso:TemplateHidden msdt_dt="string">0</mso:TemplateHidden> <mso:ManagedPropertyMapping msdt_dt="string">'Link URL'{Link URL}:'Path','Line 1'…</mso:ManagedPropertyMapping> <mso:MasterPageDescription msdt_dt="string">This Item Display Template will show a small thumbnail…</mso:MasterPageDescription> <mso:ContentTypeId msdt_dt="string">0x0101002039C03B61C64EC4A04F5361F385106603</ mso:ContentTypeId> <mso:TargetControlType msdt_dt="string">;#Content Web Parts;#</mso:TargetControlType> <mso:HtmlDesignAssociated msdt_dt="string">1</mso:HtmlDesignAssociated> <mso:HtmlDesignConversionSucceeded msdt_dt="string">True</mso:HtmlDesignConversionSucceeded> <mso:HtmlDesignStatusAndPreview msdt_dt="string">https://hippodevssp.sharepoint.com/search/_ catalogs/masterpage/Display%20Templates/Content%20Web%20Parts/Item_ TwoLinesWithExtraInfo.html, Conversion successful.</mso:HtmlDesignStatusAndPreview> </mso:CustomDocumentProperties>
The most important properties from this block are:
Since we wish to display the author and the last modified date of the document, let's add these two managed properties to the ManagedPropertyMapping property. Add the following snippet in the beginning of the property, as follows:
<mso:ManagedPropertyMapping msdt_dt="string">'Author:'Author','LastModified':
'LastModifiedTime',… </mso:ManagedPropertyMapping>
We mapped the Author managed property to the Author key, and the LastModifiedTime managed property to the LastModified key. Next, we will discuss how to actually use the new properties.
Using the newly added properties is done using plain old JavaScript.
<div id="TwoLines">
var author = $getItemValue(ctx,"Author");
var last = $getItemValue(ctx,"LastModified");
Remember that each result is rendered using this display template, so the author and the last variables are unique for that one item that is being rendered.