Tuesday, December 23, 2014

How to enable Content Search Web Part Display Templates for Search Result Web Part

By default the out of the box control and display templates have been marked to work with their respective web parts. You can however re-use the templates across both web parts, as long as you ensure any script dependency is fixed. For example the CSWP has a reference to _layouts/15/search.cbs.js which must be added to the page in order for the Search Result Web Part to use the CSWP display templates.

The good thing is that the CSWP display templates are installed even though you don’t have the CSWP available. Here are the steps you need to perform to use the “Two Lines” CSWP display template with the Search Result Web Part.
  • Navigate to site settings
  • Under “Web Designer Galleries” go to “Master Pages”
  • Go to the “Display Templates” folder
  • Go to the “Content Web Parts” and you should see this listing
    image_thumb4

  • Edit properties on “Control_List.js”
  • Check “Search Results”
    image_thumb6

  • Click “Save”
  • Apply the same property change to “Item_TwoLines.js”
You have now enabled the display templates for use in the Search Result Web Part, and it’s time to set it up on a page.
Navigate to the page where you want the listing. In this sample I’m using the home page of a team site.
  • Edit the page
  • Insert a “Script Editor” web part on the page
    image_thumb13
  • Add a reference to search.cbs.js by adding <script type="text/javascript" src="/_layouts/15/search.cbs.js"></script> If you skip this part the display template will get an error with a missing reference to a javascript function.

    image_thumb17
  • Insert a “Search Result” web part below the “Script Editor” web part
    image_thumb20
  • Edit the web part properties of the “Search Result” web part and click “Change query”
  • Click “Switch to Quick Mode” (for this demo) and you should see this settings page
    image_thumb23
  • Click “Ok”
  • Expand the “Display Templates” section on the web part properties and pick the control and display template previously enabled
    image_thumb30
  • Click “Ok” and save the page changes and you should see something similar to this
    image_thumb34

5 comments:

  1. Worked like a charm! Thank you very much for the detailed walkthrough, this will come in very handy.

    ReplyDelete
    Replies
    1. You might want to load the .js file via $includeScript in the control template instead of a script part, but it shows the dependency, which was the key point. And hope it comes in handy some time :)

      Delete
  2. How to use Display template/Item Template acorss WebApplication/SiteCollection Level ?

    ReplyDelete
    Replies
    1. You have to deploy them to all applicable site collections.

      Delete
  3. Hi Mikael

    This is great, thanks for sharing.

    Just a quick note for readers who are attempting this on a publishing site. There is a HTML file associated with each .js file. The properties need to be editied on that file, not directly on the .js file.

    Cheers

    Adrian

    ReplyDelete