Rolling up content is a very common requirement for many SharePoint deployments. Since SharePoint 2007, the Content Query Web Part (CQWP) has been the de facto choice for rolling up content from across a site collection to a single location. This was very useful for displaying news items or calendar items on the home page of a site, or gathering up all pages that share some similar characteristic.
A couple of limitations of the CQWP were that its reach was limited by the boundaries of a site collection, and that to apply custom styles, you had to have a decent understanding of XSLT, meaning it wasn’t the easiest of Web Parts to style.
SharePoint 2013 includes a new Web Part for content rollup called the Content Search Web Part (CSWP). (You might think of the CSWP as the good ol’ CQWP on steroids.)
As you can probably infer from the name, the Content Search Web Part derives its content from the search index of your SharePoint farm. The new CSWP removes the two biggest obstacles that users often faced with the CQWP: Namely, you can now roll up content from across site collections, and styling doesn’t require knowledge of XSLT. Instead, the CSWP relies on Display Templates for serving up the look and feel of the rollup. A Display Template is a combination of HTML and JavaScript that SharePoint uses to present the content.
The Content Search Web Part is even available on a non-publishing team site, something the CQWP can’t boast about. The only downside is that the Display Templates aren’t readily available for editing in a non-publishing site. This article isn’t going to go into the details of configuring the CSWP, but instead share a tip about how you can overcome this obstacle and add a custom Display Template to your non-publishing site.
When you add a CSWP to the page and configure your query, you have the option to configure its display setting by changing the Control and Item settings. The Items list is actually made up of the display templates that are available in the site. If you decide that none of the out-of-the-box Display Templates meet your needs, you can create your own. The one caveat is that in order to edit the Display Template, you need access to a publishing site, as they include the ability to edit the HTML files that help create the Display Template.
You’ll begin by mapping a drive to your master page gallery and opening the Display Templates folder. The best way to build your own is to copy one of the existing templates and modify it to fit your needs. You only need to copy the HTML file; a corresponding JavaScript file will automatically be created. Likewise, you’ll only be editing the HTML file, and the JavaScript file automatically updates every time you save your changes.
In the HTML file, you will make your edits and add any additional JavaScript to make the rollup function that meets your needs. Once that’s finished, you’ll need to publish your HTML file so you can choose your new Display Template from the Items list when you edit the CSWP on your site.
Taking this one step further, you can actually copy the JavaScript file to your non-publishing site (a Team site, for example). Map a drive to that site’s master page gallery, open the Display Template, and copy the .js file into the Display Templates folder.
The next step is to add a CSWP to your site, configuring it with everything except selecting your custom template (as it won’t show up in the Items dropdown). Export your CSWP to your computer and open it up with a text editor. Find the property ItemTemplateID and change that to point to your custom .js file, then import the Web Part back onto your page. The configured Web Part will now have your custom styles applied.
This is a handy trick if you need the functionality that the CSWP provides but also need it to blend in with your site.
Ryan Keller is a member of the SharePoint at Rackspace team.