I wrote a custom display template for a content search web part that shows recent documents edited or modified by a user. I added hover functionality as well to allow users to hover.

For some reason though, PDF files are not opening when clicked from the web part. It just reloads the page and does not open at all. All other files are opening fine and I can edit as well like word file, excel etc.

Below is the Display template code I wrote by copy pasting code from two lines template initially and then added other customization inside:

<html xmlns:mso="urn:schemas-microsoft-com:office:office" xmlns:msdt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"> 
<title>TE Item RecentDocs</title>

<!--[if gte mso 9]><xml>
<mso:TemplateHidden msdt:dt="string">0</mso:TemplateHidden>
<mso:ManagedPropertyMapping msdt:dt="string">&#39;Link URL&#39;{Link URL}:&#39;Path&#39;,&#39;Line 1&#39;{Line 1}:&#39;Title&#39;,&#39;Line 2&#39;{Line 2}:&#39;&#39;,&#39;FileExtension&#39;,&#39;ServerRedirectedEmbedURL&#39;,&#39;ServerRedirectedEmbedURL&#39;,&#39;HitHighlightedProperties&#39;:&#39;HitHighlightedProperties&#39;,&#39;HitHighlightedSummary&#39;:&#39;HitHighlightedSummary&#39;,&#39;ParentLink&#39;:&#39;ParentLink&#39;</mso:ManagedPropertyMapping>
<mso:MasterPageDescription msdt:dt="string">This Item Display Template will show a preview and properties for Record Center Customer Documents</mso:MasterPageDescription>
<mso:ContentTypeId msdt:dt="string">0x0101002039C03B61C64EC4A04F5361F385106603</mso:ContentTypeId>
<mso:TargetControlType msdt:dt="string">;#SearchResults;#Content Web Parts;#</mso:TargetControlType>
<mso:HtmlDesignAssociated msdt:dt="string">1</mso:HtmlDesignAssociated>
<mso:HtmlDesignConversionSucceeded msdt:dt="string">True</mso:HtmlDesignConversionSucceeded>
<mso:HtmlDesignStatusAndPreview msdt:dt="string">https://***.sharepoint.com/_catalogs/masterpage/Display Templates/Content Web Parts/TE_Item_RecentDocs.html, Conversion successful.</mso:HtmlDesignStatusAndPreview>
<mso:CrawlerXSLFile msdt:dt="string"></mso:CrawlerXSLFile>
<mso:HtmlDesignPreviewUrl msdt:dt="string"></mso:HtmlDesignPreviewUrl>


            Warning: Do not try to add HTML to this section. Only the contents of the first <div>
            inside the <body> tag will be used while executing Display Template code. Any HTML that
            you add to this section will NOT become part of your Display Template.
        $includeLanguageScript(this.url, "~sitecollection/_catalogs/masterpage/Display Templates/Language Files/{Locale}/CustomStrings.js");

        Use the div below to author your Display Template. Here are some things to keep in mind:
        * Surround any JavaScript logic as shown below using a "pound underscore" (#_ ... _#) token
        inside a comment.

        * Use the values assigned to your variables using an "underscore pound equals"
        (_#= ... =#_) token.

    <div id="RC_CustomerDocuments">

var id = ctx.ClientControl.get_nextUniqueId();
var itemId = id + Srch.U.Ids.item;
var hoverId = id + Srch.U.Ids.hover;
var hoverUrl = "~sitecollection/_catalogs/masterpage/Display Templates/Search/Item_Word_HoverPanel.js";
$setResultItem(itemId, ctx.CurrentItem);
ctx.currentItem_ShowHoverPanelCallback = Srch.U.getShowHoverPanelCallback(itemId, hoverId, hoverUrl);
ctx.currentItem_HideHoverPanelCallback = Srch.U.getHideHoverPanelCallback();

var encodedId = $htmlEncode(ctx.ClientControl.get_nextUniqueId() + "_2lines_");

var linkURL = $getItemValue(ctx, "Link URL");

var iconURL = Srch.ContentBySearch.getIconSourceFromItem(ctx.CurrentItem);

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

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";
var loadSiteFrame = true;
ctx.CurrentItem.csr_Path = ctx.CurrentItem.Path;
var serverRedirectedEmbedURL = $getItemValue(ctx, 'ServerRedirectedEmbedURL');

        <div class="cbs-Item" name="Item" id="_#= $htmlEncode(itemId) =#_" data-displaytemplate="RC_CustomerDocuments" onmouseover="_#= ctx.currentItem_ShowHoverPanelCallback =#_" onmouseout="_#= ctx.currentItem_HideHoverPanelCallback =#_">
            <div id="wrapper">
                <div class="ms-srch-hover-outerContainer" id="_#= $htmlEncode(hoverId) =#_" ></div>
            <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 =#_" />
            <div class="cbs-Detail" id="_#= dataContainerId =#_">
                <a class="cbs-Line1Link ms-noWrap ms-displayBlock" style="word-wrap:break-word !important;width:200px" href="_#= linkURL =#_" title="_#= $htmlEncode(line1.defaultValueRenderer(line1)) =#_" id="_#= line1LinkId =#_">_#= line1 =#_</a>
                <div class="cbs-Line2 ms-noWrap" title="_#= $htmlEncode(line2.defaultValueRenderer(line2)) =#_" id="_#= line2Id =#_">_#= line2 =#_</div>

FYI, Inside Property Mappings, I have set the LinkURL to ServerRedirectedURL. Can someone please help me modify this code so that users can also open PDF documents when clicked, thanks in advance.

  • Display template is fine. Tested it at my end and it works. Problem could be somewhere else or because of some other webpart, check the console ? Can you try adding just the CSWP on a blank page and check, it will probably help you isolate the problem Commented Aug 5, 2018 at 18:32
  • Thanks for the feedback gautam. It works for you because LinkURL must be set to Path under Property Mappings for CSWP. If you click on word or excel files, they would download and won't open in browser. If you change the LinkURL attribute to ServerRedirectedURL then it would allow opening all files in browser except pdf files. Can you please test this? I tested on a blank page and it gives me same behavior. So something like this would work: if PDF files are clicked then following should be executed: ctx.CurrentItem.ServerRedirectedURL else ctx.CurrentItem.Path (for word, excel files)
    – mdevm
    Commented Aug 5, 2018 at 20:16
  • FYI, I am only showing documents in the CSWP through query with title and Created set as line 1 and line 2 under property mappings. Also LinkURL is set to ServerredirectedURL.
    – mdevm
    Commented Aug 5, 2018 at 20:18
  • got it, in your code, add this line var linkURL = $getItemValue(ctx, "Link URL"); linkURL.value = linkURL.value + "?web=1"; in the display template. Keep the mapping of LinkURL to Path managed property. This will open the pdf in the browser Commented Aug 6, 2018 at 5:09

1 Answer 1


In SharePoint Online the best way to achieve opening of documents in the browser is by appending web=1 as a query string parameter to the file URL like https://tenant.sharepoint.com/Documents/file.docx?web=1.

Now, for office documents, like Word, Excel, PowerPoint, etc the serverRedirectedEmbedURL property is populated however for other document types like PDF it is null. Therefore, your page was reloaded because it didnt have the href value for PDF documents.

So, instead of checking the file type and using if loop, best option for you would be to simply append ?web=1 to the file path as below. You needn't modify the managed property mapping. When you do this, it will not only open the office documents in the browser but also PDF documents as well.

var linkURL = $getItemValue(ctx, "Link URL"); 
linkURL.value = linkURL.value + "?web=1";

When you do this, it will open documents in the browser using OWA, if possible, but otherwise it will download them.

References - Making a Link to a Document in SharePoint Open in Browser

Link to "open in browser" with Office Web Apps

  • Much Appreciated Gautam, thanks a lot. I hope this will help others as well.
    – mdevm
    Commented Aug 6, 2018 at 13:46
  • Super, Gautam! Respect, thank You much!)
    – Gennady G
    Commented Aug 8, 2019 at 11:00
  • I have a similar requirement. I'm using search results web part .Results will show up from a document library where it has all office documents,PDF,csv files etc..Now those documents or files should not get downloaded ,instead files should open in browser itself.How can I do this?
    – Swetha
    Commented Oct 17, 2019 at 7:14

Your Answer

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

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