More on the XSLT behind the CoreResultsWebPart

Posted Tuesday, August 18, 2009 2:27 PM by CoreyRoth

A while back, I wrote a post on how to customize the search results page.  I focused on some of the options with the CoreResultsWebPart (or CRWP as I call it), but today I thought of some more things to discuss in regards to the XSLT document that is used to display the search results.  If you want to know how to add your own managed properties to the search results page, be sure and check out the link above.  What I am going to talk about today is what some of the XSL templates do.  Before you begin, I recommend saving a copy of the XSL you are working on and putting it in source control.  It’s quite easy to mess up what you have and then nothing renders at all.  In the worst case scenario, you can always create a new results page and copy over the XSL from there.

The first template to talk about is at the top of the document and is called dvt_1.noKeyword.  This is the template that is displayed whenever the user submits a blank search query or when fixed keyword querying is turned on and no keywords have been specified.  Here is what this section looks like. 

<xsl:template name="dvt_1.noKeyword">

  <span class="srch-description">

    <xsl:choose>

      <xsl:when test="$IsFixedQuery">

        <xsl:value-of select="$NoFixedQuery" />

      </xsl:when>

      <xsl:otherwise>

        <xsl:value-of select="$NoKeyword" />

      </xsl:otherwise>

    </xsl:choose>

  </span>

</xsl:template>

Since the CoreResultsWebPart supports some form of variation, the text it displays comes through the variables $NoFixedQuery and $NoKeyword.  In English, here is what that text looks like.

Variable Value
$NoFixedQuery Please set the 'Fixed Query' property for the webpart.
$NoKeyword Enter one or more words to search for in the search box.

If you need to customize the text, the quickest way is just to remove the xsl:value-of and put your own text in.  I am sure you can customize the values of those variables, but that falls out of the scope of today’s post.

The next section, I want to mention is dvt_1.empty.  I find that this is one of the most common sections, people want to customize as it is what is displayed when there are no matching search results.  This template also displays alert me and RSS links which you probably don’t need to customize.  The section you will be most interested in is in the span with an id of CSR_NO_RESULTS.

  <span class="srch-description" id="CSR_NO_RESULTS">

    <xsl:value-of select="$NoResults" />

    <ol>

      <li>

        <xsl:value-of select="$NoResults1" />

      </li>

      <li>

        <xsl:value-of select="$NoResults2" />

      </li>

      <li>

        <xsl:value-of select="$NoResults3" />

      </li>

      <li>

        <xsl:value-of select="$NoResults4" />

      </li>

    </ol>

  </span>

</xsl:template>

As you can see the variables $NoResults, $NoResult1, etc… are used here to give more information to user on how to find what he or she is looking for.  This is the section you want to edit if you need to provide different information to the user on how to search.

The dvt_1.body template adds links around the search results, and sets up the basic table layout that the typical search results page uses.  This is where the links are to change the sort order, add an alert, or view RSS. 

The Result template displays each search result.  The post I wrote on customizing search results has more information on editing this section to display additional managed properties.  If you need more of a grid layout for search results, you can change the table formatting here and in the body template to put the results in table cells instead of in a single row.  If you are looking to match the style of other text that is rendered, using a div with a class of srch-description works well.

There are also a few other templates used to display strings, the file size, as well as do the hit highlighting.  I haven’t found a need to customize that yet.  Most of this information is pretty obvious, but I have seen some posts out there on the topic, so I thought this might be a good reference.

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required)