Alert This post is over a year old, some of this information may be out of date.

Adding a link to the search result list or library into your display template

post

A lot of users want to get an overview of the latest items of a list or library. Some users want to include a link to the list or library from which the items were retrieved. As this link from the search results location may vary, you should not hard code the link back to the item or control template.

Solution

The solution is very easy. By default you have a managed property called ParentLink. You need to add this property to the ManagedPropertyMapping attribute inside your item display template.

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

Next, you should add some extra code to the item template.

Note: You could choose to add extra code to the control template instead but it requires that you write more code.

The link only needs to be shown if the last item gets processed. This means that the code needs to check to see when the last item gets processed. In a post on my blog I showed how you could include alternating row styling. I also explain how you can check if the last row is rendered: Showing Alternating Rows in the Content Search WebPart (Display Template).

To check if the last row is processed, you should include the following code in the Item Template:

1
2
3
4
5
6
7
8
9
<!--#_
// Check if it is the last item to process
var currentItemIdx = ctx.CurrentItemIdx + 1;
if(currentItemIdx === ctx.CurrentGroup.RowCount) {
_#-->
	<div class="view-all"><a href="_#= ctx.CurrentItem.ParentLink =#_" title="View all">View all</a></div>
<!--#_
}
_#-->

Place it at the bottom of your Item Template and you get the following result:

Show image Items with link to list
Items with link to list
Show image Documents with link to library
Documents with link to library

Important: This only works if your items are coming from the same library. Otherwise View all will link to the list or library location of the last item.

Download

You can download the template that I created for this post on GitHub: Item display template with view all link to list or library.

Comments

Back to top