SharePoint 2010 + Search within document sets

Mon, Mar 5, 2012 3-minute read

I could play a game of “state the obvious” and say that SharePoint 2010 search is powerful. It’s really an incredible beast - and that’s even without any FAST goodies in there. But something that isn’t obviously supported OOTB but seems there’s a few people out there trying to do - search within folders, or in my case, Document Sets. SharePoint search was already configured and, actually, I could search within document sets, by doing a site wide search and then using the refinement panel (on the left hand side of a search result page in a search center) to narrow it down to my document set. What I wanted to do was perform this search directly from a search box.

As you use the refinement panel, you’ll notice that the URL changes, as you stack up the filters, and as it happens, that’s all that’s required is a properly crafted URL:

<br /> http://yoursite/_layouts/OssSearchResults.aspx?k=[searchterms] <strong>site:"http://yoursite/yourlibrary/yourdocset"</strong>

Note the highlighted part - you can restrict your search results to a specified document set (or folder) by supplying the URL in to the site: refinement option/filter.

With that bit taken care of, the challenge was to get a searchbox that would do this dynamically - i.e., craft the URL properly to apply the filter and direct through to the search results page properly. It’s possible using a standard search box and the trusty old Content Editor.

The standard search box enables you to append anything you like to a query using the “Additional Query Terms” property of the webpart. The trick is to get it to dynamically set the URL of the folder you’re looking at, so it works on every page. This assumes that in the URL of the page, you have a parameter called RootFolder which contains the full path to the folder or document set that you want to search in.

  1. Edit your page, and add a standard search box, and a content editor webpart. Reposition the content editor so it appears after your search box.
  2. Edit the properties of your search webpart so that it looks like this.

  1. Edit the HTML source of your Content Editor:

And add the following script:

</script>

This code reads the URL of the page you’re on, and grabs the contents of the “RootFolder” parameter. It then builds a new string based on it and the target base URL. (Yes, you could probably derive the base URL using Javascript as well…). Finally, it searches all inputs on your page to find your searchbox (where the value is DUMMY), and replaces its value with the new URL. I search via value rather than by Id because I don’t know the Id of my input element. And unfortunately, in SharePoint, they have a habit of changing - this will always work, especially where you may have multiple lists with multiple (copies) of your document set content type.

And that’s about it. Enjoy.

Props: I got the “GetUrlVals” script from Ashley Ford.