SharePoint 2010 + Search within document sets

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:

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

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.

3. Edit the HTML source of your Content Editor:

And add the following 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.


  1. Hi Matt,

    I copied and pasted your code into the context web editor and I see all the code on the page and search didn’t work. Please help me

  2. Thanks for the reply
    replace “” with the baseurl. since i did not want to show the url i have put .
    in the search result page the url contains :
    i have followed all steps but doesnt work.
    in ur case url:
    http://yoursite/_layouts/OssSearchResults.aspx?k=%5Bsearchterms%5D site:”http://yoursite/yourlibrary/yourdocset”
    in my case url:
    http://yoursite/_layouts/OssSearchResults.aspx?k=%5Bsearchterms%5D &cs=This%20Site&u=http://yoursite/yourlibrary/yourdocset
    so how should i change queryTerm ?

  3. Hi G. What do you mean it doesn’t work? What does it do (or not do)? Have you set up the search box correctly. I also don’t know you’ve got <> in there. I think you also have to switch off the context scopes for the search box. If you get to the search results page, the contents of the search box should look something like

    your search terms site:”http://yoursite/document lib/document set”


  4. Hi,
    Thanks for the post
    in my case, Search results page url:
    where ABC refers the Document library name
    DEF refers the Document set name
    When I have put
    var queryTerm = “site:”” + baseURL + rootFolder + “””; it does nt work
    Please help on this.

Leave a Reply

Your email address will not be published.