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