Displaying Hyperlink Fields with Content Search Web Parts

christineSharePoint, Templates & Downloads2 Comments

In SharePoint, Content Search web parts allow you to style links from a link list however you like. This is useful for publishing site navigation where you want a more polished look (these have a hover effect with a description not shown here as well).

links

Hyperlinks from a link list or from a column type of hyperlink are problematic in display templates because they hold both the URL and the hyperlink text in one property. In the Content Search web part they will just reload the same page when clicked instead of loading the URL of the link by default. You can fix this by splitting the URL from the link text as below, then using that variable as the href in your anchor tag.

var linkURL = $urlHtmlEncode(ctx.CurrentItem.LinkURL);

linkURL = linkURL.split(",")[0];

Anchor tag:

<a class="line1LinkPortalv2" href="_#= linkURL =#_" id="_#= line1LinkId =#_">_#= line1 =#_</a>

Here’s the property mappings in the CSWP properties:

linkurl mapping

Title will display the link text from the link list, and CommentsOWSMTXT will display the link description.

2 Comments on “Displaying Hyperlink Fields with Content Search Web Parts”

  1. jmc

    You can also just use the URLOWSURLH managed term as the Link URL in the Property Mappings of the web part.

    What I have not yet been able to achieve is configuring the SORTING so that the results sort on the Description portion of the URL value.

    1. christine

      Good to know! I have had the same sorting issue, may work around it by using a workflow to put the link text in a separate field and sort on that one instead. Haven’t gotten around to trying it but it’d probably work.

Leave a Reply

Your email address will not be published. Required fields are marked *