20 February 2015

Display Templates - Tips & Tricks

In my previous blogpost I already introduced the SharePoint Display Templates. This blogpost is more like a collection of small things that I ran into while working with Display Templates.

Types of Display Templates

There are different types of Display Templates in SharePoint and of every type there is already a number of templates available in SharePoint. Therefore it is advisable to check if there's any Template already present that fits your needs (For me it's never good enough so I always end up creating my own templates ☺). If you even want to make a tiny difference to the standard templates, don’t change the standard template, but instead copy it and create a new custom one.

You can find an overview of types of- and standard Display Templates here.

Managed Properties

Managed Properties are SharePoint columns which can be retrieved by Display Templates. These come in all shapes and sizes, so there are several different ways to display them. More about that later in this blogpost.

In SharePoint there are more properties than just the Managed Properties, they're called Crawled Properties. It's not possible to retrieve the value of these Crawled Properties directly in the Display Templates. You have to map them to a (existing or newly created) Managed Property. You can read here how to do that.

Personally, I used a Site Collection in SharePoint Online, which means I didn't have access to Central Admin. Instead I created the Managed Properties from within the site collection via this url:
https://[JourDomain].sharepoint.com/_layouts/15/listmanagedproperties.aspx?level=sitecol

Retrieve Managed Properties

In the <head> section of the Item Display Template, there's a <mso:ManagedPropertyMapping msdt:dt="string"> tag. That's where you should define the Managed Properties which you want to retrieve. It looks something like this:
<mso:ManagedPropertyMapping msdt:dt="string">'Link URL'{Link URL}:'Path','Line 1 code'{Line 1}:'Title','Line 2 code'{Line 2}:'Description'</mso:ManagedPropertyMapping>

The syntax to define a Managed Property looks like this: 'Property Display Name'{Property Name}:'Managed Property Name'

  1. Property Display Name is, according to Microsoft, the property name that shows in the Web Part editing pane when the display template is selected. I don't really get what they mean here. I don't see it anywhere. If anyone knows more about this, I'd be happy to hear about it!
    As far as I know, this is the value with which you call the Managed Property in the script on the Display Template.
  2. Property Name is an identifier that uses localized string resources to look up the name of the managed property. It is also the value that appears in the Property Mappings section of the Web Part settings menu. When you edit the settings for a Web Part, you can change this value to change what managed property is associated with the field that appears in the Web Part.

    This property is only useful if the Web Part settings menu has the 'Property Mappings' option. If it has not, you can skip this part of the syntax and just use this:
    'Property Display Name':'Managed Property Name'
  3. Managed Property Name is a string of one or more managed properties, separated by semicolons. At run time, the list is evaluated from left to right, and the first value that matches the name of a managed property of the current search item will have its value mapped to this slot. This enables you to write a display template that can work with multiple item types and that can use consistent rendering if compatible properties are present.

After you map a property, you can get its value in script by using the following code inside the <body> tag:
<!--#_
var title = $getItemValue(ctx, "Line 1 code");
_#-->
For this example, I have partially copied the standard SharePoint TwoLine Item Display Template (and modified it a bit), because it makes it very clear where the three different values show up.

You can use variables within the sections of JavaScript as you typically would, to manipulate variables and create HTML strings to be rendered on the page at run time. But, to reference variables declared in the script directly in the HTML, you must use the following format:
_#= variabele =#_ In case of a text value it makes sense to use this syntax:
_#= $htmlEncode(variabele) =#_ The htmlEncode encodes the characters that can crash a script. For example, a quote (') will be encoded to &#39;.

Take a look in the standard Display Templates library that SharePoint has to see what the possibilities are. Furthermore, this website has a lot of valuable information about scripting in Display Templates.

Displaying images

The image and icon Managed Properties actually consist of just text, a url to be precise. To convert it to a visible image, you have to call the Managed Property inside the src of a <img> tag. It would look something like this:
<img src="_#= variabele =#_" alt="" /> There is a catch here though. SharePoint doesn't like urls with spaces in it… Usually spaces are converted into %20, but unfortunatly not in this case. Therefore you have to make sure that the Picture Library in which the images are stored, does NOT contain spaces. Another option is to put some JavaScript together that turns spaces into %20 (don't know how to do that, never tried).

Displaying dates

This is a nice one as well, because you have the freedom to choose the format you prefer. It could look something like this: _#= variabele.format("d MMMM yyyy") =#_ You can also split up the date. I created a colored square myself, in which I displayed the date in two separate pieces. The day and month are displayed below each other instead of inline: <div class="date-formats">
<span>_#= variabele.format("d") =#_</span> <!-- day in integer -->
<small>_#= variabele.format("MMMM") =#_</small> <!-- month in string -->
</div>

There's an overview of all available date formats here.

Displaying file type icons

File type icons are a special case, because it doesn't work the way you would expect, at least not in Search Result Display Templates (no idea why, but it is what it is). The file type icon is the little icon in the first column, for example in Document Libraries, that indicates the type of document. It can be a MS Word icon, or Excel, PDF, etc.

To get it done anyway, we're just going to steal it out of a ContentBySearch Display Template. Here's how to do that:

  1. Add the following code into the tag of the Control Display Template: <script>
    $includeScript(this.url, "/_layouts/15/search.cbs.js");
    </script>
  2. Add the following line of code to the '<!--#' section in the Item Display Template: var iconUrl = Srch.ContentBySearch.getIconSourceFromItem(ctx.CurrentItem);
  3. Call the variabele in an <img> tag like this: <img src="_#= $urlHtmlEncodeString(iconURL) =#_" />

And we're done! I didn't cover any styling here, so it might be that you still have to fix that. I'll soon release a detailed tutorial about creating a 'My Documents' Display Template, in which I also cover the file type icon.

Well, that's it for now, hopefully this was useful to you ☺

Wil je dit in het Nederlands lezen?

SharePoint 2013 - Display Templates

Display Templates are sort of stand-alone files in which Managed Properties (SharePoint columns, like Title, Description, etc) are being retrieved and HTML, CSS en JavaScript can be processed. These files can be rendered in one or multiple pages of your SharePoint site. They’re used in order to control the display of Web Parts. Display Templates consist of two separate HTML files and with each of these files, a JS file is automatically being generated and attached. You will not be doing anything with these JS files, SharePoint does that automatically.

The first HTML file is the Control Display Template and the second is the Item Display Template. The Control Display Template determines the overall structure of how the results are presented (like lists, lists with paging, slide shows, etc). For example, a <ul> in which you will later render <li>'s.

The Item Display Template determines how each result in the set is displayed (like list items, images, text, ect). That would be the <li> (and its content) which we named in the previous example. Just one <li>? Yes, it’s a template, so a script will loop through and reuses the same <li> over again. The Web Part settings decide how many items are displayed, the templates have nothing to do with that.

The reason there are two separate (Control and Item) files instead of a single one, is the fact that Control- and Item Display Templates can be randomly combined. So they are not necessarily permanent sets. It may well be that you create multiple Item Display Templates for the same Control Display Template, because the Item Templates do use the same markup and styling, but, for example, retrieve the values of different SharePoint columns (Managed Properties).

Nice story, but how do I do this?
Let's start at the beginning. Because I used SharePoint Online myself (where you don't want to deploy from Visual Studio), this blog post will be based on it as well. Therefore we will use the SharePoint Design Manager and Notepad++.

Personally, I like the 'Search Results' method best. It is just one Web Part (the Search Results Web Part), with which you can literally and metaphorically go anywhere you want. You just use a different Search Results Web Part for every Web Part you want on the page, give it its own query and let the Display Templates decide how and what is displayed. Thus, the same Web Part can look completely different and display other data in every instance.
I have to admit that I didn’t figure out every kind of Display Template there is, so I don’t know if in some scenarios there are better alternatives. What I do know is that Search Results work just fine for what I needed to do with it.

Start

You can find the Search Results Display Templates here:
https://[YourDomain].sharepoint.com/_catalogs/masterpage/Display%20Templates/Search/

If you have a child SiteCollection which you want to use, the path would be something like this:
https://[YouDomain].sharepoint.com/sites/[YourSiteCollection]/_catalogs/masterpage/Display%20Templates/Search/

Map Network Drive

The easiest way of working is to map a network drive for the Master Page Gallery of you SharePoint site, so you can access the files via Explorer on your computer. Read this to find out how to do that.
Note that: You have to use IE, you have to check the 'Keep me signed in' option, You have to add your SharePoint domain/site to your trusted sites and you have to open a random library using the 'Open with Explorer' option in the ribbon (only possible in IE). After these steps you should be able to map the MP Gallery. As long as you are logged into the site (via the browser that is), you can use Explorer to access the files in the MP Gallery. If you log out, access will be denied in Explorer as well. In the MP Gallery you can navigate to Display Templates -> Search for the right location.

Starter Display Templates

Below I've put links to some starter templates you can use as a base. The Control Display Template contains a heading and a container for the items, the Item Display Template shows a Title and a Description.


Indeed, the JS files are missing. This is because SharePoint creates those itself, as soon as you upload the files above to the Display Template Gallery. You have to use the SharePoint user interface (the browser) to upload the files though. Unfortunatly, via the mapped network drive the JS files will not be generated at all.

Where to go from here?

It would've become a very very long piece of text if I was going to write down detailed howto’s in this blogpost, so I've created a few separate blogposts to explain a few options:

Finally, I wrote a blogpost with some tips & tricks. Obviously I ran into some issues when I started working with the Display Templates myself. To save you the grief, I wrote the solutions to these issues into this blogpost and also some best practices to prevent problems.

What is also very interesting, is this web page. You can read everything about how and why and the Display Template structure.

Wil je dit in het Nederlands lezen?