Customizing Search Results (SharePoint 2010): People Search Results Custom Sorting using XSLT

Overview

By default People Search Results web part sorting capabilities are limited by the following options:

  • Relevance (default)
  • Social Distance
  • Name

Below is presented XSLT based approach how to provide additional sorting options for People Search Results web part.

But before we proceed let me clarify the main limitation of this solution:

  • the specified method allows to sort  search results returned for page only (to control results returned for page ResultsPerPage property is used)
  • applying sorting take place during XSLT transformation (in our case it  is applied to relevant results)

Solution description

Provide custom sorting options values

ParameterBindings property is used for storing custom sort options values and passing them to XSLT:


<ParameterBindings>
<ParameterBinding Name="CustomNameSortLabels" DefaultValue="Job Title,Department,Last Name" />
<ParameterBinding Name="CustomNameSortValues" DefaultValue="jobtitle,department,lastname" />
<ParameterBinding Name="V1FromUrl" Location="QueryString(v1)" />
<ParameterBinding Name="QueryFromUrl" Location="QueryString(k)" />
<ParameterBinding Name="HsFromUrl" Location="QueryString(hs1)" />
<ParameterBinding Name="RmFromUrl" Location="QueryString(rm1)" />
</ParameterBindings>

,where

CustomNameSortLabels and CustomNameSortValues   are used for storing  custom sort option labels and values respectively.

Customize Sort Options  in People Search Results

In order to display custom sort options the following XSLT template is used:


<xsl:template name="GetCustomSortResultUrl">
<xsl:param name="NameSortValue"/>
<xsl:value-of select="concat(concat(concat('peopleresults.aspx?k=',$QueryFromUrl),'&amp;v1='),$NameSortValue)"/>
</xsl:template>
<xsl:template name="CustomSortOptions">
<xsl:param name="NameSortValues" select="$CustomNameSortValues"/>
<xsl:param name="NameSortLabels" select="$CustomNameSortLabels"/>
<xsl:param name="separator" select="','"/>
<xsl:choose>
<xsl:when test="not(contains($NameSortValues, $separator))">
<xsl:if test="string-length($NameSortValues) &gt; 0">
<xsl:variable name="NameSortUrl">
<xsl:call-template name="GetCustomSortResultUrl">
<xsl:with-param name="NameSortValue" select="$NameSortValues"/>
</xsl:call-template>
</xsl:variable>
<xsl:element name="option">
<xsl:attribute name="value">
<xsl:value-of select="ddwrt:EnsureAllowedProtocol(string($NameSortUrl))"/>
</xsl:attribute>
<xsl:if test="$NameSortValues = $V1FromUrl and string-length($HsFromUrl) = 0">
<xsl:attribute name="selected">selected</xsl:attribute>
</xsl:if>
<xsl:value-of select="$NameSortLabels"/>
</xsl:element>
</xsl:if>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="NameSortUrl">
<xsl:call-template name="GetCustomSortResultUrl">
<xsl:with-param name="NameSortValue" select="substring-before($NameSortValues, $separator)"/>
</xsl:call-template>
</xsl:variable>
<xsl:element name="option">
<xsl:attribute name="value">
<xsl:value-of select="ddwrt:EnsureAllowedProtocol(string($NameSortUrl))"/>
</xsl:attribute>
<xsl:if test="substring-before($NameSortValues, $separator) = $V1FromUrl">
<xsl:attribute name="selected">selected</xsl:attribute>
</xsl:if>
<xsl:value-of select="substring-before($NameSortLabels, $separator)"/>
</xsl:element>
<xsl:call-template name="CustomSortOptions">
<xsl:with-param name="NameSortValues" select="substring-after($NameSortValues, $separator)"/>
<xsl:with-param name="NameSortLabels" select="substring-after($NameSortLabels, $separator)"/>
<xsl:with-param name="separator" select="','"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

Complete XSLT file for People Search Results  could be found here

Below is shown sorting options control after applying specified XSLT and providing Parameter Binding property value from above example

SortOptions

Sorting for search results is defined using XSLT sorting element:


<xsl:for-each select="All_Results/Result">
<xsl:sort data-type="text" order="ascending" select="*[name() = $V1FromUrl]"/>
<xsl:call-template name="SingleResult"/>
</xsl:for-each>

Complete XSLT file for People Search Results with custom Sort Options could be found here

Results

People Search Result page with results sorted by Last Name is shown below

PeopleSearchResultsCustomSort

References

9 thoughts on “Customizing Search Results (SharePoint 2010): People Search Results Custom Sorting using XSLT

  1. Thank you Vadim. I got this working by including the XSLT code and Parameters in both the web parts (Search Action Links and Core Results for People)… As you have specified in your blog post, only the current page items are getting sorted. One issue is, once sorted in custom property, it is appending a query string to the URL and is not getting cleared. Hence the OOB sorting (Default, Name, Social Distance) are not working.. I can write some script to clear it though…

    • Hi Srikanth,
      regrading sorted results per page only, unfortunately using only XSLT based approach it seems that this is the only way.
      Thank you for pointing on issue with not clearing custom property, may be you will provide me with the fixes
      P.S. In next post will be discussed another way about sorting search results.

  2. Hello many Thanks for your Blog Entry.

    Can i change the Display Value “Last Name” to “Name”?

    Is this possible?

    Thanks for your Answer.

    Michael

  3. Hi,
    I applied your code (thanks!) but got error when selecting a custom sortby column. “Internal server error exception: System.Xml.XmlException: Unexpected end of file has occurred. The following elements are not closed: Columns, root. Line 1, position 162. ”
    Also how to hide “default” and “social distance” options?
    Thanks, and sorry, I am not a xslt person or developer.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.