Using search web parts to display relevant content to users

Since the SharePoint 2007 days we’ve been able to utilise audience targeting to display different content to different groups of users, whilst this works well its often not as granular as clients would like. Fortunately SharePoint 2013’s search now makes life easy, here’s how:

To start simple, lets say we need a web part that displays any policy and procedure documents related to the current user’s department. We can do this without a single line of code by using the content search web part (CSWP). Prior to building the search query we’re going to need to know three items:

  1. How to identify policy and procedure documents – In this case they have a content type named “Policies and Procedures”
  2. How to identify the departments that document has been tagged with – The search schema shows a managed property named owstaxIdDBDepartment that contains this information (Search the search schema for the name of your site column to find yours)
  3. How to identify which department the current user belongs to – Within the edit screen of the department user profile property, the name shows as SPS-Department

Using this information we can then build an appropriate search query:

ContentType=”Policies and Procedures” owstaxIdDBDepartment:{User.SPS-Department}

Troubleshooting tip – Setup a simple query to return all policy and procedure documents, set the display template to diagnostic, then use the property mapping setting to display the managed property your using to query against the documents department to confirm exactly what is being returned.

Lets say we want to get a bit more fancy and create a web part that displays news articles that are relevant to the current users skill set. To ensure users don’t miss news posts, we want the web part to return news posts that contain the users skill within any of their managed metadata tags.

The owstaxidmetadataalltagsinfo managed property makes life very easy, it contains the data from all managed metadata fields. So knowing that all news articles have the content type name of “News Articles” and the user profile property that contains the users skills is named SPS-Skills, the query would be as follows:

ContentType=”News Articles” owstaxidmetadataalltagsinfo:{User.SPS-Skills}

For more info on SharePoint’s Keyword Query Language see –



Consistent text & images within display templates

SharePoint sites that feature different sized images and lines of text within aggregation web parts just don’t look professional. Enforcing ridiculously strict guidelines for content authors is always one option, I wouldn’t fancy suggesting it with a workshop though.

Fortunately Microsoft have given us everything we need to create consistent rollups. as shown below:



Image renditions do pretty much all the work, before starting check you have the prerequisites in place:

  • If your on premise the blob cache needs to be configured as detailed here
  • Ensure your using a site with publishing features enabled
  • From Site Settings -> Image Renditions, add a new image rendition specifying the image dimensions you require then make a note of its ID

The image rendition can then be called by appending ?RenditionID=X to your image within the display template. Note replace X with your image ID.

When using publishing pages, I don’t like to enforce a default image rendition as it removes the option for content authors to edit the image rendition from within the page. Instead I take the approach of enforcing the image rendition within the display template, to do this you’ll need to remove the html formatting that’s returned within the managed property like so:

var newsPicture = $getItemValue(ctx, “RollUpImage”);
var newsPictureString = newsPicture.toString();
var newsPictureBefore= newsPictureString.substr(0, newsPictureString.indexOf(‘?’));
var newsPictureURL = newsPictureBefore + “?RenditionID=5” ;

Whilst your there its also a good idea to set a default rollup image like so:

if (newsPictureString.indexOf(“/”) == -1) {
var newsPictureURL = ‘/sites/newshub/PublishingImages/default.jpg?RenditionID=5’;

Note – Update your RenditionID and site collection url accordingly.

Finally you need to show your content authors how to work with image renditions.


If the display template is going to be used in the same size web part throughout the site collection, you could simply just trim the text whenever there are more than X characters using the Srch.U.getTrimmedString – More details.

More than likely you’ll want to rollup news into various sized web parts, by using CSS to trim the text you can avoid the need for multiple display templates:

If the field is a single line, you can simply apply the ms-noWrap class to your html element.

For multiple lines you’ll need to create your own class as shown below:

display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;

The above styles will limit the output to three lines, modify the line-height, max-height -webkit-line-clamp styles to meet your needs.

The amount of lines displayed can then be adjusted at will, for example I have web parts in a custom page layout that has a horizontal web part zone that has a class of .horizontalWebpartZone, and I want only two lines:

.horizontalWebpartZone .eventRollupDescription{
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;

Use Excel 2010 to report on SharePoint lists

A quick win that’s often neglected is to utilise Excel to create reports on SharePoint, here’s how:

  1. Go to the list you’d like to report on and choose Export to Excel
  2. From the Data menu choose Connections then  Properties
  3. Check Refresh data when opening file
  4. Now create a pivot table/chart as you usually would in Excel
  5. Optionally create a named range of the area you would like to display in SharePoint
  6. Save the Excel spreadsheet to a document library within your site
  7. Add an Excel Web Access Web Part to a page
  8. Set the Workbook and Named Range

Note reports won’t update in real time -To update the report, its as easy as pressing the Open in Excel button then saving the spreadsheet.