Staff Directory Search in SharePoint 2010

March 31, 2011 — 53 Comments

[Update May 2012] Please note that the code snippet has been amended to enable it to work across most browsers, including Firefox.  Many thanks to Benjamin in the comments section for his help.

The short version

Old section of code:

var firstname = escapestr(document.all["firstname"].value);
var lastname = escapestr(document.all["lastname"].value);

Is now replaced with:

var first = document.getElementById('firstname');
var last = document.getElementById('lastname');
var firstname = escapestr(first.value);
var lastname = escapestr(last.value);

[Update Dec 2011] Please note that this solution does not quite get to a full address book search. When Microsoft says ‘Sort by name’, they actually only sort by first name. Daft I know but go complain to them.  The best approach is to add a note for people – add at least the first letter of each name if possible, e.g. search for First name: Cl, Last name: P. That usually ensures all possible matches are listed on the first page.

—-

This post applies to: SharePoint Server 2010, Standard or Enterprise edition. (Requires User Profile Service). It does not apply to SharePoint Foundation Service or FAST.

SharePoint Server includes people in search results. This is great if you’re looking for expertise in a given subject. Results are based on the content of user profiles, which can be a mix of formal information – organisation hierarchy, name, contact details etc. and informal stuff – skills, hobbies, interests etc.

However, what SharePoint does not do straight out of the box is provide address book style staff directory searches. If you are searching specifically for a person, you have to spell their name precisely or phonetically. This post will walk through setting up staff search where you can enter all or part of a name (e.g. show me names beginning ‘Sm’) with results displayed alphabetically.

Preparation

To set-up staff directory search, you first need to have configured a search site using one of the built-in Search site templates available with SharePoint Server 2010 Standard and Enterprise edition. I recommend the Enterprise Search Center site template, it’s available in SharePoint Server Standard and Enterprise edtions (yes, Microsoft has habit of mixing up what the word Enterprise applies to).

For help with setting up the search site, see the following blog post: SharePoint 2010 Search Set-up

Create the Staff Directory search tabs and results page

Instead of using the standard People results page that comes with the Enterprise Search site, we’re going to add an additional tab and results page called Staff Directory. Note: you could just modify the People page, I prefer to use a separate one and leave People for finding expertise.

First we need to create the page we are going to display staff directory search results:

  1. Navigate to the search site
  2. Go to Site Actions – View All Site Content
  3. In All Site Content, under Document Libraries, click on Pages
  4. In the Pages library, click on the Documents tab at the top of the page, click on New Document and select Welcome Page
    Staff Search image 1
  5. To create the page:
    - Enter a title: Staff Directory
    - URL: staffresults
    - Select the page layout: (Welcome Page) People Search results, and click Create
    Staff Search Image 2
  6. The page will be checked out, check it in and publish as a major version (1.0). There’s still some work to do but we’ll be editing it later…

Second, we need to create tabs to make it easy to navigate to the page:

  1. Go back to Site Actions – View All Site Content
  2. In All Site Content, under Lists, click on Tabs in Search Pages
  3. Click Add New Item and enter the Tab Name: Staff Directory and Page: staffresults.aspx
    Staff Search image 3You must use the same page name that you just created. You don’t need to enter the full URL, just the page name, the list points directly at the Pages library.
  4. Repeate steps 1 – 3, but this time select the list Tabs in Search Results

The end result: Your search site now has a dedicated tab and search results page for your staff directory:

Staff Search 4

Create the Staff Directory Search Box

The following can be done on any page on any site but let’s assume you want it on the Intranet home page.

  1. Add a Content Editor web part to the page
  2. Edit the web part to change it’s title to Staff Search
  3. Click inside the web part, then in the menu ribbon, click on the HTML icon and choose Edit HTML Source
    Staff Search Image 5Note: You must do this inside a web part and using the HTML Source box for it to work. Otherwise SharePoint will remove the JavaScript when you save the page
  4. Insert the following code* inside the HTML Source box:  Replace the URLs to point to your search site and page name. You may want to include the full path, e.g. http://server/… Change the table settings if you want a different size or style of layout.
    <script language="javascript">
    //function for enter on keyboard and apostrophes in search strings
    function txtWildPeopleFinder_KeyDown(e)
    {
    if (e.keyCode == 13 || e.keyCode==10)
    {
    e.returnValue=false;
    DoWildPeopleSearch();
    return false;
    }
    else
    return true;
    }
    function escapestr(str)
    {
    return str.replace("'","%22");
    }
    //staff search
    function DoWildPeopleSearch()
    {
    var first = document.getElementById('firstname');
    var last = document.getElementById('lastname');
    var firstname = escapestr(first.value);
    var lastname = escapestr(last.value);
    var url;
    //search on last name only (first name is empty)
    if(firstname == "")
    {
    url = "/search/Pages/staffresults.aspx?k=LastName%3A" + lastname;
    window.location=url;
    return;
    }
    //search on first name only (last name is empty)
    if(lastname == "")
    {
    url = "/search/Pages/staffresults.aspx?k=FirstName%3A" + firstname;
    window.location=url;
    return;
    }
    //search on first and last
    url = "/search/Pages/staffresults.aspx?k=lastname%3A" + lastname +
    "%20FirstName%3A" + firstname;
    window.location=url;
    return;
    }
    </script>
    
    <table width="100%" id="StaffSearchTable" border="0" cellspacing="0"
    cellpadding="4"><tbody>
    <tr><td width="80" nowrap="nowrap">First Name:</td>
    <td width="100%"><input name="firstname" id="firstname"
    onkeydown="txtWildPeopleFinder_KeyDown(event)" type="text" size="25"
    maxlength="55"/></td></tr>
    <tr><td width="80" nowrap="nowrap">Last Name:</td>
    <td><input name="lastname" id="lastname"
    onkeydown="txtWildPeopleFinder_KeyDown(event)"
    type="text" size="25" maxlength="55"/> </td></tr>
    <tr><td></td>
    <td><input onclick="DoWildPeopleSearch()" type="button" value="Search"/></td></tr>
    </tbody></table>
    <p>Enter all or part of a name<br/>e.g. A will find all names beginning with A</p>
  5. Click OK. If you are confident you’ve got the code right, now woud be a good time to export this web part. SharePoint is very fussy about using JavaScript and if you try to edit it at a later date, it may break. Export and then import to the web part gallery to make it easy to just add it back in at a later date. If you’ve made an error, delete the web part and start again from scratch.
  6. Once done, save the page and you should now have a web part that looks something like this:
    Staff Search Image 6
  7. Test the web part to make sure you entered the correct URLs for the search page. Assuming you did, stay on the Staff Directory results page, ready for the next step.

Note. The code creates a layout using a table. If this offends your design sensabilities, by all means insert DIVs and appropriate HTML code to create a properly formatted AAA-compliant box. (p.s. it still won’t be compliant if you’re using a web part zone because the zones use tables in the page layout…)

* I’d love to take full credit for this code but somebody else wrote the original. I’ve had it for years and can’t remember if I got one of the Devs internally (when I was at Microsoft) to help or found it via Google. There’s quite possibly a better way of writing it.

Modifying the Staff Directory results page

The final step is to edit the Staff Directory results page. If you’re not already looking at the page, navigate to it now.

Staff Search Image 7

The image above shows the default People search results page. You can see the search parameters in the search box – I searched for everyone with a last name beginnning G. But look at the results. They are not in alpahebetical order. We’re going to replace the default search box with our staff directory search and change the way the results are displayed.

  1. Edit the staffresults.aspx page
  2. Delete the web part called People Search Box
  3. If you exported/imported your Staff Search web part earlier, now add it to the Top Zone of the page
  4. Edit the web part settings and under Appearance, change the Chrome to None (to hide the title and borders)
  5. Click inside the web part content and click the HTML icon to Edit HTML Source. The code is identical to the before but we are going to use a different table layout. Replace your TABLE section with the following:
    <table id="StaffSearch" border="0" cellspacing="0" cellpadding="3">
    <tbody><tr><td width="80" nowrap="nowrap">First Name:</td>
    <td width="180"><input name="firstname" id="firstname"
    onkeydown="txtWildPeopleFinder_KeyDown(event)" type="text" size="20"
    maxlength="100"/> </td>
    <td width="80" nowrap="nowrap">Last Name:</td>
    <td width="180"><input name="lastname" id="lastname"
    onkeydown="txtWildPeopleFinder_KeyDown(event)" type="text" size="20"
    maxlength="100"/> </td>
    <td><input onclick="DoWildPeopleSearch()" type="button" value="Search"/>
    </td></tr></tbody></table>
  6. Click OK and save the page. Then click Edit to edit it again.
  7. Find the People Search Core Results web part and edit the web part settings
    - Expand the Display Properties section
    - Under Default Results Sorting, change it from ‘Default’ to ‘Name
    - You can also modify other settings, such as number of results per page, size of summary and URL
    - Click Apply/OK to save the changes to the web part settings. Save and publish the page

And there you have it, staff directory search in an address book format with results listed alphabetically by surname.

Staff Search Image 8

There are some other tweaks that I often do, such as adding department to the search box as a dropdown list (requires very accurate data in your AD – i.e. consistent department names – term sets are a useful way of managing) and adding additional properties to the search results such as mobile phone, alternate contact (again, is dependent on the data being available in the profile). But this should get you started.

Final note: It is possible to use the code here and create a staff directory search in SharePoint Server 2007. The complication comes from not having a built-in capability to display results by name. You can set-it up but with a huge limitation – it sorts alphabetically by results page and you can’t have more than 50 items per page. Also, the people results in SharePoint 2007 look terrible. You can make them look more like a business-card style results layout but it takes quite a bit of tweaking using XML. If there’s demand in the comments for it, I’ll dig out my old notes…

This post is part of the SharePoint 2010 Handbook: Enterprise Search

Sharon Richardson

Posts

53 responses to Staff Directory Search in SharePoint 2010

  1. Thanks Sharon – I look forward to your update.
    In the mean time I’ve managed to make it function but there may be a better way to do it.
    The problem I was having was it did not seem to assume or accept a wildcard on the property searches. Without applying this solution a search of FirstName:Kev yielded no results nor did FirstName:Kev*. Only a true equal search worked when specifying a property.

    All other things being the same I had to make two adjustments. One to SharePoint, the other to your code.
    First, in SharePoint I had to adjust the FirstName and LastName fields in Search Service > Search Schema > Crawled Properties and check the “Include in full-text index” checkbox.
    After that I needed to explicitly add the wildcard to the url search string. e.g. the line for the lastname only search became:
    url = “/search/Pages/staffresults.aspx?k=LastName%3A” + lastname + “*”;

    That may not be the most elegant solution but it seems to be doing the trick.

  2. Good job on the Staff search. is it possible to get the data from a term set or pull from a list?

  3. Thanks kojo. And yes! When you create a new property, select the type ‘String (multi value)’ and the settings will refresh to give you the option of specifying a term set. (You can’t pull from a list, but you can use a metadata term set).

Leave a Reply

*

Text formatting is available via select HTML. <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>