10 min read

(For more resources related to this topic, see here.)

Features of SharePoint 2013 – Result types and design templates

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.

Display templates

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:

  • Filters layer : In the preceding screenshot they are highlighted with the green border on the left and numbered 1. This layer shows the new refinement panel area that is not limited to text alone, but also enables the use of UX elements such as sliders, sliders with graphs, and so on.
  • Control layer : In the preceding screenshot they are highlighted with the red border in the middle and numbered 2. This layer shows that not only results but also controls can be templated.
  • Item layer : In the preceding screenshot they are highlighted with the orange border in the middle and numbered 3. This layer shows that each result type can be templated to look unique. For example, in the screenshot we see how a site result (the first result), conversation results (next three results), and image result (last one) looks like. Each result type has its own display template.
  • Hover panel layer : In the preceding screenshot, they are highlighted with the blue border on the right and numbered 4. They are introduced in SharePoint 2013, the hover panel shows information on a hovered result. The extra information can be a preview of the document (using Office Web Apps), a bigger version of an image or just about anything we like, as we can template the hover panel just like any other layer.

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

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.

Styling results in a 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:

  1. The search criteria section is equivalent to the result type. Using the Query Builder we tell the web part which result type it should get. The Query Builder enables us to either choose one of the built-in queries (latest documents, items related to current user, and so on) or build our own. In addition, we can set the scope of the search. It can either be the current site, current site collection, or a URL. For our example, we will set the query to be Documents(System) , meaning it searches for the latest documents, and the scope to Current site collection :

  2. Next, we set the display template for the control holding the results. This is equivalent to the Control layer we mentioned earlier. The CSWP provides three control templates: List , List with Paging , and Slideshow . The control templates change the way the container of the items looks. To compare the different templates, take a look at how the container looks when the List template is chosen:

  3. And the following screenshot displays how the exact same list looks when the Slideshow template is chosen:

    Since our content is not images, rendering the control as Slideshow makes no sense.

  4. Last but not least, we set the Item display template. As usual, SharePoint comes with a set of built-in item templates, each designated for different item types. By default, the Picture on left, 3 lines on right item display template is selected. By looking at the preceding screenshot we can see it’s not right for our results. Since we are searching for documents, we don’t have a picture representing them so the left area looks quite dull. If we change the Item display template to Two lines we will get a much more suitable result:

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.

Creating a custom display template

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.

Getting the new properties

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:

  • ManagedPropertyMapping : This property holds all the managed properties that our display template will have access to. The properties are organized in the key:value format. For example, if we wish to make use of the Author property, we will declare it as ‘Author’:’Author’. The value can be a list of managed properties, so if the first one is null, the mapping will be done using the second one, and so on.
  • ContentTypeId : This property sets the content type of the display template. The specific value recognizes the file as a display template.
  • TargetControlType : This property sets the target of the display template. In our example it is set to Content Web Parts , which means the search content web part and any other related search content web part. Other possible values are SearchBox , SearchHoverPanel , SearchResults , and so on.

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.

Getting the values of the new properties

Using the newly added properties is done using plain old JavaScript.

  1. Scroll down a bit until you see the following opening div statement:

    <div id="TwoLines">

  2. The div tag begins with what seems to be a comment markup (<!–), but if you look closer you should recognize that it is actually JavaScript. By using built-in methods and client object model code, display templates can get any information out of SharePoint, and of the outside world. The getItemValue method is in charge of getting content back based on a given managed property. That means if we wish to get the author of a result, and we set the key to the managed property to be Author, the following line of code will get it:

    var author = $getItemValue(ctx,"Author");

  3. The same goes for the last modified date. We used the key LastModified for the managed property, and hence the following line of code will be used:

    var last = $getItemValue(ctx,"LastModified");

  4. Add these two lines just above the closing comment statement markup (_#–>).

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.

LEAVE A REPLY

Please enter your comment!
Please enter your name here