1

I am working on customizing display templates for the Content Search web part. I have a custom item template, and a custom control template. I'm going to be setting up several similar web parts, and in each of them the query is going to be changed to point only at a single list.

What I want to do is put a "View all" link at the bottom of the control template that goes to the list that the search web part is displaying items from.

Is there a way to do some markup in the control template that would expose a text box in the web part editor part so I can configure the URL that the "View all" link should point to?

1 Answer 1

2

That is only possible if you extend the CSWP, but you configure the query to point to a single list you say. Then it is not required to have such a property. What you can do is add some code to your item display template to show a "view all" link once the last item got processed. This link needs to point to the list or library, which can be retrieved from the ParentLink property.

So in your managed property mappings attribute, add the ParentLink property:

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

At the end of the item template add the following code to insert the view all link on your page:

<!--#_
    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>
<!--#_
}
_#-->

Here is the whole example:

<html xmlns:mso="urn:schemas-microsoft-com:office:office" xmlns:msdt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"> 
<head>
<title>Two lines - View All</title>

<!--[if gte mso 9]><xml>
<mso:CustomDocumentProperties>
<mso:TemplateHidden msdt:dt="string">0</mso:TemplateHidden>
<mso:ManagedPropertyMapping msdt:dt="string">'Link URL'{Link URL}:'Path','Line 1'{Line 1}:'Title','Line 2'{Line 2}:'','FileExtension','SecondaryFileExtension','ParentLink'</mso:ManagedPropertyMapping>
<mso:MasterPageDescription msdt:dt="string">This Item Display Template will show a small thumbnail icon next to a hyperlink of the item title, with an additional line that is available for a custom managed property.</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:CustomDocumentProperties>
</xml><![endif]-->
</head>

<body>
    <div id="TwoLines">
<!--#_
var encodedId = $htmlEncode(ctx.ClientControl.get_nextUniqueId() + "_2lines_");

var linkURL = $getItemValue(ctx, "Link URL");
linkURL.overrideValueRenderer($urlHtmlEncode);
var iconURL = Srch.ContentBySearch.getIconSourceFromItem(ctx.CurrentItem);

var line1 = $getItemValue(ctx, "Line 1");
var line2 = $getItemValue(ctx, "Line 2");
line1.overrideValueRenderer($contentLineText);
line2.overrideValueRenderer($contentLineText);

var containerId = encodedId + "container";
var pictureLinkId = encodedId + "pictureLink";
var pictureId = encodedId + "picture";
var dataContainerId = encodedId + "dataContainer";
var line1LinkId = encodedId + "line1Link";
var line1Id = encodedId + "line1";
var line2Id = encodedId + "line2";
_#-->
        <div class="cbs-Item" id="_#= containerId =#_" data-displaytemplate="Item2Lines">
            <a class="cbs-ItemLink" title="_#= $htmlEncode(line1.defaultValueRenderer(line1)) =#_" id="_#= pictureLinkId =#_">
                <img class="cbs-Thumbnail" src="_#= $urlHtmlEncode(iconURL) =#_" alt="_#= $htmlEncode(line1.defaultValueRenderer(line1)) =#_" id="_#= pictureId =#_" />
            </a>
            <div class="cbs-Detail" id="_#= dataContainerId =#_">
                <a class="cbs-Line1Link ms-noWrap ms-displayBlock" href="_#= linkURL =#_" title="_#= $htmlEncode(line1.defaultValueRenderer(line1)) =#_" id="_#= line1LinkId =#_">_#= line1 =#_</a>
<!--#_
if(!line2.isEmpty)
{
_#-->
                <div class="cbs-Line2 ms-noWrap" title="_#= $htmlEncode(line2.defaultValueRenderer(line2)) =#_" id="_#= line2Id =#_">_#= line2 =#_</div>
<!--#_
}
_#-->
                </div>
        </div>

<!--#_
    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>
<!--#_
}
_#-->
    </div>
</body>
</html>
3
  • Is there another property on the item like ParentLink that would be the URL of the item's parent web? Jan 9, 2015 at 15:56
  • You have SPWebURL on SharePoint Online, but you could also get it on-premises: eliostruyf.com/… Jan 9, 2015 at 20:18
  • Ah, perfect! I'm developing on-prem but ultimately it will all be deployed online, so that's exactly what I need. Thanks again! Jan 9, 2015 at 21:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.