Add a Link to the Search Result List or Library into Your Display Template

Elio Struyf

by Elio Struyf on 2/26/2015

Share this:
Print

Article Details

Date Revised:
2/26/2015

Applies to:
Display Template, search result list


A lot of users want to get quick list 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.

<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 explained how you can check if the last row is rendered. Here is the link to that blog: 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:

<!--#_
// 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:

Link to the Search Result List Display Template

Link to the Search Result Library Display Template

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 I created for this post on GitHub: Item display template with view all link to list or library

 

 



Topic: Business Productivity

Sign in with

Or register