Having search previews for pictures that are stored in SharePoint document libraries is normally not achievable since in SharePoint Online we cannot define which are the crawled file types, unlike in SharePoint on-premises. The previews (on the hover panel) should work however if the pictures are in a “Picture Library” or in an “Asset Library”.

To overcome this limitation, it is possible to combine the use of custom display templates and search result types. However, one limitation remains: the items from the document libraries (the picture items) will still be displayed as list items, rather than documents and will be referenced as:

…/Forms/DispForm.aspx?ID=4&RootFolder=/…

Using the method described here you will be able to have search result previews for images that are stored in SharePoint document libraries, both on the hover panel and directly from the search results list (like the previews done for PowerPoint files). It will also work properly if the images are in Assets Libraries or Picture Libraries.

Here are the steps needed to add previews for image files:

  1. Using SharePoint Designer make copies for the files “Item_Picture.html” and “Item_Picture_HoverPanel.html”, optionally rename them (for instance use the suffix _Custom, “Item_Picture_Custom.html” and “Item_Picture_HoverPanel_Custom.html”);
  2. Make the following changes in the file “Item_Picture_Custom.html”:
    1. Update the title (it will be easier to find it in the Result type templates) to “Item Picture Custom”
    2. On the section “<mso:ManagedPropertyMapping…” add the property Filename in the format ‘Filename’:’Filename’
    3. Update the value for the variable hoverUrl to point to the newly created/renamed HoverPanel .js file, for instance:

      [code language=”js”]var hoverUrl = "~sitecollection/_catalogs/masterpage/Display Templates/Search/Item_Picture_HoverPanel_Custom.js"[/code]

    4. Below this line add the following content
      Line:

      [code language=”js”]ctx.currentItem_HideHoverPanelCallback = Srch.U.getHideHoverPanelCallback();[/code]

      Content:

      [code language=”js”]
      var PictureParentPath = ctx.CurrentItem.ParentLink;
      var CheckPath = PictureParentPath.indexOf("/Forms/");
      if(CheckPath == -1) {
      var FullPicturePath = ctx.CurrentItem.ParentLink + ‘/’ + ctx.CurrentItem.Filename;
      } else {
      var FullPicturePath = PictureParentPath.substring(0, CheckPath != 1 ? CheckPath : PictureParentPath.length) + ‘/’ + ctx.CurrentItem.Filename;
      }
      [/code]

    5. Replaces the lines below with the following content:
      Lines:

      [code language=”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;
      }
      [/code]

      Content:

      [code language=”js”]
      if(!Srch.U.n(ctx.CurrentItem.PictureThumbnailURL) && !ctx.CurrentItem.IsContainer) {
      ctx.CurrentItem.csr_PreviewImage = ctx.CurrentItem.PictureThumbnailURL;
      } else {
      ctx.CurrentItem.csr_PreviewImage = FullPicturePath;
      }
      ctx.CurrentItem.csr_PathLength = Srch.U.pathTruncationLengthWithPreview;
      [/code]

      It will look like this:

    6. Check in the file as a major version
  3. Make the following changes on the file “Item_Picture_HoverPanel_Custom.html”:
    1. Update the title (it will be easier to find it in the Result type templates) to “Item Picture HoverPanel Custom”
    2. On the section “<mso:ManagedPropertyMapping…” add the property Filename in the format ‘Filename’:’Filename’
    3. Below this line add the following content:
      Line:

      [code language=”js”]ctx.currentItem_ShowChangedBySnippet = true;[/code]

      Content:

      [code language=”js”]
      var PictureParentPath = ctx.CurrentItem.ParentLink;
      var CheckPath = PictureParentPath.indexOf("/Forms/")
      if(CheckPath == -1) {
      var FullPicturePath = ctx.CurrentItem.ParentLink + ‘/’ + ctx.CurrentItem.Filename;
      } else {
      var FullPicturePath = PictureParentPath.substring(0, CheckPath != 1 ? CheckPath : PictureParentPath.length) + ‘/’ + ctx.CurrentItem.Filename;
      }
      [/code]

    4. Below these lines add the following content:
      Lines:

      [code language=”js”]
      _#= ctx.RenderHeader(ctx) =#_
      </div>

      <div id="_#= $htmlEncode(id + HP.ids.body) =#_" class="ms-srch-hover-body">
      [/code]

      Content:

      [code language=”js”]
      <!–#_ if(Srch.U.n(ctx.CurrentItem.PictureURL)){ ctx.CurrentItem.csr_DataShown = true; _#–>

      <div class="ms-srch-hover-imageContainer">
      <img id="_#= ctx.CurrentItem.csr_id =#_" src="_#= $urlHtmlEncodeString(FullPicturePath) =#_" onload="this.style.display=’block’;" />
      </div>

      <!–#_ } _#–>
      [/code]

      It will look like this:

    5. Check in the file as a major version
  4. In the Enterprise Search Center go to the Search Result Types section (Site Actions – Site Settings – Search – Result Types);
  5. Make a copy of “Image” result type and choose a new name for it, for instance “Image Custom”;
  6. On the section “What types of content should match? You can skip this rule to match all content” choose “Select a value”;
  7. Click on “Show more conditions”;
  8. On the section “Which custom properties should match?” choose from the dropdown “FileType” and input the picture extensions, like jpg, gif, etc.;
  9. On the section “What should these results look like?” choose the name of the newly created display template, in our case it is “Picture Item Custom”;
  10. At the end it should look like this:

Now, when searching for images in the Enterprise Search Center we will get a preview both in the hover panel and in the search results.