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),'&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) > 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
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