Those who have worked with display templates would have worked with accessing managed properties in the item template of the Content Search Web Part (CSWP). There might be a requirement when you want to access the values of the managed properties of each result item in the control template of your CSWP rather than your item template.
The below code does exactly that:
a. $.each() is the jQuery loop function that is used to loop through each result returned
b. ctx.ListData.ResultTables.ResultRows retrieves the “RelevantResults” results from the 4 result tables available in the CSWP as shown below:
c. function (key, val) is the function that is going to process each search result.
d. val.EmailDate.toString(): Here only we are accessing the managed property called “EmailDate” from the variable “val” which would hold the values of all managed properties of the result item.
Let me you explain a small tip on how to debug the display templates of your Content Search Web Parts in SharePoint in Google Chrome.
There will always going to be 2 files created for your display template. One is the .HTML file in which you would be working and the other is the .JS file which gets automatically created and used by SharePoint once you upload your .HTML file.
a. Open the page in which you have your CSWP.
b. Press Ctrl+Shift+J. This will open your developer dashboard.
c. Click on “Sources” from the tabs as shown below:
d. Expand _catalogs/masterpage and then Display Templates/Content Web Parts
e. You should be seeing the .JS file of your display template. Click on the .JS file to open it in the workspace.
f. Put a breakpoint anywhere and refresh the page and the page load would pause when the break point is hit in the display template as shown below:
g. From now you can debug like how you do in Visual Studio using F10 and F11.
There might be a requirement when there are no results returned from your CSWP’s query, you would still need to display some default content. Follow the below steps in order to accomplish this:
a. Edit your content search web part.
b. In the tool part, uncheck the option “Don’t show anything when there are no results.”
c. Open the control template of you content search web part, search for the below if condition:
d. The text or the HTML content that you give inside the above if condition would appear in your web part when it has no results to display. You may use it to display any custom empty message when there are no results returned.