Tag Archive for: xml

Display filename instead of document title in SharePoint search

06 Jun
June 6, 2011

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>
    <xsl:if test=”$isdocument &#61; 0″>
    <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>
    </xsl:if>
    </a>
    <br/>
    </span>

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]

© 2014 Chris Barnes