Friday, May 10, 2013

SharePoint 2013 Search: Creating an Image Horizontal Result Block

SharePoint 2013 contains templates to display both videos and people in a horizontal fashion presenting a nice presentation in the Everything search results. For an example of the Video horizontal display see this post.

I thought it would be nice to also have this functionality for images as well as attempt to display the hover panel on the items (since the people and video horizontal displays do not incorporate the hover panel). Also, one of my blog readers happened to query about an image horizontal display - so it was time to implement!!

Launch SharePoint Designer 2013 and navigate to the search center display templates (similiar to the steps here).

Locate the Item_Picture.html, right-click and select Copy:


 
Right-click again and select Paste:

 
Rename the copied file to Item_Picture_CompactHorizontal:


Right-click again and select Edit File in Advanced Mode:


Rename the title and the main div id:

 
Add the following code as shown in the image below:
 
<!--#_ 
                if (!Srch.U.n(ctx.CurrentItem.ParentTableReference) && ctx.CurrentItem.ParentTableReference.TotalRows > 1) {
_#-->
 

<!--#_ 
                } else {
_#-->

 



Scroll to the bottom and add an additional closing bracket:


Between the if and the else that you pasted first, enter the following code as shown in the image below:

            <div id="_#= $htmlEncode(itemId) =#_" name="Item" class="ms-srch-people-intentItem" onmouseover="_#= ctx.currentItem_ShowHoverPanelCallback =#_" onmouseout="_#= ctx.currentItem_HideHoverPanelCallback =#_">
    <div id="ImageInfo">
<!--#_ 
       var pathEncoded = $urlHtmlEncode(ctx.CurrentItem.Path);
                            var encodedName = $htmlEncode(ctx.CurrentItem.Title);
_#-->

                    <ul id="ImageCard">
                       <li id="ImagePic">
        <a clicktype="Result" href="_#= pathEncoded =#_" title="_#= encodedName =#_">
                               <img id="PicPreview" src="_#= pathEncoded =#_" height="80px" width="80px"/>
                            </a>
      </li>
       <li id="ImageTitle">
        <div id="imageTitle" class="ms-textSmall ms-srch-ellipsis" title="_#= encodedName =#_"> _#= encodedName =#_ </div>
       </li>
     </ul>
                  <div id="_#= $htmlEncode(hoverId) =#_" class="ms-srch-hover-outerContainer"></div>
    </div>
            </div>




Save the the changes and then navigate to your Search Center.

From your Search Center site settings, select Search Query Rules from the Site Collection Administration section:


Select the Local SharePoint Results (System):


After the Query Rules load on the page, scroll down and find the Image entry. From the drop-down menu select Copy:


Change the Rule name:

Scroll down to the Actions sections and click on edit to edit the result block:
 
 

Change the Query. I was not getting expected results from the InternalFileType property. Therefore I changed my query filter to "(ContentType:PictureItem OR  ContentType:Image)".

I also changed the amount of items to 6:

In the Settings select This block is always shown above core results and also change the Item Display Template to the new Picture Horizontal template:
 
Click OK. Click Save on the Edit Query Rule page.
 
 
Navigate to your search page and peform a search for images:


The images display horizontally and the hover works as well:


 
 
Search On!!!
 




 

15 comments:

  1. THANKS BUT
    if (!Srch.U.n(ctx.CurrentItem.ParentTableReference) && ctx.CurrentItem.ParentTableReference.TotalRows > 1)
    ITS SHOWS UNDERFUND

    ReplyDelete
    Replies
    1. Did you copy the code tags too?



      I have this working and did it live from scratch at my session this past Saturday.

      Delete
    2. Hi Steve

      i have copied the code what u said but facing the problem here ctx.CurrentItem.ParentTableReference

      i didn't find property like ParentTableReference in ctx.CurrentItem in my debugging what is the mistake with my code please can you... thanks in Advance

      Delete
    3. I would right-click the file in SharePoint Designer and select Reset to Site Definition. Then proceed with the modifications. Maybe something didn't copy over correctly.

      Delete
  2. Steve the same thing how we can do for people display template? Any idea?

    ReplyDelete
    Replies
    1. There is one already there. It shows up in the list as Person Intent.

      Delete
  3. Is it possible to have the results show up on multiple rows? For example, after the user clicks on "Show More" I would like to have all the results displayed horizontally but max each row to 5 items per row.

    ReplyDelete
    Replies
    1. You would need to create an image results page and then somehow create a different image template to display the results in rows of 5. I'll try to create something when I get a chance.

      Delete
    2. Hi Steve,

      Were you ever able to create a display template which displayed the results in several rows?

      Delete
    3. I have not done this using several rows. However I would output all items in a div that is responsive such that they wrap into more rows depending on screen size.

      Delete
  4. Hi Steve, Thanks again for this great article.
    I followed this article and I have a little issue and I hope you can help me with that. Results are coming fine but they are not displaying images if there is more than 1 instead showing X. When I mouse hover the results then I can see the images. I'm not sure what I did wrong here.

    ReplyDelete
    Replies
    1. It is something with the code you put in between the if and else. Take a look at my source code on this blog on the Reference Links and Source Code page (link at top of site)

      Delete
    2. Hi Kash

      I found the same issue.

      I changed this line of code in the image tag

      id="PicPreview" src="_#= pathEncoded =#_" height="80px" width="80px"

      to this...

      id="PicPreview" src="_#= $urlHtmlEncode(ctx.CurrentItem.PictureURL) =#_" height="100%" width="100%"


      And it works.

      Delete
  5. Hey Steve! Thank you for the great article. I strictly followed your instructions but the display template somehow doesn't work. I even published a major version of the display template in the Master Page Gallery and approved all changes - still no success. Instead of using the new display template the previously assigned one is displayed on my search results page. Do you have any idea what might have gone wrong?
    Thank you! Katrin

    ReplyDelete
    Replies
    1. I have seen issues by going through the gallery. I would update in SharePoint Designer and make sure the resultant .js file is updated.

      Delete

Matched Content