Force File Name in SharePoint 2010 Search Results
There are a few resources on this for 2007, but I thought I’d add a small update for 2010.
First, start by following the instructions in the links above for the IsDocument property. You can get to it from the search service administration and it’s under Metadata Properties:
Now go to your site and create a new web part page (header, left nav, right body) and call it CustomSearchResults.aspx (or whatever you’d like). On the right hand side, add a ” web part and on the left hand side, add a Refinement Panel web part.
Now you need to edit the results web part.
- Under the Core Results settings panel, expand the Display Properties sub-panel.
- Uncheck Use Location Visualization to enable the Fetched Properties and XSL Editor options.
- Modify the Fetched Properties XML string to include <Column Name=”Filename”/> (strongly recommend copying the string out and editing it in a text editor).
- Click the XSL Editor button to modify the XSL. Again, I strongly recommend copying the XSL out to a text editor.
- Around line 206-207, you should see the following line:
12<xsl:template match="Result"><xsl:variable name="id" select="id"/>
- Modify it to look like so:
1234<xsl:template match="Result"><xsl:variable name="id" select="id"/><xsl:variable name="filename" select="filename"/><xsl:variable name="isdocument" select="isdocument"/>
- Next, around (new) line 324, you should see the following XSL:
1234567891011121314151617181920<xsl:otherwise><a id="{concat($currentId,'_Title')}"><xsl:attribute name="href"><xsl:value-of select="$url"/></xsl:attribute><xsl:attribute name="title"><xsl:value-of select="title"/></xsl:attribute><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></xsl:otherwise>
- Update this to the following:
1234567891011121314151617181920212223242526<xsl:otherwise><a id="{concat($currentId,'_Title')}"><xsl:attribute name="href"><xsl:value-of select="$url"/></xsl:attribute><xsl:attribute name="title"><xsl:value-of select="title"/></xsl:attribute><xsl:if test="$isdocument"><xsl:value-of select="filename"/></xsl:if><xsl:if test="not($isdocument)"><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></xsl:otherwise>
Now hook up your custom search results page by clicking on Site Settings and then Search Settings. In the Site Collection Search Results Page text box, enter the full relative path to your page (including sub-site paths and such). For example: /sites/site_xyz/pages/mycustomsearchresults.aspx.
That should do it. Unfortunately, I haven’t figured out how to turn on hit highlighting for the file name yet.