Wildcard People Search on MOSS 2007

Wildcard People Search on MOSS 2007

Posted by: Ramon Scott
4/19/2007

Unfortunately out-of-the-box Microsoft Office SharePoint Server 2007 does not play nice with wildcard searches. This is particularly troublesome when searching for people with hard to spell names.

Here's a quick solution for a adding a web part to the home page that allows for searching for people using first, last or a combination of names. Wildcards are supported on either.

So, entering a first name of Rob would return results for Rob, Robert, Roberta, Robbie, etc. Likewise, a last name search for Smit would return results for Smith, Smits, Smitkerson, etc.

To use this, just add a content editor web part to the moss home page and paste the code below into the source view. The form will submit to the default people search results page.

<script language="javascript">
//function to handle enter on keyboard
function txtWildPeopleFinder_KeyDown(e)
{
  if (e.keyCode == 13 || e.keyCode==10)
  {
    e.returnValue=false;
    DoWildPeopleSearch();
    return false;
  }
  else
    return true;
}
//escape apostrophes in search strings
function escapestr(str)
{
return str.replace("'","%22");
}

//search function
function DoWildPeopleSearch()
{
var firstname = escapestr(document.all["firstname"].value);
var lastname =  escapestr(document.all["lastname"].value);
var url;

//search on last name
if(firstname == "")
{
url = "/searchcenter/Pages/peopleresults.aspx?k=LastName%3A" + lastname;
window.location=url;
return;
}
//search on first name
if(lastname == "")
{
url = "/searchcenter/Pages/peopleresults.aspx?k=FirstName%3A" + firstname;
window.location=url;
return;
}

//first and last
url = "/searchcenter/Pages/peopleresults.aspx?k=lastname%3A" + lastname +  "%20FirstName%3A" + firstname;
window.location=url;
return;
}
</script>

<table cellpadding="3" cellspacing="0" border="0" width="100%" ID="Table3">
<tr>
  <td width="80" nowrap>
   First Name:
  </td>
  <td width="100%">
   <input size="20" maxlength="100" id="firstname" name="firstname" type="text" onkeydown="txtWildPeopleFinder_KeyDown(event)">
  </td>
</tr>
<tr>
  <td width="80" nowrap>
   Last Name:
  </td>
  <td>
   <input size="20" maxlength="100" id="lastname" name="lastname" type="text" onkeydown="txtWildPeopleFinder_KeyDown(event)">
  </td>
</tr>
<tr>
  <td> &nbsp; </td>
  <td>
   <input type="button" onclick="DoWildPeopleSearch()" value="Search">
  </td>
</tr>
</table>