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.




40 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
  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
  12. Hello

    My company works with SharePoint online. Is this solution valid too?. I cannot find the place to include extensión file.

    ReplyDelete
    Replies
    1. You should be able to change the template from Default to Picture Item. Look at the Part II and Part III posts.

      Delete
    2. I confirmed that currently you cannot produce previews for images that are not stored in picture library: http://stevemannspath.blogspot.com/2015/01/sharepoint-online-search-image-preview.html

      Delete
  13. Does this work in Foundation version?

    I am not able to get it working there.

    ReplyDelete
    Replies
    1. I don't work with Foundation but from what I have read it should work. Which part doesn't work for you?

      Delete
    2. Hi Steve,

      Thanks for all the information. I bought your Search Walkthrough book as well. You missed Re-index document library in your book. Any how
      I followed all the steps for Problem #1: Image Search Results Are List Items

      1- Add jpg, gif file types in Search Service Application File Types
      2- Full Crawl
      3- Added images in Site Asset Library
      4- Re-Index Site Asset Document Library
      5- Run Full Crawl again
      6- Reset IIS

      BUT

      Search still returns aspx file not image file.

      Please suggest

      Delete
  14. Hi Guys,

    After all the effort; I restarted the boxed and now it works. The Search is returning Image file as well as ASPX page..

    Thanks Anyway.

    ReplyDelete
  15. Hi there, I am trying to achieve exactly what is described above on a Sharepoint 2013 Office 365 hosted version. I see preview images in Image libraries, but I do not in for example Asset Libraries. My problem is that I don´t seem to have access to the Search Service Application in my Office 365 version. Therefore I have 2 questions: is there another way to do what is described above under "Solution to Problem #1: Add Image File Types to Search" or how do I get access to the Search Service Application? Many thanks in advance for any hint, Erik

    ReplyDelete
    Replies
    1. I confirmed that currently you cannot produce previews for images that are not stored in picture library: http://stevemannspath.blogspot.com/2015/01/sharepoint-online-search-image-preview.html

      Delete
  16. Thank you Steve, that puts and end to my investigation and I will use image libraries then, Erik

    ReplyDelete
  17. hi Steve, thanks for the helpful blog. One query, can we display all image(s) preview in search results page directly (instead of hover action) - something similar to what google image search does..

    regards
    Bala

    ReplyDelete
    Replies
    1. You could create a results template that showed the images.

      Delete
    2. Steve,
      Do you mean from a picture library or are you saying it's possible with a document library to show the images from a results template? I've been trying to accomplish this by messing with the configurations in the results web part but no luck yet.
      Thanks!

      Delete
    3. No. Libraries and Search Results are two different things. I think I have a post on it. Let me find it.

      Delete
    4. See here: https://stevemannspath.blogspot.com/2013/05/sharepoint-2013-search-creating-image.html?m=1

      Delete
  18. The source files are no longer available. can you please re upload them to OneDrive or other mirror?

    ReplyDelete
    Replies
    1. https://1drv.ms/f/s!ApNQcZnuSU1EdUuzbKFBpwnaMkw

      Delete
  19. Hi Steve, we have continuous crawl enabled, do you still recommend starting an incremental crawl on the content source in order for the changes to take effect? Does running a full crawl instead make sense...(Considering that we have a continuous crawl enabled)?

    ReplyDelete
    Replies
    1. if you have continuous running then any content should be reindexed after clicking on the Reindex Document Library button. if you don't reindex then a full crawl will do the trick.

      Delete

Matched Content