Configuring hover panel with custom display tempalte

In this one project there was a requirement to change the search result, we had to add some fields to represent the summary of the record for result of type document and as you might know that you can almost do anything with display templates. We created a display template to show those summary fields. Now, as you know SharePoint has different display templates for different types of documents, like there is a separate display template for Word, PowerPoint, PDF etc. Each of these display templates has a HoverPanel display template associated with them. So, our requirement was to display the same set of summary fields regardless of the document type, and also to have hover over functionality intact. In the normal display template the hover URL is specified and glued with the display template with the following set of lines,

var hoverUrl = "~sitecollection/_catalogs/masterpage/Display Templates/Search/Item_KH_WebPage_HoverPanel.js";           
ctx.currentItem_ShowHoverPanelCallback = Srch.U.getShowHoverPanelCallback(itemId, hoverId, hoverUrl);
         
So, now to handle each different file and show the correct HoverPanel we decided to get the file extension. The following line would give the file extension,

var fileType = ctx.CurrentItem.FileType.toLowerCase();

This will return the extension of the file and then you can conditionally apply the HoverPanel, like below,

if(fileType == "pdf"){
        var hoverUrl = "~sitecollection/_catalogs/masterpage/Display Templates/Search/Item_PDF_HoverPanel.js";
        ctx.CurrentItem.csr_OpenApp ="PdfFile.OpenDocuments";
        displayTemplateClass="PDFItem";
 }
 else if(fileType == "doc" || fileType == "docx"){
        var hoverUrl = "~sitecollection/_catalogs/masterpage/Display Templates/Search/Item_Word_HoverPanel.js";
        ctx.CurrentItem.csr_OpenApp="word";
        displayTemplateClass="WordItem";
 }
 else if(fileType == "xls" || fileType == "xlsx"){
        var hoverUrl = "~sitecollection/_catalogs/masterpage/Display Templates/Search/Item_Excel_HoverPanel.js";
        ctx.CurrentItem.csr_OpenApp = "excel";                                                            
        displayTemplateClass="ExcelItem"
 }
 else if(fileType == "ppt" || fileType == "pptx"){
        var hoverUrl = "~sitecollection/_catalogs/masterpage/Display Templates/Search/Item_PowerPoint_HoverPanel.js";
        ctx.CurrentItem.csr_OpenApp ="powerpoint";
        displayTemplateClass="PowerPointItem";
 }

I have taken the values for these two parameters, csr_OpenApp and displayTemplateClass from the HoverPanel display templates of the document types.
Once we have decided which HoverPanel to use we will bind it like below,                                          ctx.currentItem_ShowHoverPanelCallback = Srch.U.getShowHoverPanelCallback(itemId, hoverId, hoverUrl);

ctx.currentItem_HideHoverPanelCallback = Srch.U.getHideHoverPanelCallback();

And now we apply the displayTemplateClass and HoverPanel to our main div like below,

<div id="_#= $htmlEncode(itemId) =#_" name="Item" data-displaytemplate="_#= displayTemplateClass =#_" class="ms-srch-item" onmouseover="_#= ctx.currentItem_ShowHoverPanelCallback =#_" onmouseout="_#=  ctx.currentItem_HideHoverPanelCallback =#_">
_#=ctx.RenderBody(ctx)=#_                                     
<div id="_#= $htmlEncode(hoverId) =#_" class="ms-srch-hover-outerContainer"></div>
</div>              
                        
This is good as long as file type is Word, Excel, PowerPoint or PDF but what if we want the result is a video, well can't we just add a conditional statement to handle that like below,                                  

//https://support.office.com/en-us/article/Video-formats-that-work-in-Office-365-Video-dd1af01c-fd8e-4640-b17b-93ee02b9b817
else if(fileType == "3gp" || fileType == "3g2" || fileType == "3gp2" || fileType == "asf" || fileType == "mts" || fileType=="m2ts" || fileType == "avi" || fileType == "mod" || fileType == "ts" || fileType == "vob" || fileType == "xesc" || fileType == "mp4" || fileType == "mpeg" || fileType == "mpg" || fileType == "m2v" || fileType == "ismv" || fileType == "wmv"){
        var hoverUrl = "~sitecollection/_catalogs/masterpage/Display Templates/Search/Item_Video_HoverPanel.js";              
        displayTemplateClass="VideoItem";
}
                                               
Well we have to, but that's not it. I won't render the HoverPanel as it should for videos by just writing that, so what’s missing?
What missing is, we need to pass the managed properties that are require to render video HoverPanel,

{File Extension}:&#39;SecondaryFileExtension&#39;,&#39;Author&#39;,&#39;ContentTypeId&#39;,&#39;Created&#39;,&#39;PeopleInMedia&#39;,&#39;MediaDuration&#39;,&#39;VideoFileURL&#39;{Video File URL}:&#39;UserEncodingURL;ExternalMediaURL;DefaultEncodingURL;&#39;

Include these set of properties to managed property section and you will be good.

Thanks for reading!!

Comments

Popular posts from this blog

PnP Modern Search Extensibility: Creating Custom Layouts

PnP Modern Search Extensibility: Creating Handlebar Helpers

PnP Modern Search Extensibility