Thursday, November 21, 2013

Tweaking search results xslt to get document titles to open documents directly in browser instead of Office client

In a recent BI-project we needed lots of Excel files to be opened directly in the Excel web app also when found through search. All libraries were configured to open documents in the web browser, but even so search results always lists all document titles to open in Office clients.

In addition to displaying the document title, the author, description, ++, the default search results also display a separate 'View in browser' link for each document. So the logic was to get the functionality of this link to work for the item document titles instead.

This is quite easy to fix. You have to go into the XSLT file that controls how the search output is put together and do three minor changes. This is preferably done through SharePoint Designer.

I really, really advice you to not do these changes directly in the XSLT found in the web part properties of your search core results web part, but rather create your own XSLT file in SharePoint Designer and link this to your web part like described in Matthew McDermott's great blog.

In your copy of the original XSLT - do the following:

Here is the code that replaces the original div for srch-Title3:

<div class="srch-Title3">
<!-- call custom template to show link to doc that opens in browser-->
<span>
<xsl:call-template name="TitleOpensInBrowser">
<xsl:with-param name="browserlink" select="serverredirectedurl" />
<xsl:with-param name="currentId" select="$currentId" />
</xsl:call-template>
<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
</span>
</div>

Here is the custom template code that I've put right below the original ViewInBrowser template code:


<!-- Custom template to fetch doc title into link that opens in browser-->
<xsl:template name="TitleOpensInBrowser">
<xsl:param name="browserlink" />
<xsl:param name="currentId" />
<xsl:if test="string-length($browserlink) &gt; 0">
<span class="srch-urllink BISearchResultsUrlLink">
<a href="{$browserlink}" id="{concat($currentId,'_VBlink')}"><xsl:value-of select="title" /></a>
</span>
</xsl:if>
</xsl:template>

Also there is the param definition right at the top of the file:

<xsl:param name="TitleOpensInBrowser" />

This should do the trick. All items listed in your search results should now open in the web browser rather than in clients.

No comments:

Post a Comment