Displaying the filename in the SharePoint search results

Displaying the filename in the SharePoint search results

By default the SharePoint search web part ‘Core Search Results Web Part’ displays the document’s title as a hyperlink to the document (or list item). While this is fine for most cases, recently a customer explained to me:

We are crawling all our old file shares using the SharePoint Search. Our end users are used to finding documents by their filename, however the SharePoint search is displaying the document’s title which confuses our end-users

Of course I could argue with him about the purpose of the title and/or filename properties of a document, but I decided to grant his wish. First of all, navigate to the search page which you want to modify, and put that page into edit mode. Locate the ‘Core Search Results Web Part’ modify it, and press the bug button ‘XSL Editor…’

My advise is to use a text-editor like Notepad++ to edit the XSL. Scroll all the way down and just before the end of the style-sheet (marked by ‘<!– End of Stylesheet –>’) insert the following XSL template.

<xsl:template name="substring-after-last">
   <!-- declare that it takes two parameters - the string and the char -->
   <xsl:param name="string" />
   <xsl:param name="char" />
   <xsl:choose>
      <!-- if the string contains the character... -->
      <xsl:when test="contains($string, $char)">
         <!-- call the template recursively... -->
         <xsl:call-template name="substring-after-last">
            <!-- with the string being the string after the character -->
            <xsl:with-param name="string" select="substring-after($string, $char)" />
            <!-- and the character being the same as before -->
            <xsl:with-param name="char" select="$char" />
         </xsl:call-template>
      </xsl:when>
      <!-- otherwise, return the value of the string -->
      <xsl:otherwise><xsl:value-of select="$string" /></xsl:otherwise>
   </xsl:choose>
</xsl:template>

This template is pretty self-explanatory, it returns a substring after the last occurrence of a specified character. I didn’t come up with this XSL myself, but took it from the website of Dave Pawson. Now locate the XSL line where the document’s title is used:

  <span class="srch-Title">
   <a href="{$url}" id="{concat('CSR_',$id)}" title="{$url}">
    <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>
    <br/>
   </span>

And replace it with something like this:

  <span class="srch-Title">
   	  <a href="{$url}" id="{concat('CSR_',$id)}" title="{$url}">
		  <xsl:call-template name="substring-after-last">
				<xsl:with-param name="string" select="$url" />
				<xsl:with-param name="char" select="'/'" />
			</xsl:call-template>
		</a>
  <img src="/_layouts/images/blank.gif" height="1" width="15" />
   <span class="srch-Description">
      (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>
	</span>)
    <br/>
   </span>

This piece of XSL takes the URL of the document and takes the substring after the last occurrence of ‘/’, hence the filename. I have chosen for a hybrid solution where I show both the filename (as a hyperlink) alongside the title, but feel free to experiment.

About the author

Alain

You can leave a response, or trackback from your own site.

Leave a Reply