Thursday, 7 April 2016

SharePoint 2013: Image Preview in Search Results - Part II

This is the second post on previewing images in SharePoint search results. The first post is here.

In the first post I demonstrated a way to display image previews in search results and resolved two problems. But what about the images in a Picture Libary? I didn't see them appear in search results as pictures until I added the file types. Shouldn't there be special results for Picture types?

UPDATE: Get the source files here


Previewing of Images Stored In Picture Libraries

Problem #1: Images in Picture Libraries Returned as Files or List Items
After going through the steps in Part I , even images stored in Picture Libraries are returned as files themselves. The hover works fine but you should be able to take advantage of the image result type.

Solution to Problem #1: Copy and Configure the Image Result Type

In your Search Center site collection select Site settings from the settings menu:

Under Site Collection Administration, click on Search Result Types:


Scroll down and find Image. Use the drop-down menu and select Copy:


On the Add Result Type page, select Picture Item udner What should these results look like?


Click Save:
Run a search for an item in a Picture Library:



 
There is a preview image right in the results! That's great but that didn't happen in image results from other types of libraries - onto Problem #2.
 
Problem #2: Image Search Results Not Consistent
Now the results from a Picture Library and non-Picture Library look different. It is not consistent for the user:

Solution to Problem #2: Modify the Display Template for the Result. 
This time you need to edit the Item_Picture.html file.

Simply add an else statement to the if in the middle of the code:

 else {
           ctx.CurrentItem.csr_PreviewImage = ctx.CurrentItem.Path;
      }


(If you only see .js files, the .js code to  modify is at the bottom of this post)

Save the file and run a search again:

 


Now all images that are returned have a preview image in the results!!!!

But now the hover is the Picture Library List item hover, not the one I modified in the first post. So in the next and last post of this series I modify that hover to include a larger preview!



If you are not running search in a Search Center Site Collection and/or you only see .js files in your Search Display Templates, here is the resultant .js code to paste over after the render header in Item_Picture.js:

    if(!Srch.U.n(ctx.CurrentItem.PictureThumbnailURL) && !ctx.CurrentItem.IsContainer) {
     ctx.CurrentItem.csr_PathLength = Srch.U.pathTruncationLengthWithPreview;
                    ctx.CurrentItem.csr_PreviewImage = ctx.CurrentItem.PictureThumbnailURL;
       }
      else {
           ctx.CurrentItem.csr_PreviewImage = ctx.CurrentItem.Path;
      }


No comments:

Post a Comment