Skip to content

Expanding / Collapsing Search Refiners in SharePoint 2010 (and FAST Search Server 2010 for SharePoint)

11/17/2011
The addition of search refiners in SharePoint 2010 (and FAST Search Server 2010 for SharePoint) is a welcome one.  It really creates a conversational search experience where users can explore the data that’s out there.

One thing I’ve noticed with clients is they often want more refiners on the page than is really convenient.  Using the OTB refiner configuration there is a point of diminishing returns in adding refiners because most of them will be “below the fold” (meaning you’d have to scroll down to see them).

I recently put together a very simple solution to create expanding / collapsing refiners which really save a lot of real estate and give the page a nice clean look.

The solution is comprised of a few parts:

  1. A couple of changes to the OTB Refiner Panel XSTL
  2. Two CSS styles
  3. A few lines of jQuery

Changes to Refiner XSLT

In order to make hiding/showing the refiner details easier I chose to put everything below the refiner title in a div.  That way I could just refer to the whole div and show/hide it instead of working with a bunch of individual elements.

To do so, edit the refiner panel web part and click the XSLT Editor button.  Copy the XSLT and save a copy as a backup.

IMPORTANT.  Make sure you uncheck the “Use default Configuation” checkbox on the refiner panel.  Otherwise you’ll pull your hair out trying to figure out whey your changes aren’t sticking!

XSLT Editor
 

In the XSLT, do a search for “ms-searchref-categoryname”.  This is the class that SharePoint uses for the title of each refiner.  From here, we’ll make two changes.

First, add an onclick event to the div so it looks like this:

<div class=”ms-searchref-categoryname” onClick=”$(this).next().toggle();”>

This is jQuery for “every time you click me, hide or show the element immediately following me”.

Second, add a new div imediately after the ms-searchref-categoryname div that contains everything until the next title element.  Give it a class (in my case it was “myRefinerDetails”)

So this section of the code should now look like this (Note I’ve collapsed a lot of the code for brevity):

New div and jQuery

New div and jQuery

CSS Styles

The CSS styles serve two purposes.  First they hide the refiner details so they are collapsed when the page loads.  Second, they make the titles look a lot cooler 🙂

<style type=”text/css”>

.ms-searchref-categoryname

{

BACKGROUND-COLOR: #528789;

COLOR: #ffffff;

border-bottom:thin white solid;

CURSOR: pointer;

}

.myRefinerDetails

{

DISPLAY: none

}

</style>

With these two pieces in place, you’re refiners should now look like the following picture.  I have the Authors refiner expanded.  If you click on any refiner title it will switch between expand/collapse.

Styled and Expanded Refiners

Styled and Expanded Refiners

jQuery

The last step is to add a little jQuery that will make sure that when the page re-loads that any refiner that has been selected will be expanded.

Here’s the code:

<script src=”https://ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js”></script&gt;

<script language=”javascript”>

$(document).ready(function(){

$(“.ms-searchref-categoryname”).each(function() {

if($(this).next().children(“.ms-searchref-filters”).children(“.ms-searchref-selected”).index() != 0)

{

$(this).next().show();

}

});

});

</script>

What this does is finds our refiner titles (by the class name ms-searchref-categoryname ) and loops through all of them.  For each one, it looks through the associated refiner values and if it finds one that is selected (with the class ms-searchref-selected) and it is not the default selected item (the default selected refiner is just used to clear the filter) then it expands the details.  Pretty amazing what you can do with a couple of lines of jQuery!

Now after I select a refiner it stays expanded after a postback.

You can stick the code, along with the styles in a content editor web part, in a page layout or on a masterpage depending on your needs.

That’s it!  Pretty simple, right?

I think this is a nice little solution that has a lot of bang for the buck.  I would encourage you to explore it.  Expand on it.  Do more with the styling, maybe adding pivoting arrows on the expand / collapse.  Most of all, have fun!

Hope this proves useful to you.

In an upcoming post, I’ll show you how to use a similar method to create search breadcrumbs.  Stay tuned.

Advertisements

From → FAST, SharePoint 2010

6 Comments
  1. Paul L. permalink

    Hi Paul – thanks for this excellent collection of tips! I am a newbie to jquery and scripting, and frankly, I can’t get the “$(this).next().toggle();” to work – I am adding it to the XSLT as you advise, but I get ‘Error on Page’ with the little warning triangle in the lower LH corner of the page. Is there some other bit that needs adding? (I tried putting it into searchresults.aspx, in the same place, with the
    same result.)

    Any pointers appreciated!

    PCL

    • Hi Paul,

      What is the error (you should be able to click on the warning triangle thing on the bottom left for details).

      Couple of thoughts off the top of my head:
      Do you have a reference to jQuery on the page? The reference I used in the code sample requires access to the internet – can your environment see the internet?
      If you copy/pasted from the blog, make sure the quotes are straight quotes (not smart quotes).

      Let me know if that helps!

      Paul

      • Paul L. permalink

        Hi again Chris –

        The error is ObjectExpected (Code 0, Char 1 on some line).

        My code looks like so (all in the XSLT of the OOTB SharePoint Search results page):

        Having previously professed my ignorance on many aspects of Javascript, I tried this ‘Test()’ method with a simple alert(‘testing 1 2 3′) reference, which worked fine.

        Dyin’ to get this working cuz I love the approach you are taking. Thanks for your assistance!

        Paul

      • Paul L. permalink

        and of course, I forgot the code – so sorry!

      • Paul L. permalink

        Well! If at first you don’t succeed, as they say….though my error messages didn’t say so, I got to wondering if there weren’t a browser issue involved. Sure enough, trying this from a browser outside my VM (where IE Enhanced Security had been on, though now disabled) worked.

        Many thanks again for these excellent techniques.

        Paul

  2. So cool! Glad you got it working. BTW, if you’re in the area, I’ll be speaking about this and other solutions at SharePoint Saturday The Conference in Falls Church, next month.
    http://www.spstc.org/Pages/default.aspx

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: