Freigeben über


How to add "View Library" link to the SharePoint Search results from file share

Recently a customer asked me to improve one little thing.
You know the “view library” link that appears once you search for the files in SharePoint?

These guys had setup a search over a file share, but the library link did not appear there.
Apparently the solution appeared to be rather simple. However, I did not find anything in the internet and the customer did not as well.
I assume you are familiar with the display templates concept in the SharePoint 2013. There templates are used to display search results and actually they are JavaScript files (or, in the case of SharePoint Standard, HTML files that generate the JS files).

So the links are rendered by the Item_CommonHoverPanel_Actions.html
This part renders the “View Library” link.

if(!Srch.U.e(ctx.CurrentItem.ParentLink) && ctx.CurrentItem.csr_ShowViewLibrary) { _#-->

_#= $htmlEncode(Srch.Res.hp_ViewLibrary) =#_

<!--#_
}

While debugging I have found out that the CurrentItem.ParentLink is not filled.
I could not find out why it happens that way for the file shares, however, once I found this, the fix became very simple. I have added this simple piece of code before the block.
//ignia custom code var url = ctx.CurrentItem.csr_Path; if (url.indexOf('file://') >=0) { var lastIndexOfSlash = url.lastIndexOf('/'); if (lastIndexOfSlash > 0) { ctx.CurrentItem.ParentLink = url.substring(0,lastIndexOfSlash); } }

Simple!