This week, we're privileged to get a lesson in creating custom searches from John Ross, a consultant for SharePoint911 and an active member of the SharePoint community. The search capabilities of Microsoft Office SharePoint Server (MOSS) are one of the most powerful pieces of functionality the product has to offer. Search is often one of the primary reasons that companies purchase MOSS. Typically a company will install and configure MOSS and one of the first things they do is run a basic search query to see what happens and the search results normally cause “oohs” and “ahhs,” but eventually someone is going say something like, "Can we make search do...?"
by John Ross
Creating a custom search solution would probably require that a developer write code that leverages the SharePoint object model, right? Not necessarily! Let’s first take a look at how search works and we’ll do this by running a sample query on the word “SharePoint:”
What happened? When we told MOSS to execute the query, it redirected us to the search results page and built a query string that defined the search term as well as the scope. The exact string looks like this:
On the search results page, there is a web part called “Search Core Results” which will look at that query string and show the corresponding results. You’ll notice that the search term shows up after the “k” variable and the scope shows up after the “s” variable in the query string. If you were to send this URL to a colleague it would work for them as well. Of course the results might be slightly different because MOSS search is smart enough to only show you results that you have access to – but I digress. In addition to the search query and the scope, MOSS can also use the value of a specific metadata property to search. For example if you only wanted to search the property “title,” you could type in something like this:
This would result in a query string that looks like this:
1. First, add a Content Editor Web Part to a page.
3. Add the following code to the CEWP and save it.
As noted in the comments, this code assumes that you’d want to use the out-of-the-box search center created when a publishing site collection is created. However, it's possible to create your own search results page by using the same web parts used by the results page--because that’s all the results page really is-- just a bunch of web parts. Put the page in edit mode and take a look for yourself:
Using this technique opens up a ton of possibilities for creating custom searches that are user friendly. Perhaps you have a list where you're capturing employee contact information, and you want to build an employee search. You could simply create multiple fields, and when you press the search button the values can be concatenated into the desired search string. The only difference in this case is that you would need to know what the query string looks like. In this case we are going to search on the property Name and City:
The query string will look something like this:
Notice that we’ve used quotation marks after each property; this is important because a search query that uses multiple properties won't work without the quotation marks. Additionally, you’ll see that MOSS modifies the spaces, colons, and quotation marks by changing them to the HTML equivalent. As long as your query string recreates the same type of syntax that you would enter in the search box, MOSS will handle the URL Encoding for you. All that would be necessary would be to create another HTML input box, make sure it's being passed to your function from the button, and then add the additional value to the string you're building. It would look something like this:
John Ross is a Senior SharePoint Consultant for SharePoint911. John has over seven years of experience implementing solutions for clients ranging from small businesses to Fortune 500 companies as well as government organizations. John has experience working with all project phases from analysis to implementation and has been involved with a wide range of SharePoint solutions that include public facing Internet sites, corporate intranets, and extranets. Additionally, John is an active member of the SharePoint community and is an esteemed speaker on a wide range of topics. John also maintains a blog focused various SharePoint topics. He is also a co-author of The Ted Pattison Group's SPG301: SharePoint Planning and Governance.
The search capabilities of Microsoft Office SharePoint Server (MOSS) are one of the most powerful pieces of functionality the product has to offer. Search is often one of the primary reasons that companies purchase MOSS. Typically a company will install and configure MOSS and one of the first things they do is run a basic search query to see what happens and the search results normally cause “oohs” and “ahhs,” but eventually someone is going say something like, "Can we make search do...?"