Display filename instead of document title in SharePoint search

My employer has been using SharePoint for a number of years now, and it’s recently come to light that people haven’t always been putting meaningful information in the Document Title field. It seems that if this field is left blank, it will default to the document filename, but a lot of our documents have incorrect titles. e.g. if they have been based on a template or another document.

Clearly the best solution is to educate users to use the Title field properly for newly-created documents (see Title vs Name), and also to update all existing documents, but it’s a big task and for now we have opted for the”quick fix” of showing the document filename in search results. Here’s how you do it:

  • Ensure that the IsDocument managed┬áproperty is set for use within scopes:
    • Browse to Central Administration
    • Click on your Shared Service Provider
    • Click Search settings
    • Click Metadata property mappings
    • Find the IsDocument property and set Use in scope to True
    • Click OK
  • Edit the page and modify the Search Core web part
  • Under Results Query Options, edit the Selected Columns property to include Filename:
    <Column Name=”Filename”/>
    (it doesn’t matter where as long as it is between <Columns> and </Columns>, and obviously not halfway through another <Column> tag)
  • Find the following line (it should be in the Result template):
    <xsl:variable name=”id” select=”id”/>
  • Underneath it, add the following two lines:
    <xsl:variable name=”filename” select=”filename”/>
    <xsl:variable name=”isdocument” select=”isdocument”/>
  • A few lines down, replace the code starting with <span class=”srch-Title”> and ending in</span> with the following:
    <span class=”srch-Title”>
    <a href=”{$url}” id=”{concat(‘CSR_’,$id)}” title=”{$url}”>
    <xsl:if test=”$isdocument &#61; 1″>
    <xsl:value-of select=”filename”/>
    <xsl:if test=”$isdocument &#61; 0″>
    <xsl:when test=”hithighlightedproperties/HHTitle[. != '']“>
    <xsl:call-template name=”HitHighlighting”>
    <xsl:with-param name=”hh” select=”hithighlightedproperties/HHTitle” />
    <xsl:otherwise><xsl:value-of select=”title”/></xsl:otherwise>

I found this tip on the TechNet forums, although it didn’t work as-is because the author had forgotten to declare the isdocument variable. It has been tested in MOSS 2007 but will possibly also work in SharePoint 2010. If desired, you could easily tweak the XSL above to show the document title as well as the filename.

There’s no easy way of changing the OSSSearchResults.aspx page that appears when you do a “This Site” search, and even if you do, it’s unsupported and any changes will most likely be lost next time you install a MOSS service pack. If you have ISAPI_Rewrite installed, as we do, you can easily redirect site searches to the Search Center using this line:

RewriteRule ^(.*)OSSSearchResults.aspx(.*)$ /SearchCenter/Pages/results.aspx$2 [I,L,RP]

If you enjoyed this post, please share it!

    4 comments on “Display filename instead of document title in SharePoint search

    1. Joe says:

      I need to do exactly this on a SharePoint 2010 site. Do you have a step-by-step as good as this one for 2010?

      BTW this is the best example I’ve seen after surfing for hours! Great job! Thanks.

    2. I have an issue like this, but after changing display xsl in way you propose I got the bug that filename is’not correctly highlighted, ie isn’t highlighted at all. What is the reason? Did you have the same with your code?

    3. devolution says:

      as written, this does not appear to work on Search Server 2010. the code in the default template is a bit different. there’s two different places referring to “srch-Title” properties, and they all have a numeric value with them (like “srch-Title2″). i tried inserting the modified code into the first one i found but it removed all formatting from the results.

    4. Keith says:

      This drove me nuts so I opened a ticket with MS support and we found a reg key [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office Server\14.0\Search\Global\Gathering Manager]

      change the value to 0 then rerun a full crawl.

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>