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:

function getSearchBox(v) {
       var inputs = document.getElementsByTagName('input');
       for (var i = 0; i < inputs.length; i ++) {
                if (inputs[i].value == v) {
                        return inputs[i];
                }
        }
        return false;
}
 
function getUrlVars() { 
 
    var vars = {}; 
 
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) { 
 
        vars[key] = value; 
 
    }); 
 
    return vars; 
}
 
 
var baseURL = "yoururl";
var rootFolder = getUrlVars()["RootFolder"];
 
var queryTerm = "site:\"" + baseURL + rootFolder + "\"";
getSearchBox("DUMMY").value = queryTerm;
</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.



Tags: , , , , ,

5 Responses to “SharePoint 2010 + Search within document sets”

  1. G says:

    Hi,
    Thanks for the post
    in my case, Search results page url:
    <>/_layouts/OSSSearchResults.aspx?k=<>&cs=This%20Site&u=<>/ABC%2FDEF
    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.
    Thx

  2. 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”

    Matt

  3. Also remember that in order for this to work, there must be a RootFolder parameter in the URL of the document set where you have your search box.

  4. G says:

    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 :
    /_layouts/OSSSearchResults.aspx?k=&cs=This%20Site&u=/ABC%2FDEF
    i have followed all steps but doesnt work.
    in ur case url:
    http://yoursite/_layouts/OssSearchResults.aspx?k=searchterms site:”http://yoursite/yourlibrary/yourdocset”
    in my case url:
    http://yoursite/_layouts/OssSearchResults.aspx?k=searchterms &cs=This%20Site&u=http://yoursite/yourlibrary/yourdocset
    so how should i change queryTerm ?

  5. Hi. In the configuration of the searchbox webpart, please try turning off any contextual scopes I.e., in the “scopes dropdown” section.

Leave a Reply