The Content Query Web Part (CQWP) and the ddwrt Namespace

I’m positive I’ve run into this little issue before, but since I don’t seem to have done a post on it, I stymied myself again.

A client of mine spotted a nice post from Ben Tedder (@bentedder) showing how to Create a Classifieds Listing using SharePoint 2010. It uses a bunch of Content Query Web Part (CQWPs) to build up a sort of virtual bulletin board where people can post things they have for sale, help they need, upcoming community events, etc. Ben has been doing a lot of really cool stuff with SPServices, so I’m familiar with his stuff and know that the quality level is high.

Ben’s post gives a nice outline of how to create this type of solution, and we decided I’d implement it. Normally I’d jump straight to the Data View Web Part (DVWP) for something like this, but there’s some appeal to the CQWP in this case. It’s not unlikely that they will want to change both how the categories are laid out on the page and what categories are available, and there’s no reason to involve a developer at that point; the CQWP is pretty easily adjusted by a reasonably sophisticated user.

When I first dropped Ben’s template into the ItemStyles.xsl file, saved it, and refreshed my page, I got an error. No worries, I figured, I must have missed a closing tag or comma or something. About an hour and a half later, I’d tried every version of paring the XSL down that I could think of and had pretty much isolated the issue to where I was displaying the Created date. But I couldn’t for the life of me figure out why it wouldn’t work.

Time for a drive to get some lunch, and within five minutes the answer had occurred to me. (I’m a big proponent of separating myself from the physical location where something has me stumped – I find it lets my mind wander into different territory.)

The problem was that the ddwrt namespace wasn’t registered in ItemStyles.xsl. What that means is that where I was using the ddwrt:FormatDateTIme function, SharePoint had no idea what I was talking about, and it threw an error. When I displayed the Created date without using the ddwrt function, it worked just fine.

[important]If you’re not familiar with the ddwrt namespace, you should be if you write any XSL at all for SharePoint. The only article I’ve ever found which explains it is one on MSDN from Serge van den Oever
which he wrote way back in 2005 called SharePoint Data View Web Part Extension Functions in the ddwrt Namespace. It’s such an important namespace, it always amazes me that this is the only article which explains it on Microsoft’s site.[/important]

Once back in the saddle, I looked at one of my trusty DVWPs and figured out where the references need to be. Here’s the top of the ItemStyles.xsl file before I edited it:

SNAGHTML1de513ac

and after I added the reference to the ddwrt namespace.

SNAGHTML1de594eb

It’s a simple little change, but absolutely necessary if you want to use the ddwrt namespace functions in your CQWPs.

Now, with this post, I’m happy that I won’t paint myself into this corner again, and hopefully it’ll help a few of you to stay out of that corner as well.