Tuesday, November 30, 2010

Image Content Type Thumbnails in 2010 Search Results

These changes will allow the SharePoint 2010 Search Results to display the thumbnail for Images stored in an Asset Library. While the thumbnails exist in SharePoint, for some reason they are not automatically displayed or easy to get to.

You can make these changes in the Search Service Application under Federated Locations/Display Information, or by modifying the Core Search Results web part in the search results.

Step 1
In the Search Service Application under Metadata Properties set up the following new managed properties, then run a full crawl

For Content Type (for some reason you cannot access the default ContentType managed Property) set up one that uses the same mappings
ContentTypeforSearch Mappings= Basic:5(Text), ows_ContentType(Text)

To get the Image File Information you'll need the URL. Despite this being mapped to something called "Thumbnail On Form" it is not the Thumbnail URL, but instead the main Image URL.
ImageFileURL Mappings= ows_ThumbnailOnForm(Text)

Step 2
After running a full crawl, add these 2 new properties to the "Columns" of the search results.

Step 3
Add the following template near the top of the XSL document, such as under the rows of xsl:param

<!-- Return the text after the last occurrence of a delimiter -->
<xsl:template name="string-find-last-text">
<xsl:param name="text" />
<xsl:param name="delim" />
<xsl:choose>
<xsl:when test="contains($text, $delim)">
<xsl:call-template name="string-find-last-text">
<xsl:with-param name="text" select="substring-after($text,$delim)" />
<xsl:with-param name="delim" select="$delim" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>

Step 4
Finally add a result format that will display the thumbnail image
Just above the following line:
<xsl:when test="string-length(picturethumbnailurl) &gt; 0 and contentclass[. = 'STS_ListItem_PictureLibrary']">

Add this additional xls "when" section for Images from an Asset Library (ListItem_851)

<xsl:when test="contenttypeforsearch[. = 'Image'] and contentclass[. = 'STS_ListItem_851']">

<xsl:variable name="imagefilename">
<xsl:call-template name="string-find-last-text">
<xsl:with-param name="text" select="imagefileurl" />
<xsl:with-param name="delim" select="'/'" />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="imageurlroot">
<xsl:value-of select="substring-before(imagefileurl,'/')" />
</xsl:variable>
<xsl:variable name="siterootname">
<xsl:value-of select="substring-before(url,$imageurlroot)" />
</xsl:variable>
<xsl:variable name="thumbnailfilenameextension">
<xsl:text>.</xsl:text>
<xsl:call-template name="string-find-last-text">
<xsl:with-param name="text" select="$imagefilename" />
<xsl:with-param name="delim" select="'.'" />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="thumbnailpath">
<xsl:value-of select="$siterootname" />
<xsl:value-of select="substring-before(imagefileurl,$imagefilename)" />
<xsl:text>_t/</xsl:text>
<xsl:value-of select="substring-before($imagefilename,$thumbnailfilenameextension)" />
<xsl:text>_</xsl:text>
<xsl:value-of select="substring-after($thumbnailfilenameextension,'.')" />
<xsl:text>.jpg</xsl:text>
</xsl:variable>


<div style=" padding-top: 2px; padding-bottom: 2px;">
<div class="srch-picture1">
<img src="/_layouts/images/imageresult_16x16.png" />
</div>
<div class="srch-picture2">
<img class="srch-picture" src="{$thumbnailpath}" alt="" /><xsl:value-of select="$thumbnailfilenameextension" />
</div>
<span><ul class="srch-picturetext">
<li class="srch-Title2 srch-Title5">
<a href="{$url}" id="{concat('CSR_',$id)}" title="{title}">
<xsl:choose>
<xsl:when test="hithighlightedproperties/HHTitle[. != '']">
<xsl:call-template name="HitHighlighting">
<xsl:with-param name="hh" select="hithighlightedproperties/HHTitle" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise><xsl:value-of select="title"/></xsl:otherwise>
</xsl:choose>
</a>
</li>

<li>
<xsl:if test="string-length(picturewidth) &gt; 0 and string-length(pictureheight) &gt; 0">
<xsl:value-of select="$Size" />
<xsl:value-of select="picturewidth" />
<xsl:value-of select="$Multiply" />
<xsl:value-of select="pictureheight" />

<xsl:if test="string-length(size) &gt; 0">
<xsl:if test="number(size) &gt; 0">
<xsl:text disable-output-escaping="yes">&#8195;</xsl:text>
<xsl:choose>
<xsl:when test="round(size div 1024) &lt; 1"><xsl:value-of select="size" /> Bytes</xsl:when>
<xsl:when test="round(size div (1024 *1024)) &lt; 1"><xsl:value-of select="round(size div 1024)" />KB</xsl:when>
<xsl:otherwise><xsl:value-of select="round(size div (1024 * 1024))"/>MB</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:if>
</xsl:if>

<xsl:if test="string-length(datepicturetaken) &gt; 0">
<xsl:text disable-output-escaping="yes">&#8195;</xsl:text>
<xsl:value-of select="$PictureTaken" />
<xsl:value-of select="datepicturetaken" />
</xsl:if>

<xsl:if test="string-length(author) &gt; 0">
<xsl:text disable-output-escaping="yes">&#8195;</xsl:text>
<xsl:value-of select="$Authors" />
<xsl:value-of select="author" />
</xsl:if>

<xsl:if test="string-length(write) &gt; 0">
<xsl:text disable-output-escaping="yes">&#8195;</xsl:text>
<xsl:value-of select="$Date" />
<xsl:value-of select="write" />
</xsl:if>

</li>

<li>
<span class="srch-URL2" id="{concat($currentId,'_Url')}">
<xsl:choose>
<xsl:when test="hithighlightedproperties/HHUrl[. != '']">
<xsl:call-template name="HitHighlighting">
<xsl:with-param name="hh" select="hithighlightedproperties/HHUrl" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="url"/>
</xsl:otherwise>
</xsl:choose>

</span>
</li></ul>
</span>
</div>
<div class="srch-clear">
<img alt="" src="/_layouts/images/blank.gif" />
</div>
</xsl:when>

12 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hi,

    This looks like exactly what we need for our SharePoint 2010 implementation. We have an Asset library with Pictures in it and we wish to have thumbnails visible in search results.

    I successfully performed step 1 and ran the full crawl, but I am having problems with the rest of the steps.

    Step 2; how do I get to these "columns"?
    Step 3 and 4; where do I find these xsl documents?

    Thank you for your help.

    Regards, Bostjan

    ReplyDelete
    Replies
    1. The Columns and XSL are part of the Search Core Results web part

      Delete
  3. Nevermind, I found it.

    Another question though; is something like this possible for "/_layouts/OSSSearchResults.aspx" as well?

    ReplyDelete
    Replies
    1. Not to my knowledge. Because of this, and other shortcomings, we've abandoned OSSSearchResults and use a custom search box that sends everyone to the main search center, using Path as part of the search for This Site and This List.

      Delete
  4. When I added columns to the Fetched Properties I am getting following exception: property doesn't exist or is used in a manner inconsistent with schema settings. Please help me out in fixing this issue

    ReplyDelete
    Replies
    1. Did you create the ContentTypeforSearch and ImageFileURL managed metadata fields in Step 1, and then run a full crawl? If so, add the columns one at a time and figure out which one isn't working. It might be a typo, or formatting issue with the Columns structure.

      Delete
    2. I have cross checked it, there was no typo. When I checked Use Location Visualization, this error was fixed. I made the required changes in the xsl document. Still in the search result image thumbnails are not displaying. Do we need to do any more changes to get them fixed? Please let me know.

      Delete
    3. Do you get a broken thumbnail icon, or no thumbnail at all. If none at all then that section of the xsl isn't being run. Make sure it is in the right spot, just above the Picture Library "when" line. You might want to research xsl "when" statements for some formatting examples. Otherwise if you are using a custom content type you'll need to adjust this line with the name of your content type, replacing "Image":

      If you get a broken thumbnail icon then the code is running, but the path isn't being built to match your environment, so you may need to rework the logic a bit.

      Delete
    4. Thank you for the response. I was able to get the thumbnails for pictures in asset library. I have verified the same in Basic Search and in enterprise search it was working fine. But in Fast Search i am unable to get the thumbnails. Do we need to do any additional setting in Fast Search core result to get the thumbnails of asset images? Please let me know.

      Delete
    5. Great - I'm glad you were able to get it working. Unfortunately I don't have any experience with Fast Search. If it has xsl that you can modify see if you can find a Picture Library thumbnail section, duplicate that and modify it using some of my code above.

      Delete
  5. Great post thanks, works a treat! :)

    ReplyDelete