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="">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xmp><xsl:copy-of select="*"/></xmp>

Which results in something like this

<title>Your document title</title>
<author>Mikael Svenson</author>
<hithighlightedsummary>This is a summary.</hithighlightedsummary>
<HHTitle>Your document title</HHTitle>
<imageurl imageurldescription="Web Page">/_layouts/images/html16.png</imageurl>
<docvector>[procedure overview, 1][office procedure, 1][links, 0.707107]</docvector>
<escprojname>00 - UNDEFINED</escprojname>
<escdeptname>0000 - UNDEFINED</escdeptname>
<esccategoryname>COMPANY MANAGEMENT SYSTEM</esccategoryname>
<escsubcategoryname>GOVERNING DOCUMENTS</escsubcategoryname>

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.