Thursday, August 7, 2008

CQWP and custom column troubles

Last week I've been struggling with one of the most deceptive bugs I've ever encountered. I had created a custom site column, a date-time field. Multiple contenttypes (all pages) used this site column. Then I used a content by query webpart to show a list of pages on the site, filtered by my custom date field. The first time I tried this I got no results. I thought something had gone wrong in the declaration of my columns and types, so I made some minor modifications and tried again. It worked, I got results and I proudly published my work.

One day later, a co-worker complained that the results of the CQWP were incorrect. I checked, and he was right. Some results should not have been shown but were, and some that should have been shown were missing. More than a day I spent on this until I found this great posting by Ranjan Banerji (the fact that the link reads "CQWP nightmares" should be a hint):
http://techblog.ranjanbanerji.com/post/2007/10/30/Content-Query-Web-Part-(CQWP)2c-Cross-List-Query-Nightmare-Part-3.aspx

What happens is that the custom date field gets assigned some generic name in the SQL database. This name is then used in the query by the CQWP. However, there is no guarantee that this name is the same in every content type that uses the custom site column. So the query could be right for only one of your contenttypes. It could also work for most. Maybe even all. But you might not notice you're missing some results until someone else points it out to you...

No comments:

Post a Comment

Rating