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.

image

First I add a couple of items to the list.

image

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

image

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.

image

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.

image

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)

image

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

image

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

image

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

image

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

4 comments:

  1. I am kinda new to Sharepoint. When I test the RefinableDate00>{Today} I get sorry something went wrong. I am sharepoint 2013 online and installed the farm with the standard settings. Any ideas

    ReplyDelete
    Replies
    1. Hi,
      Did you map the crawled property to the managed property (and on what level did you do this?) The easiest is to modify the search schema on the SharePoint tenant admin page.

      Also, make sure to re-crawl the content after you have made the mapping. This ensures RefinableDate00 has values.

      See http://techmikael.blogspot.no/2014/02/how-to-trigger-full-re-index-in.html for some information on how to trigger crawl of all content. If it's just one list/library, you can do it via the Advanced Settings page for the list/library.

      Thanks,
      -m

      Delete
  2. This is an excellent article and I learned several things from it. Thanks a ton. I do have a question regarding the idea behind the solution, to show relevant articles.

    Supposed I want just a centralized (singular) Articles library in my "root" site (site collection /) and I want my authors to be able to tag the articles as pertaining to multiple areas? I have a term set that is driving my simple navigation. This term set is exposed via a column in my Article content type. The issue I am running into is filtering the content search web part based on the current site. I know I am missing some vital methodology here but I can't quite figure out how to get from point A to point B. What do you think? I don't really want an Articles library for every web.

    ReplyDelete
    Replies
    1. Sorry, I meant Announcements, not Articles.

      Delete