Wednesday, February 5, 2014

Sample on how to use Content Search to display an announcement list

Figured I’d write a short sample on how you can use content search to display the last three announcements ordered by create time, filtering out the expired ones. I will also add parameters to limit fetching results from the announcement list on the particular web where we place the content search web part. This allows the site to be used as a web template with the logic still in place without hardcoding the path to the list.

The combination of content type and site/site collection is very powerful and especially useful if you create web templates.

Topics covered in this post:
  • Content Search Web Part
  • Query variables
  • Map date column to a managed property
  • Limit result to the current web, by content type and by date
  • Re-crawl of a list

In addition I have added two columns to the list, description (reused KPIDescription) and URL, where URL will point to an image. The setup for this was to create a custom content type inheriting announcement and adding two site columns. Using site columns makes the job easier as they will have auto generated managed properties to be used in search.


First I add a couple of items to the list.


To check that I get data back I use the SharePoint 2013 Query Tool, and see that my fields are populated. By querying on my new content type I can easily filter on my new list – spcontenttype:rockannouncement


Next up is adding a content search part to my home page and configure it to show the results.

First I edit the page, pick my spot and click insert web part from the ribbon. The Content Search web part is available in the Content Rollup category.


I’m going to go with the default settings for now, using the Picture on left, 3 lines on right out of the box layout. This file is located at /_catalogs/masterpage/Display Templates/Content Web Parts/Item_Picture3Lines.html.

Note: Make sure you activate Publishing Infrastructure on the site collection in order for the .html file to show, and for conversion to take place when you upload new html files. If no you have to upload pre-converted .js files.
imageThe picture link resides in my URL property. The automatically generated managed property for this is URLOWSURLH. Next I want to display the Title(Title), Description (KpiDescriptionOWSMTXT), and lat modified time(Write) With this setup you should have something similar to the image on the right.

Now we need to change the query, which you do by clicking the Change query button in the web part property settings pane. This will bring up the query wizard where we will do our filtering.

The first think I do is click on Switch to Advanced Mode.

I already mentioned the query to limit by content type, but as I wrote initially, I want to limit the results to this sites announcement list, not using path to limit it, and I want to limit to announcements not yet expired.

Limiting on the expired posts requires some search schema configuration. The automatically generated managed property ExpiresOWSDate is indeed marked as queryable, but in order to limit on a range (greater than, less than) and not the exact value, you need a property which is sortable as well.

Navigate to Site settings, and click Search Schema, where I like to do my edits on the site collection level, or even at the SSA or tenant level. Find the managed property called RefinableDate00 and map the crawled property ows_Expires to it.


If you have added items to your list, now is a good time to re-index it from the Advanced settings page on the List Settings page.

The following query does exactly what I want, limit on content type, on the current Web and on announcements which has not yet expired.

spcontenttype:rockannouncement WebId:{Site.ID} RefinableDate00>{Today}

(See my post on some other limitations as well)


The last part of the query modification is to set sorting on the LastModifiedTime managed property from the Sorting tab.


If you click the Test tab, you can see what the final query will actually look like, with the results on the right side.


Hit, OK, save the web part and the page and we have a custom announcement listing with images.


The most obvious part to change here is how the date is displayed, but that will be for another post.