13/07/2009

Modifying MOSS Search Results with XSLT

I finally got round to modifying the way MOSS returns it's search results.


Not satisfied with a single line of results I wanted to be able to show the results in tabular form with multiple rows to give the user a better experience and to actually inform them which item of data in the results corresponded to which item of metadata.



This is what I wanted to achieve



So how is this done?....XSLT !



1. Modify your core search results webpart

This is covered completely in Tobias's Blog Here




2. Creating your XSLT

So now we know what properties are available we can decide which ones to add to our XSLT.
So lets dissect one -

The first section is the declarative xsl

<xsl:stylesheet version="1.0" exclude-result-prefixes="xsl msxsl ddwrt" xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" xmlns:asp="http://schemas.microsoft.com/ASPNET/20" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:ddwrt2="urn:frontpage:internal">
<xsl:output method="html" indent="no"/>
<xsl:decimal-format NaN=""/>
<xsl:param name="FileName" />
<xsl:param name="dvt_apos">'</xsl:param>


The second section parses the search results - this returns all the default columns
NOTE the lines highlighted in GREEN


<xsl:variable name="dvt_1_automode">0</xsl:variable>
<xsl:template match="/">
<xsl:call-template name="dvt_1"/>
</xsl:template>
<xsl:template name="dvt_1">
<xsl:variable name="dvt_StyleName">RepForm1</xsl:variable>
<xsl:variable name="Rows" select="/All_Results/Result" />
<table border="0" width="100%">
<xsl:call-template name="dvt_1.body">
<xsl:with-param name="Rows" select="$Rows" />
</xsl:call-template>
</table>
</xsl:template>
<xsl:template name="dvt_1.body">
<xsl:param name="Rows" />
<xsl:for-each select="$Rows">
<xsl:call-template name="dvt_1.rowview" />
</xsl:for-each>
</xsl:template>
<xsl:template name="dvt_1.rowview">


The following section actually renders the results to the page.


This section is the code that is parsed to the page in the GREEN table above


The data items from the search are highlighted in PURPLE




<tr>
<td width="60%" colspan="" class="ms-vb">
<table width="100%" border="0">
<tr>
<td width="20">
<img src="_layouts/images/CMSEditSourceDoc.GIF"></img>
</td>
<td width="20%">
<b>Title:</b>
</td>
<td>
<xsl:value-of select="title" />
</td>
</tr>
<tr>
<td width="20">
<img src="_layouts/images/memberother.gif"></img>
</td>
<td width="20%">
<b>Author:</b>
</td>
<td>
<xsl:value-of select="author" />
</td>
</tr>
<tr>
<td width="20">
<img src="_layouts/images/LINKTOPAGE.GIF"></img>
</td>
<td width="20%">
<b>Link:</b>
</td>
<td>
<a href="{url}" target="_blank">
<xsl:value-of select="title" />
</a>
</td>
</tr>
<tr>
<td width="20">
<img src="_layouts/images/bizdatacontentsource.gif"></img>
</td>
<td width="20%">
<b>Description:</b>
</td>
<td width="70%">
<xsl:if test="description != ''">
<xsl:value-of select="description" />
</xsl:if>
<xsl:if test="description = ''">
No Description Added
</xsl:if>

</td>
</tr>
</table>
</td>

<td width="30%" colspan="" class="ms-vb">
<table width="100%" border="0">
<tr>
<td width="20">
<img src="_layouts/images/CHNGCOL.GIF"></img>
</td>
<td width="40%">
<b>Document Owner:</b>
</td>
<td>
<xsl:value-of select="documentowner" />
</td>
</tr>
<tr>
<td width="20">
<img src="_layouts/images/ICODCC.GIF"></img>
</td>
<td width="40%">
<b>Document Status:</b>
</td>
<td>
<xsl:value-of select="documentstatus" />
</td>
</tr>
<tr>
<td width="20">
<img src="_layouts/images/CAT16.GIF"></img>
</td>
<td width="40%">
<b>Document Category:</b>
</td>
<td>
<xsl:value-of select="documentcategory" />
</td>
</tr>
<tr>
<td width="20">
<img src="_layouts/images/VERSIONS.GIF"></img>
</td>
<td width="40%">
<b>Document Version:</b>
</td>
<td>
<xsl:value-of select="documentversion" />
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td width="100%" colspan="2">
<font color="dodgerblue">
<b>Notes:</b> This item was <b>modified</b> on <xsl:value-of select="write" />
This item is <b>ranked</b> at <xsl:value-of select="rank" />/1000
This item <b>size</b> is <xsl:value-of select="size" />B
<br/><br/> <b>Highlight Summary</b> : <xsl:value-of select="hithighlightedsummary" />
</font>
</td>
</tr>
<tr>
<td width="100%" colspan="2">
<hr class="ms-consolehr"></hr>
</td>
</tr>
<xsl:if test="$dvt_1_automode = '1'" ddwrt:cf_ignore="1">
<tr>
<td width="100%" colspan="2" class="ms-vb">
<span ddwrt:amkeyfield="" ddwrt:amkeyvalue="string($XPath)" ddwrt:ammode="view" />
</td>
</tr>

</xsl:if>
</xsl:template>
</xsl:stylesheet>




Although this is a simple table, I hope this provides you a primer into how to modify your search results.


No comments:

Post a Comment