Wednesday, August 4, 2010

Perform Partial Name search in SharePoint 2007

A lot of questions have been raised whether MOSS 2007 can support wildcards, suffix matchings, grouping of query terms, or logical operators. The truth is most of these deficiencies is not as result of the SharePoint engine but the fact that Out of the Box Web Parts do not expose these functionality on the User Interface.

A typical scenario when performing people search in SharePoint, users would like to search with partial names, like: You type in Bra on the search Box and you expect the search results to return: Bradley, Bramley, brambley, Brandon.
In SharePoint out of the box, there is no Web part that supports this. You basically have to type in full names like: Bramley to get any results.

To work around this issue follow this thought process:
On the people Search Web Part (Out of the box), type in your criteria, ensure you are using partial name not full name.

It will display a Peopleresults page. Note the URL displayed on the browser.

Then click on Search Options. Perform the same search again. Again enter the partial name in first name box, then perform search again.

Note that the search will launch PeopleResult page again. This time it places your search criteria in “ ” This is the source of the problem, see the figure below.

If you remove the “ ”. Meaning you just have FirstName:bra in the box and search again. The engine will return the results we want, it will work just as fine. At this point note the URLs:
If you type in your search criteria in the first name box, SharePoint will launch the following URL: http://servername:9090/SearchCenter/Pages/peopleresults.aspx?k=FirstName%3A%22Bra%22
Do you the same for the last name, the following will be launched
Again it looks the problem is on the front end not the engine.
Solution: create a custom Web Part with 3 controls: firstname, Lastname and Search button.
Now you WebPart Solution should look as follows:

When the user clicks on Search, the Web Part will pass the text to the URLs below
When Search Button is clicked: Pass the inputs to this urls: http://servername:9090/SearchCenter/Pages/peopleresults.aspx?k=FirstName%3A%22inputText%22 for the first name. http://servername:9090/SearchCenter/Pages/peopleresults.aspx?k=LastName%3A%22LastNameInput%22
For the last name.

A typical method for Search will be:

void btnSubmit_Click(object sender, EventArgs e)
string getURL = string.Empty;
if (string.IsNullOrEmpty(lastNametxt.Text.Trim()) && !(string.IsNullOrEmpty(firstNametxt.Text.Trim())))
getURL = string.Format(Properties.Settings.Default.SearchURL+"FirstName%3A{0}", firstNametxt.Text.Trim());
else if (string.IsNullOrEmpty(firstNametxt.Text.Trim()) && !(string.IsNullOrEmpty(lastNametxt.Text.Trim())))
getURL = string.Format(Properties.Settings.Default.SearchURL + "LastName%3A{0}", lastNametxt.Text.Trim());
else if (!((string.IsNullOrEmpty(lastNametxt.Text.Trim())) && (string.IsNullOrEmpty(firstNametxt.Text.Trim()))))
getURL = string.Format(Properties.Settings.Default.SearchURL + "FirstName%3A{0}%20LastName%3A{1}", firstNametxt.Text.Trim(), lastNametxt.Text.Trim());
In your settings file define your searchUrl as : http://servername:9090/SearchCenter/Pages/peopleresults.aspx?k=
Please note you can simply hardcode the URL as: http://servername:9090/SearchCenter/Pages/peopleresults.aspx?k

In the Web part above you can simply type names like: Bra to return all related names such as Bramley, Bram, Brad,bradley, brandon and so on. In fact Bra* and Bra will return the same results.

I hope this saves someone time. I will look at the possible of packaging this solution.