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>