Apr04

Custom Advanced Search: Building a URL to search multiple metadata properties

A few days ago in my post called "Creating a custom advanced search by building strings with JavaScript" I talked about how you could use the Content Editor Web Part (CEWP) to build strings that would allow you provide a user friendly way to leverage the advanced search capabilities in MOSS.

To take the concept one step further, what if you created multiple HTML fields in your CEWP and wanted to allow users to be able to type in values in each field to refine the searching experience.  For example you could have two boxes like Car and Color.  A user could type in Blue and Ford and get only the values that matched both metadata properties. All you'd need to do is figure out what your search query would need to look like.  To test you'd need to type a query in this format:

Property1:"Value1" Property2:"Value2"

This would produce a URL that looked something like this:

myResults.aspx?k=Property1%3A%22Value1%22%20Property2%3A%22Value2%22

All you'd need to do is just use some JavaScript to build a string in a similar format.  If you really wanted to get fancy, you could build some smarts into your JavaScript function to allow users to enter data into some or all of the fields and have it check if the field is blank to not use that value in the string.

jr

Published: Apr-04-08 | 0 Comments | 80 Links to this post

Apr02

Creating a custom advanced search by building strings with JavaScript

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

Published: Apr-02-08 | 1 Comment | 4210 Links to this post