none
Open PDF results from a Content Search Web Part in a new tab RRS feed

  • Question

  • We have a page in a site with a Search Box web part and a Content Search Web Part to which the results are sent. My users have a minor issue wherein clicking a PDF result, which opens in the same tab, downloading said PDF, and clicking the Back button in their browser (almost all are using Google Chrome) causes the Content Search web part to error out, saying only "Something went wrong" and providing a correlation ID.

    Having told them about clicking with the middle button or Ctrl+Clicking to open in new tabs, I figured I would go the extra mile and the simplest workaround, I thought, would be to force PDF links to open in a new tab using some JavaScript. I have found at least three different sources which provide a quick and easy solution using JavaScript from a Content Editor web part to simply add attributes that will add on to or subtract from PDF anchor tags. Unfortunately this will not work for me as the search results are not loaded on page load, but only after a query has been made.

    In dev tools I can track the global variable g_ExecuteOrWaitJobs to get all the events SharePoint is raising, as well as see that when the search bar works, ProcessQuery is something that runs, but I am at a loss for how to leverage this information to help me.

    How can I use JavaScript to alter these anchor tags to open PDFs in new tabs, and have that process every time a query is made?


    Jonathan Huelman

    Friday, January 11, 2019 3:58 PM

Answers

  • Hi jonathan,

    As you mentioned, the search results are not loaded on page load, but only after a query has been made(so adding code into a content editor web part in this page will not work).

    For your requirement, I would suggest you customize the default(or create a new) display template for your content search web part, and you need to add -target="_blank" to the anchor tag in the custom display template for make the results open in new tab.

    Below article is about how to Upload and apply display templates to the Content Search Web Part, which may be helpful to you:

    https://blogs.technet.microsoft.com/tothesharepoint/2013/05/24/stage-11-upload-and-apply-display-templates-to-the-content-search-web-part/

    Best regards,

    Allen Bai


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Tuesday, January 15, 2019 3:24 AM

All replies

  • Hi jonathan,

    As you mentioned, the search results are not loaded on page load, but only after a query has been made(so adding code into a content editor web part in this page will not work).

    For your requirement, I would suggest you customize the default(or create a new) display template for your content search web part, and you need to add -target="_blank" to the anchor tag in the custom display template for make the results open in new tab.

    Below article is about how to Upload and apply display templates to the Content Search Web Part, which may be helpful to you:

    https://blogs.technet.microsoft.com/tothesharepoint/2013/05/24/stage-11-upload-and-apply-display-templates-to-the-content-search-web-part/

    Best regards,

    Allen Bai


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Tuesday, January 15, 2019 3:24 AM
  • This is precisely what I was looking for. And that link was a big help. Thank you very much!

    Jonathan Huelman

    Wednesday, January 16, 2019 4:03 PM