Geeks With Blogs
Matt Gilbert An attempt at techy stuff

MOSS doesn't offer this out the box and of course many people would like to have it. Here's a quick and dirty javascript change which will offer this functionality. You'll have to forgive any inelegance in my coding, I'm a bit rusty ;)

You need to edit the search.js file and add the following:

A trim function

function trim(s)
{
 var l=0; var r=s.length -1;
 while(l < s.length && s.substr(l,1) == ' ')
 { l++; }
 while(r > l && s.substr(r,1) == ' ')
 { r-=1; }
 return s.substring(l, r+1);
}

 

and then insert the following into the GoSearch function

 

           return false;
        }
        else return;
    }

// start of added block
    var searchWords = trim(k);
    var testURL = '';

    // the target url we need to test comes from different places depending on the source page
    try
    {
        testURL = document.forms[0].elements[DDId].value;
    }
    catch(e)
    {
        testURL = Url;
    }

    var searchURL = testURL;

    // only make changes if this is a people search
    if(searchURL.indexOf('peopleresults.aspx') != -1)
    {
        // if there is a value and it doesn't contain : (which is the case if named properties are already being searched)
        if((searchWords.indexOf(':') == -1) && searchWords != '')
        {
            // lastname only
            if(searchWords.indexOf('*') == 0)
            {
                k = 'lastname:' + trim(searchWords.replace(/\*/g, ''));
            }
            // do we have more than one value (there is a space break)?
            // if so, use the first value for the first name and the rest for the last name
            else
            {
                searchWords = searchWords.replace(/\*/g, '');
                var wordBreak = searchWords.indexOf(' ');
                if(wordBreak != -1)
                {
                    k = 'firstname:' + searchWords.substr(0, wordBreak) + " lastname:" +  trim(searchWords.substr(wordBreak+1));
                }
                else
                {
                    k = 'firstname:' + searchWords;
                }
            }
        }
    }
// end of added block

    var sch = '?k=' + encodeURIComponent(k);

    if(null != HdQId){

 

This should allow the following functionality:

1) To search on a first name, simply type the name or the start of it and run the search.

e.g. matt or ma

2) To search for a last name, put a * in front of your search text

e.g. *gilbert or *gil

3) To search full name enter both first and last names (again these can be partial names)

e.g. matt gilbert, or matt g or even m g

 

This all assumes you have not made any other changes to search or pages which will stop this working. It should work for out-the-box MOSS. You'll have to watch that any updates don't touch the search.js file of course as you might lose the changes.

We've implemented this and added those search tips (1-3 above) onto the peoplesearch page and our customer people directory page.

Posted on Friday, December 11, 2009 9:04 AM SharePoint | Back to top


Comments on this post: MOSS 2007 People Search - Wildcard searching

# re: MOSS 2007 People Search - Wildcard searching
Requesting Gravatar...
Great Article!

Do you know if there's a way to make the "search options" drop down menu perform the search the same way? If you click the "search options" drop down menu next to the search field and put a search value in say, the "First Name" field it adds parenthesis to the beginning and end of the value which seems to break the wildcard searching ability.
Left by Aaron Fischer on Jan 21, 2010 1:00 PM

# re: MOSS 2007 People Search - Wildcard searching
Requesting Gravatar...
Hi Matt Gilbert,

Thanks for yor post.

Can you please share the script to include in the GoSearch function.Where exactly in GoSearch function, I have to include the script.
Left by Srikanth on Apr 29, 2013 7:31 AM

# re: MOSS 2007 People Search - Wildcard searching
Requesting Gravatar...
Hi Srikanth,

In-between

return false;
}
else return;
}


and


var sch = '?k=' + encodeURIComponent(k);

if(null != HdQId){


so above and below where I've put comments for the added code block
Left by Matt on Apr 29, 2013 7:39 AM

Your comment:
 (will show your gravatar)


Copyright © mattjgilbert | Powered by: GeeksWithBlogs.net