Customising SharePoint People Search with XSLT

One of the clients I have been working with recently wanted to extend the normal people search in SharePoint with the ability to track staff who are First Aiders.  Exactly the same concept can be applied to other roles such as Fire Wardens, JPs and Health & Safety Representatives.

People Search First Aid

User Profile Service

The first thing we need to do is add a field to our User Profile properties.  Go to Central Administration -> Manage Service Applications -> User Profile Service -> Manage User Properties and add a new property.  I called mine FirstAid and made it a Boolean field, then allowed it to be edited by the user and shared with everyone.

User Profile Service First Aid

User Profile Service First Aid

Good start.  Now users will be able to set the checkbox themselves to indicate that they are first aid qualified.

Managed Property

Before we go any further, run a full crawl to make sure the search service knows about our new field.

Then head to Central Administration -> Manage Service Applications -> Search Service -> Metadata Properties and create a New Managed Property.  To keep it consistent I called this FirstAid again, set it to Yes/No (to match the Boolean) and then Add Mapping to the People:FirstAid field we have just created.

First Aid Managed Property

First Aid Managed Property

Make sure you tick the last checkbox to add the managed property to custom results.

Another full crawl should now make sure the property is ready to be used in our search results.

Custom XSLT

Now we need to add our field to the people search results page.  I decided to display this as an icon so I added a green cross image to a library to represent my first aiders.

Navigate to your people search page, by default this will be peopleresults.aspx in your search centre.  Edit the page through the browser and edit the People Search Core Results web part.

There are a couple of things we need to change here.  Firstly, we need to include the field in the results which are returned.  Under the Display Properties section we need to edit the Fetched Properties – you may need to un-tick Use Location Visualisation – then add in <Column Name=”FirstAid” /> – the order is not important.

Finally we need to display our icon for those users who have indicated that they are First Aiders.  You have a few options how to achieve this but I am going to suggest what I consider to be the most flexible.

Click on XSL Editor. . . and you will be presented with an extended text box containing the XSLT which is used to present the default people search.  Press CTRL-A and then CTRL-C to select it all and copy it.  In SharePoint Designer 2010 (or your preferred XSLT editor) create a new XML file and rename to PeopleSearch.xsl.  Edit this file and CTRL-V to paste in the copied XSLT.

I am not going to explain this file in detail – perhaps that is another blog post – but for now search for:

<div id="ContactInfo">

Insert the following just before it:

<xsl:if test="firstaid = 'True'">
<img
 src="/imagelibrary/firstaid.png"
 title="First Aider"
 alt="First Aider"
 style="cursor:pointer"
 onclick="location.search='?k=FirstAid:1'"
>
</xsl:if>

This does two things:

  1. Displays the chosen image if the first aid checkbox is ticked in the user profile
  2. Refreshes the page to show all first aiders if a user clicks on the icon

Save the XSLT file and grab the path to it.  Return to your people results page and under the Miscellaneous section paste the path into the XSL Link.  This will override anything in the XSL Editor and means that in future you can make further modifications without editing the page itself.  It can also be reused on multiple pages if needed.

As a nice addition, you may also like to add the image as a fixed icon on the search page so users can find first aiders simply by clicking on it.

Note that an incremental crawl will need to run before any changes users make to their profiles appear in search results.  I would definitely recommend communicating this to the user population as it will avoid confusion and repeated queries.

This entry was posted in SharePoint and tagged , , , , . Bookmark the permalink.

5 Responses to Customising SharePoint People Search with XSLT

  1. Robin says:

    Note that the field to test on in the if condition, xsl:if test=”firstaid = ‘True’, is in lowercase. That is a must!

  2. Nicola says:

    Hi there, I have followed all of the steps but when I add the code before and then save the page and link to the PeopleSearch.xls file – I get this error: Unable to display this Web Part. To troubleshoot the problem, open this Web page in a Microsoft SharePoint Foundation-compatible HTML editor such as Microsoft SharePoint Designer. If the problem persists, contact your Web server administrator. Any ideas what I have to do to get it working as in your example?

    • paylord says:

      Hello Nicola

      This is almost certainly an issue with the XSLT. Some suggestions:
      1. Check quotation marks – sometimes these do not copy correctly from the web so worth replacing them
      2. Check the HTML is correct – tags opened and closed properly
      3. Confirm you included the additional managed term in the Fetched Properties
      4. If still no joy let me know and I will email through a clean version of the XSLT.

      Hope this gets you sorted.

      Dave

  3. paylord says:

    Updated to present example script better and correct a couple of typos

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s