Friday, November 26, 2010

XSLT creation revisited for SharePoint 2010 Search and a small search tip

Search tip

If you search with only a hash “#”, then you will do an empty search and all results are returned.
When modifying the xslt for the Core Search Result Webpart it’s nice to know what data is actually included in the xml.
SharePoint 2010 has a section called “How to: View Search Results XML Data” which also existed for 2007. This time around it has included the important (obsolete for HTML5) XMP tag which makes rendering xml a breeze. Best practice is to use the PRE tag, but then you have to html encode your tags for it to render correctly.

By substituting your xslt with the snippet below, you will get xml output instead in for your web part.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xmp><xsl:copy-of select="*"/></xmp>
</xsl:template>
</xsl:stylesheet>

Which results in something like this

<All_Results>
<Result>
<id>1</id>
<workid></workid>
<rank>1006</rank>
<title>Your document title</title>
<author>Mikael Svenson</author>
<size>79872</size>
<path>http://server/path/test.html</path>
<description></description>
<write>11/1/2010</write>
<sitename>Procedures</sitename>
<collapsingstatus></collapsingstatus>
<hithighlightedsummary>This is a summary.</hithighlightedsummary>
<hithighlightedproperties>
<HHTitle>Your document title</HHTitle>
<HHUrl>http://server/path/test.html</HHUrl>
</hithighlightedproperties>
<imageurl imageurldescription="Web Page">/_layouts/images/html16.png</imageurl>
<contentclass></contentclass>
<isdocument>True</isdocument>
<picturethumbnailurl></picturethumbnailurl>
<url>http://server/path/test.html</url>
<serverredirectedurl></serverredirectedurl>
<fileextension>ASPX</fileextension>
<spsiteurl></spsiteurl>
<docvector>[procedure overview, 1][office procedure, 1][links, 0.707107]</docvector>
<fcocount>1</fcocount>
<fcoid>336059505871761914</fcoid>
<pictureheight></pictureheight>
<picturewidth></picturewidth>
<escbaseextension>xls</escbaseextension>
<escprojname>00 - UNDEFINED</escprojname>
<escdeptname>0000 - UNDEFINED</escdeptname>
<escprocnumber></escprocnumber>
<esccategoryname>COMPANY MANAGEMENT SYSTEM</esccategoryname>
<escsubcategoryname>GOVERNING DOCUMENTS</escsubcategoryname>
<eschistdocnum></eschistdocnum>
</Result>
</All_Results>

This is a result using FS4SP which includes the docvector tag which included concepts from the text. The last properties starting with “esc” are custom defined fields which I’ve added to the Display Properties –> Fetched Properties setting on the Core Result Web Part.

With this xml as a reference, it’s easy to create and modify the default xslt. I usually keep two Core Result Web Parts on the page while creating it, one with the rendered output and one with xml for reference. Once I’m done, I’ll remove my debug part from the page and package it all up for deployment.