So, we have a customer who has a library of Newsletters.
These newsletters belong to different regions, and they have a date on which they should be ‘published’ – or at least, a date at which they’re the most recent item. So, we have two columns to capture that – News Region and Publish Date.
The customer wanted a web part on a page that would show the most recently ‘published’ item for each of those regions. Something like:
I did this with the Content Query Web Part (CQWP) – but how?
Well, first I set my CQWP to query the list of newsletters. They have a specific content type that I queried for. I filtered items with a Publish Date in the future out – although not a secure way of preventing users seeing these items, at least it doesn’t make the obvious.
Next, I set the CQWP to group by News Region, and Sort by descenting Publish Date. So far, so standard – but it would also return multiple items for each region, and display their title, not their region.
This called for a special Itemstyle, which I added using the technique mentioned before for putting ItemStyles into a different file. This made sense; we’re not going to use this style extensively, so it’s best to keep it separate.
Next, I modified my CommonViewFields to bring in the News Region column. I’ve mentioned this before.
Then I wrote my XSL:
Take a look at it – it’s cool.
Looking at the code, you can see I read the News Region that we’re looking at into a variable. I then count up the number of preceding nodes which also have the same value for their News Region.
The first node with a particular News Region value will always be the one with the latest Publish Date that isn’t in the future – therefore, the most recent one for that region. The count statement won’t count nodes for other regions, either. So, if it’s the first, we display it – using the News Region text, rather than the title. Simple!
(But a bugger of an XSL query to figure out)