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.

9 comments:

  1. This was very helpful thank you!

    ReplyDelete
  2. Hi Mickael,
    Nice article. I am trying to create a new webpart overriding the existing core results webpart. If the result that is returned is of type other than page , i want to show them in a separate panel called documents. this.XmlResponsedoc contains the complete XML resultset but when i try to display them using this.XmlResponsedoc.Selectnode("/All_Results/Result"), i expected complete result set but instead it is getting filtered by the Sharepoint core results webpart property "Number of Results per page" . Any idea what should be done??

    ReplyDelete
  3. Hi Ranjani,
    So you are executing a search, and want to filter out some items, showing them in a separate pane right?

    It would be easier to use two Core Results Web Parts, where you add your filter in the "Append Text To Query" property below "Result Query Options" Something like: NOT(fileextension:docx)

    This also saves you from creating a custom web part.

    ReplyDelete
  4. Quick question:

    >>If you search with only a hash “#”, then you will do an >>empty search and all results are returned.

    Does this still work, i couldn't get it to work on my vanilla instal?

    ReplyDelete
  5. It works with FS4SP and with Office365. You can also use % to get the same results.

    ReplyDelete
  6. Hmm that's so weird, it doesn't work for me...?
    I get this: "We did not find any results for #"

    But if i do this: meta.collection:sp i get full results back.

    Same experience on qrserver as-well

    ReplyDelete
  7. Hmm.. weird indeed. And if you do: string("#")
    on the QR server you get no results?

    ReplyDelete
  8. Hi, I can see my content type name in xml result by using above code.
    But i could not able to see that name on my screen in search core result webpart.
    can you please suggest me somthing.

    ReplyDelete
    Replies
    1. Hi,
      Did you include it in your rendering xslt so that it would display?

      Delete