NewEgg has it. Best Buy has it. Now, you can have it too. I’m talking about the Breadcrumb Refiner that you see chained across their search pages. Out-of-the-box SharePoint 2010 doesn’t provide this functionality, so we’re going to have to build it. This is a semi-involved process and requires the modification of the RefinementWebPart just to get started. I’m not going to walk you through complete implementation of the web part, because that would kill a significant amount of PointBridge IP, but I will cover the steps that I’ve taken to complete this really cool feature. The easiest way to do this is to use the URL, which contains the query, refinement, and your order parameters.
Your search breadcrumb should, at the very least, provide the user a way to back track out of a search. You could also group refiners of the same type together or allow removal of a single refiner regardless of where it is in the order.
Why SharePoint Doesn’t Include a BreadcrumbSearchWebPart Out of the Box
SharePoint Search is a fluid process that requires little in the way of query and refinement structure. As long as everything is in the query/refinement string, your search should work. A breadcrumb refiner relies on more structure than SharePoint provides because, by its very nature, a breadcrumb knows where you were before. SharePoint Search is not concerned with previous results, so it’s not unlikely that refiners would be merged together or reordered to optimize query performance. That’s great for the backend query server, but a nightmare for the user. If you have a really deep search, you can’t really back out of that easily without hitting the back button, and it’s not readily apparent what you’re refining on. So a breadcrumb can be an important tool in enabling the user to quickly get to what they’re looking for. Since SharePoint Search isn’t structured to maintain the order of how a user searched, you need to provide that for them.
Modifying the RefinementWebPart
The RefinementWebPart is the control that appears on the left hand navigation bar of the Search Center. It allows the user to add criteria to his or her search. It’s unsealed and can be easily extended. The important method that you’ll need to override is GetXPathNavigator, which is the method responsible for creating the XML that is output to configurable XSLT to generate the HTML for the control. You’ll need to maintain the order that a user clicks on something by modifying the URL that it outputs. This is relatively simple, because the base GetXPathNavigator method identifies which refiners are selected, so adding and removing elements it simple string manipulation. You remove an item from the search order when the user removes that refiner and you add it when the user adds that refiner.
After you’re finished manipulating each URL, you pass out the XPathNavigator and execution proceeds as normal. Your added order parameter doesn’t harm the search in any way, and if you stopped here, you’ve gained valuable insight into how the RefinementWebPart works.
Adding a BreadcrumbSearchWebPart
Now you can create a new (non-Visual) Web Part. If you’re using FAST for searching, the refinement values are Base64 encoded, so you’ll need a class to deserialize them for you. You can use the internal Microsoft classes for this by finding the Refinement class in Microsoft.Office.Server.Search.dll. They’re internal for some reason, so you’ll need to crack the DLL in .NET Reflector to copy the source code, or create your own deserialization class.
Now that you’ve got your new web part and classes that allow you to read the refiners in the URL, you’re set to move on. To follow the path that’s been set by Microsoft of outputting XML that is transformed by XSLT, you’ll want a method that outputs an XPathDocument to be transformed by user-provided XSLT. The general steps to follow are:
- Read in the refiners and order parameters from the URL and parse them into KeyValuePairs of strings into Refinement objects and strings into strings for the refiners and orders respectively.
- Match each order to its corresponding refiner to build a URL for that order element and each preceding order element
- Optionally group refiners or create a removal URL to remove that specific refiner
- Output valid XML that can be transformed into the HTML that you want. XML is agnostic in terms of how it gets displayed, so you have a lot of opportunities to tweak the display.
Apply an XSL Transformation (XSLT)
Once you’re getting valid XML back from the web part, you can apply XSL transforms to get the HTML that you want. This is beyond the scope of this blog, but simple XSLT is not difficult. A primer can be found here: [w3schools.com]
A Search Breadcrumb is not only a cool piece of functionality, it can greatly enhance the usability of a search center. By adding some structure to the query and refiners, you can create the breadcrumb. Just note that the breadcrumb refiner cannot function without the order being appended onto the URL from a RefinementWebPart. The search center functions as a loosely coupled unit, with some web parts relying on the presence of others. The breadcrumb refiner is no different.