Add Search for Current Document Set

 

Document Sets were first introduced in SharePoint 2010 and are an excellent way to organize content. I've gained many enthusiastic users of Doc Sets, but there is one feature they often ask for - the ability to search inside a particular Document Set. The issue is that Document Sets can easily end up with tens or hundreds of documents in them, but the default search scope in SharePoint is usually going to search the whole site. With this in mind I devised a solution to allow my users to search just in the Document Set they're currently looking at.

Wouldn't it be nice to allow users to search inside the current Document Set like this? It just takes a few minutes to put this together! (I've documented here for SharePoint 2010, but this also works for 2013 and Office 365 with a minor tweak detailed below)

Search Results

 

One very nice feature of Document Sets is that they have a 'Homepage' that you can edit. This allows you to change the look and feel, including adding extra web parts and through use of the Content Editor Web Part add Script. This thinking led me to my solution...

Search results webparts in SharePoint are driven by querystring parameters in the url of the page. I therefore realized that if I added a Core Search Results webpart to the Document Set homepage, and manipulated the URL with the users search term and the Document Set address then I could show Document Set specific Search results right there in the Document Set page.

The concept is illustrated below. I need to take the url of the Doc Set, and add two querystring parameters; k for the search term and u for the container I wanted to search. (This is Microsoft's naming convention, not mine!)

 

Change of querystring params

 

To achieve the above I first need to change the Doc Set Homepage for my library. Go to library settings and click the Document Set Content Type. (This is assuming you've already added that!)

 

Document Set Content Type

 

In the next page click Document Set Settings

 

Document Set Settings

 

In that next page you'll see an option to edit the Doc Set Welcome Page.

 

Docset Page Edit

 

Now you see the default template for the welcome page

 

Doc Set Page default

 

This is what that same page looks like when you click edit. The various web part zones are visible;

 

Docset Web Part Zones

 

Upload the Javascript file (SearchAndFilter.js) I've attached at the end of the article to your local Assets Library. We're going to reference that file in a Content Editor Web Part (CEWP). Or you could take the text from that javascript file and paste into the HTML view of the CEWP, but that's not good practise.

Now we add a CEWP to the page as shown;

 

Add CEWP

 

In the Web Part settings, set the content link to be the javascript file you just uploaded to the Assets Library;

 

Configure CEWP

 

Next add a Search Core Results web part to the same web part zone;

 

Configure Search Web Part

 

Your page now looks like this;

 

Final Page Version

 

Your users now see a Search box when they're viewing Document Sets. Entering a Search Term and clicking they see results from just that Document Set as below. This is better than before but the results are kind of ugly.

 

Document Set Search Results

 

To change the look of the Search results to be friendlier, edit page again and go into the Search Core Results web part settings. Expand Display Properties, and click the XSL Editor button. (You may need to untick the 'Use Location Visualization' box to ungrey the button). In the editor that pops open, delete the text and paste in the contents of my SearchStyling.xsl file, also at the bottom of this article. Click OK and save your changes.

 

Edit Core Search WP XSL

 

Your search results should now look fairly like a normal List View. Sorting and filtering is possible on the search columns, and the description field which is sometimes verbose can be expanded or not. The XSL I've provided can be tweaked to display different columns (there's a lot of properties in search results that aren't shown by default) including your own custom columns.

 

Search Results

 

The files you need to make this work are below;

 

SearchAndFilter.js (3.60 kb) [SP2010]

SearchStyling.xsl (10.91 kb) [SP2010]

SearchAndFilterO365.js (3.60 kb) [SP2013 or Office 365]

 

If you need this for Office 365 or SharePoint 2013 instead, there's a one line tweak needed to the javascript. The Search Results webpart in that version puts it's 'search results not found' text in a different bit of the page. (Also you'll style your Search Results with Display Templates instead, which is off topic here.)

 

Happy SharePointing!

Disclaimer: The software, source code and guidance on this website is provided "AS IS"
with no warranties of any kind. The entire risk arising out of the use or
performance of the software and source code is with you.

Any views expressed in this blog are those of the individual and may not necessarily reflect the views of any organization the individual may be affiliated with.