Wildcard Search Web Part for SharePoint 2010

Posted by Corey Roth on Tuesday, September 7, 2010

SharePoint 2010 Enterprise Search has great wildcard search support built in now.  However, it requires the use to add an asterisk to their query every time they want a wildcard search.  This is a great step compared to what we had in MOSS 2007, but now it results in a training issue.  In 2007, many people wrote custom code or relied on the Wildcard Search Web Part that I built.  So I thought why not use the QueryManager object to override the query and add an asterisk to the query for the user.


Since they’ve given us some methods to override now on the CoreResultsWebPart, these kind of changes can be done easily without using reflection.  We start by adding assembly references to Microsoft.Office.Server.Search.  Then we create a new web part inheriting from CoreResultsWebPart and add the following using statements.


using Microsoft.Office.Server.Search.Query;

using Microsoft.Office.Server.Search.WebControls;


We then override the GetXPathNavigator method and get a reference to the QueryManager and override the UserQuery property.  In reality, there are only two lines of code involved.


QueryManager queryManager = SharedQueryManager.GetInstance(this.Page).QueryManager;

queryManager.UserQuery = string.Format("{0}*", queryManager.UserQuery);


You can look at the code in the CodePlex project for specifics.  Instructions to add the solution package are included in the readme.txt file.  You can use PowerShell to add the solution package or use stsadm still if you like.  Once the solution is installed, activate the Wildcard Search Core Results (DotNetMafia.com) site collection feature.




Now, go edit any search center results.aspx page you have and use the add web part button on the bottom zone.




Click on the Search group, and choose the Wildcard Search Core Results (DotNetMafia.com) web part.




Remove the existing CoreResultsWebPart from the zone and drag it into place.  You can then stop editing (or publish) the page.  Try a wildcard query and you should get results like the one below.




Notice how i search for accoun and yet I get matches for Accounting and Account.  Very cool.  I’ll warn you that this is a very preliminary release.  It definitely needs more testing so if you run into issues, please let me know.  This was compiled against the release version of SharePoint 2010.  Give it a try and let me know what you think.


Wildcard Search Web Part for SharePoint 2010


Follow me on twitter.


NOTE: This post is also available at www.DotNetMafia.com.

Tags: .NET 4.0ASP.NETSharePointEnterprise Search


Add your comment



Note: All comments require approval.