Monday, October 24, 2016

Displaying search results as a list view with document preview, sorting & filtering in SharePoint

The question was simple, please make a search display template that looks as much as a SharePoint list as possible. The template needs sorting and filtering options, a document preview and the possibility to show extra, non crucial information within the document preview.

We found out that there are few solutions available for displaying the results as a list view, filtering and document preview. But most of them do one or the other while we needed all of them combined. So we decided to build it ourselves. The files we started building from can be found at the following GitHub link, these will lay the groundwork for displaying the results in list format. The folder will contain several files, but we are only going to use Control_List_Table.html and Item.List.Item.html.

To start off, we wanted to be sure the customer knew that we would not implement every functionality of a list, this would take us to far.

Adding the display template


The files you downloaded can be uploaded in the Content Web Parts folder under Display Templates folder. The exact upload location of the display templates does not matter, as long as it is in the Display Templates folder under the masterpage gallery. The meta data fields which show up when the files are uploading, can be ignored.

JsFiles


As seen in the screenshot above, on the right side, SharePoint will automatically generate one JavaScript file for each item uploaded. When you modify the html files, make sure to publish the files. This can be done by right clicking on the items you just uploaded and clicking on ‘Publish’. Else you might find that not everyone can see your changes.

Before the display template can be configured, the search managed properties that will be used in the display template must be configured correctly. The display template has sorting functionality, in order for this to work, the sorting option on the managed properties that need to be sortable must be selected.

Modifications

document previewproperties


As you can see in the screenshot, we added a document preview to the list for every item. The document preview will show up if you click on the ellipsis of an item.  The buttons you see when the preview is open are also added and the information displayed (like author) is configurable in the web part which we will discuss further in this blog.

Another thing we added are the sorting options hidden behind that little triangle next to the column name, as in a list. This is done as following: 

sorting

One of the downsides of trying to copy something, is that you can’t copy it for 100%. One of those things is that you can’t sort very well on for example a date. In the search results the results are not seen as type they are, but just as text. So if you want to sort a date column, it will not work properly because it is not seen as a date, but as a text type.

Filtering can be done by connecting a search box web part to the content search web part. This will filter the search results.

setsortable

The snippet below will render the menu when clicking on the small triangle in the column header.


/*********************************
* Header menu *
*********************************/
spikes.Header.Menu = function() {
return {
showMenu: function(element) {
var headerElement = element.parentElement.parentElement;
var menuElements = headerElement.getElementsByClassName("ms-core-menu-box");
if (menuElements.length > 0) {
var menuElement = menuElements[0];
menuElement.style.left = element.offsetLeft + 17 - 127 + "px";
menuElement.className = menuElement.className.replace('hidesortmenu','');
}
}
};
}();

Display template configuration


To use the display template, you need to add a content search web part to a page in SharePoint and configure it correctly.

As we discussed earlier, you can change the information displayed in the document preview by adding properties like in the screenshot.

The properties under HeaderTitles will change the name of the columns of the search results. If it is left empty, the managed property names will be used as column names. We added this functionality because Managed properties don't always have a logical name.

The PreviewDialogTitle option is used in order to set the value of a certain managed property of the document as title of the document preview.

The managed properties that are not important, and don't need their own column can still be visualized on the preview panel.

The final result will look something like this.

fixed_voorbeelddisplaytemplate

Thanks for reading.


by Rens O via Everyone's Blog Posts - SharePoint Community

No comments:

Post a Comment