Friday, June 28, 2013

Filtering by Start Date and End Date in a Content Search Web Part

The Content Search Web Part is by far the most exciting web part in SharePoint 2013. However, if you try to do anything fancy it may also become the most frustrating web part in SharePoint 2013. Why the frustration? It’s because the web part relies on search. If you haven’t spent time working with search before, this can be a point of aggravation. However, it’s not the end of the world and that’s why blog posts like this are written: to share my problem, the discovery of my resolution, and ultimately make your life easier.

Here’s what I was trying to do. I had a page content type using the out of the box Start Date and End Date columns and my content search web part was to display pages where StartDate <= {Today} EndDate>={Today}.

I built my query like the mathematical order of operations: path, content type, property. Thanks to the handy search result preview pane, I could see that my query was good thus far.

Then I added StartDate<={Today} and that’s when my problems started. My search result preview came back empty.

I jumped into Central Admin and started checking my Search Service App. I went into the schema and took a peek at the managed property StartDate.

If you click on the crawled property ows_Start date you’ll noticed that it’s a date and time property. The other similarly named Start Date property, StartDateOWSDate is just text. If Start Date were mapped to StartDateOWSDate and you tried to use StartDate<={Today} in your content search web part, you’d get a syntax error because you can’t use Today when you’re mapped to a text field.

After playing around with this for a while, I needed a good sanity check. I used the SharePoint 2013 Search Query Tool, which is an excellent tool and I believe is a necessity for anyone doing SharePoint 2013 work, but I wasn’t getting anywhere towards resolving the issue. I then came across this post where you can run Powershell to get the crawled property names and values for a document.

I ran it on my file and had my moment of zen. The crawled property for start date is ows_PublishingStartDate not ows_Start date.

Intrigued, I jumped back into my search schema and searched the crawled properties for ows_PublishingStartDate. I didn’t find it. After some fierce Googling I stumbled across this. To get the ows_PublishingStartDate in my crawled properties I had to activate item scheduling in my library. So I activated the feature, reset the search index in the search service app (Note: Resetting the index is not required. I discourage you from resetting the index in a Production environment, but I was in a development environment so I had nothing to lose), and kicked off a full crawl. After the crawl completed I checked my schema for StartDate and ows_PublishingStartDate was mapped.

The real moment of truth came when I returned to my content search web part. I placed StartDate<={Today} in my query and to my delight my query worked.

I can see displaying content in the content search where StartDate<={Today} EndDate>={Today} being a very common requirement, and there’s no reason anyone should go crazy trying to figure search out.