Friday, February 22, 2013

SharePoint 2013: Image Preview in Search Results - Part I

I was discussing SharePoint Search previews of images on Twitter with Jasper Oosterveld . The discussion made me look into image previews in search results. As a result, I looked at various options and functionality to this regard. I am splitting these posts into at least two parts so I can get part of my solution out as soon as possible. This post solves the previewing of images that are not in Picture Libraries.

UPDATE: Get the source files here

Previewing of Images Not Stored in Picture Libraries

Problem #1: Image Search Results Are List Items

When images are stored in "regular" document libraries such as Site Assets, they are uploaded as documents. When the library is crawled, the results are the actual list item and not the image itself. Even if you add the Image or Picture content type to the library and modify the item, the result is still treated like a list item.

Solution to Problem #1:  Add Image File Types to Search
The reason the images are coming back as items is because image file types such as .jpg and .gif are not in the list of the search file types.

So you need to go to Central Administration and modify the Search Service Application. On the left hand side click on File Types:


On the File Types page, click on New File Type:



Enter an image file type such as jpg and click ok:



Repeat the process for other image types you want to handle.

Now go to the document library that contains the images and select Library settings from the Library top ribbon:


On the Settings page, click on Advanced Settings:


Scroll down and find the Reindex Document Library button and click it:

 
In the dialog that appears, click Reindex Document Library:

Click OK on the Advanced Settings page.
 
Now return to your Search Service Application and run an incremental crawl on the main content source:
 
 
After the crawl is completed, the search results of the images should appear as their filename instead of a list item. That solves that problem but the hover is still the Default hover template and doesn't show an image preview.
 

Problem #2: Image Search Results Hover Doesn't Show Preview
Now that the search is crawling image types, the results display the image file but the hover is using the default item template and no image appears.
  
 
UPDATE: You do not need to go through the modification of the default hover although I did in the beginning before I figured out all that was happening. The overall cause of this issue is that Image result type uses the Default Item template and not the Picture template. My solutions in Part II and Part III explain how do implement this better.

 
Solution to Problem #2: Modify the Default Hover Panel Display Template
The solution here is to modify the hover display template that is being used for default items. This can be easily performed using SharePoint Designer 2013. Follow the steps to get to the Display Templates as I have outlined previously.
 
This time, edit the Item_Default_HoverPanel.html file. Replace the RenderBody section with the following code:

                <!--#_
                    if(ctx.CurrentItem.FileExtension == "jpg" || ctx.CurrentItem.FileExtension == "gif"){
_#-->
                        <div class="ms-srch-hover-imageContainer">
                            <img id="_#= ctx.CurrentItem.csr_id =#_" src="_#= $urlHtmlEncode(ctx.CurrentItem.Path) =#_" onload="this.style.display='block';" />
                        </div>
<!--#_
                }
                else {
_#-->
    <div id="_#= $htmlEncode(id + HP.ids.body) =#_" class="ms-srch-hover-body">
                    _#= ctx.RenderBody(ctx) =#_
                </div>
<!--#_
                }
_#-->
          
 
 

You may add more conditions for each file type you want to handle. You could also check the content type however, by default, the images loaded to Site Assets are documents. So if you want to capture all images I would use the file extension check.

Save those changes and then perform a search. Hover over the image results:


Voila!!! The hover shows a preview of the image!!!

But that's not all folks. If you have Picture Libraries you could have even better results and previews but then that change will negate the solution provided here. I discuss this functionality and customization in my Part II post.




 

20 comments:

  1. as you told in solution 1, i am not able to display image in search results.... can you please tel where i might have gone wrong?

    ReplyDelete
    Replies
    1. Did you add all of the file types? I only used jpg and gif as examples. Did you run a full crawl?

      Delete
  2. Hello Steve, thanks for this but i'm lost. I'm trying to edit the Item_Default_HoverPanel.js as i only see .js files but cannot work out where exactly this code needs to be placed. as per the last section of this post "to paste over after the render header in Item_Default_HoverPanel.js" can you provide a screen shot or tell me exactly where/after it needs to be pasted? Thanks again.

    ReplyDelete
    Replies
    1. My source files are shared here: https://skydrive.live.com/?cid=444d49ee99715093&id=444D49EE99715093%21118&authkey=!AE4ZaIhIsFW_QOU

      Delete
  3. IISReset on the central admin box and restart search service (optionally reboot the box). Index Reset, then do a full crawl.
    If you are geting DispForm.aspx?

    ReplyDelete
    Replies
    1. See my solution to Problem #1 here: http://stevemannspath.blogspot.com/2013/02/sharepoint-2013-image-preview-in-search_22.html

      The Image result type uses the Default Item template and not the Picture template. I am updating this post to explain.

      Delete
    2. If you added the file types and did a full crawl, it should work. You shouldn't have to do all what you were saying. Worse case is reset crawled content and do full crawl. Results get cached so closing your browsers and starting fresh searches also helps.

      Delete
  4. Hi this doesn't work at all. The only way I can get image previews in search results is to store the images in a Picture library (no other list types work). How did you get this working in Site Assets library?

    Neil

    ReplyDelete
    Replies
    1. Picture Libraries are the only types that store a PictureThumbnail and thus that is why the Image display template displays a preview for images in Picture Libraries but not others. I would look at the steps in my Part II post which may assist you further: http://stevemannspath.blogspot.com/2013/02/sharepoint-2013-image-preview-in-search_22.html

      Delete
  5. Hello Steve,

    I have added JPG and PNG to File Type, and tehn reindexed the document library and re-ran the Incremental and Full crawl but still images are rendered as items and not image. Re-indexing the search and running full crawl also didn't helped. Any suggestions?

    ReplyDelete
    Replies
    1. You need to change the display template to Picture in the Result Type. My Part II post shows this. The link to part 2 is in this post.

      Delete
  6. Even IIS reset and search application restart not working.

    ReplyDelete
  7. FYI

    Added the file types gif, png, jpg and jpeg to the search configuration. I made sure the images are checked-in and published. Configured the images with an Image content-type. Tried re-index document library and even did a full crawl.... No cigar, the "Path" search property keeps pointing to the dispform.aspx when images are stored in a document library.

    However, when I use an Image content-type (or one with Image content-type as parent) the search property "PictureUrl" does get filled out correctly. So I'm thinking it's not the type of library that triggers this behavior but content type has also something to do with it.

    ReplyDelete
  8. any luck on the URL .I am stuck here and i have jpg in file type but still pointing to dispform.aspx.

    ReplyDelete
    Replies
    1. Have you done a full crawl? Did you go into the library settings and select Reindex Library?

      Delete
  9. I am into sp 2010 .....and incremental crawl happens every 15 minutes(full crawl happened some 10 days back) .Note: the file type 'jpg' was already there since we set up the farm(2 years back).

    Thanks
    Jag

    ReplyDelete
    Replies
    1. My solutions are for SharePoint 2013.

      Delete
  10. Steve, Do you know all the file types that are supported by document preview feature? (specifically msg files)

    ReplyDelete
  11. If you look inside the displaytemplates folder you will see the types of items supported. I don't think there is a preview for msg files and the default template will probably be used. I'll look at the Result Types to verify.

    ReplyDelete