Creating a custom advanced search by building strings with JavaScript 

Tags:

A little more than a month ago a client called and wanted to customize the MOSS search to allow users to type in a City and have the search only query values in a specific field.  It sounded easy enough, but I told the client I wanted to do some research first which led me to some blog posts like this and this.  I tried it out and it worked – well I thought it worked.  When I tested this method with a real set of data paging no longer worked.  What I mean is that if I performed a search against a scope that had 100 items, if my initial query returned 15 results.  As soon as I clicked on page 2, SharePoint basically forgot what I was searching for.  I had already told the client I had an answer for him, so I was in a little bit of a pickle.

I posted comments to the blogs and even asked a question or two in the MSDN SharePoint Forums – the answer was always “to solve the problem you’ll have to write a custom search box web part.”  This really wasn’t the answer I was looking so I decided to come up with a better way.   It wasn’t until I was in my car on a 2.5 hour drive that the answer hit me. 

A while back, Shane Young (aka The Search Pimp) had shown me a little trick on how to search on a specific metadata property.  All you have to do is  enter a query like this into your Search Box:

Title:<whatever>

That search query will then refresh the page and the URL will now show something like this:

http://webappname/searchcenter/Pages/Results.aspx?k=title%3Atest&s=All%20Sites

That URL has all the information that MOSS needs to return search results and there’s no issue with paging.  To solve my problem, all I had to do was build a string.  Those original blog posts gave me another idea – I decided to use the Content Editor Web Part (CEWP) to build a field and a button attached to a Javascript function.  The idea was that when I clicked the button it would just build my string and then redirect the page. 

If you are trying this at home here’s what you’ll need to do:

1)      Create a web part page

2)      Add the following web parts:

Content Editor Web Part

Search Statistics

Search Paging

Search Core Results

 

3)      Add the following code to your Content Editor Web Part:

Title:<input name=input1 />

<INPUT id="Submit1" onclick='Redirect(form.input1.value)' type="button" name="schButton" value="Search" />

<SCRIPT LANGUAGE="javascript">

function Redirect(input)

{   

//Location where you want to display the

//search results.  This example assumes

//you’d want to have a Core Search Results

//webpart on the same page.  But you could

//redirect to any page with a Core Search

//Results webpart

 var baseURL  = "mySearchResults.aspx?k=Title%3A"+input

    top.location.href = baseURL;

   

    return true;

}

 

</SCRIPT>

 

Using this technique opens up a ton of possibilities.  The main use I’ve seen is to allow users an easy way to search on specific information without having to show them how to use the Advanced Search.  Scopes can also be passed in as a query which you can see in the first URL I showed above.  Since doing this the first time I’ve probably had 5 requests from different clients to do some variation of this.

 

For my client, I customized the search results using this post from Tobias Zimmergen but the options for tweaking this idea are pretty wide open.  I’ve got some other ideas about how to further enhance this concept but I’m going to save it for another post. 

 

As you can see, the answer didn’t take custom coding (the JS was borrowed from one of the many examples out there) – all it took was just a little creativity with using what’s Out Of The Box.

 

JR

 
Posted by John Ross on 2-Apr-08
2 Comments  |  Trackback Url  |  Link to this post | Bookmark this post with:        
 

Links to this post

Comments


Thank you! commented on Friday, 4-Jul-2008
A simple, clean and powerful solution to many of my search-related problems. Thanks!


David commented on Wednesday, 8-Oct-2008
This doesn't solve the fact tht you are still using the leyword search syntax and therefore cannot do wildcard searches which is usually why most of my clients want a custom search interface because they want wild card searching which can be done with the two articles you linked to. Nothing is perfect but :)

Leave a comment





CAPTCHA Image Validation