I am using the Content Query Web Part (CQWP) to display news items from news subsites. There are a few capabilities missing from the CQWP that were present in the old SharePoint 2003 News web part. One of them is the display of the "New!" indicator next to items recently added.
You can get the CQWP to dipslay this indicator by editing the style templates for the web part.
To do this, you will need to use SharePoint Designer (SPD). In SPD, open your top level site and expand Style Library, then XSL Style Sheets. Look for ItemStyle.xsl.
ItemStyle.xsl holds the XSL templates that the CQWP uses to render data. Before making changes, it's probably a good idea to make a copy of it by right-clicking on it and using Copy, then Paste. If you screw up your ItemStyle.xsl file, you may get errors in any CQWP throughout your sites.
Double-click on ItemStyle.xsl to edit it and choose to check it out.
ItemStyle.xsl contains a series of templates. Each template corresponds to an Item Style in the drop down in the CQWP properties.
For example, the in the out-of-the-box ItemStyle.xsl, the first template:
<xsl:template name="Default" match="*" mode="itemstyle">
corresponds to the "Image on left" style in the Item Style drop down in the CQWP. The next template:
<xsl:template name="NoImage" match="Row[@Style='NoImage']" mode="itemstyle">
corresponds to the "Title and description" style.
You can either change one of the standard item styles or you can create a new style. You may want to create a new style if you only want the CQWP to display the "New!" indicator when you choose (by selecting your new style). If you change one of the standard templates, it will affect all of the CQWP's thoughout the site collection.
To create a new template, highlight and copy one of the existing templates, then paste it back into the file. Then change the template name and match attributes to give it a unique name. When you check in the ItemStyle.xsl file, you will see your new style as an option in the CQWP's Item Style drop down list.
Let's say we want to change the "Title and description" style so it displays the "New!" indicator anywhere it is used. The template name for this style is "NoImage".
The first thing we need to do is figure out when to display it. We could use some fancy XSL to compare the Created Date of the item to the current date and do some date math to see if it has been created within (say) the last 24 hours.
But this is not necessary - SharePoint will do this for us!
SharePoint comes with an XSLT extension object that is normally used in Data View Web Parts (a.k.a. Data Form Web Parts). But we can use those extensions in our ItemStyle.xsl also.
The functions in the XSLT extension object are described here.
The function we can use is ddwrt:IfNew(). When passed a date/time, it will return true if the date/time is less than two days old.
In order to use the extension object functions, we need to add the namespace to the ItemStyle.xsl file. At the top of the file, it declares a number of namespaces, such as:
xmlns:x="http://www.w3.org/2001/XMLSchema"
xmlns:d="http://schemas.microsoft.com/sharepoint/dsp"
xmlns:cmswrt="http://schemas.microsoft.com/WebParts/v3/Publishing/runtime"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt"
Add the "ddwrt" namespace in this mix:
xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime"
Now we need to add some code to the template so it displays the "New!" indicator, which is a GIF found in the file "_layouts/1033/images/new.gif" (this may have a different location for languages other than English).
Since we are changing the "NoImage" style, look for the start of that template:
<xsl:template name="NoImage" match="Row[@Style='NoImage']" mode="itemstyle">
Find the section of the template that generates the anchor tag (<a>) for the link to the item and displays the description:
<div id="linkitem" class="item link-item">
<xsl:call-template name="OuterTemplate.CallPresenceStatusIconTemplate"/>
<a href="{$SafeLinkUrl}" mce_href="{$SafeLinkUrl}" target="{$LinkTarget}" title="{@LinkToolTip}">
<xsl:value-of select="$MyDisplayTitle"/>
</a>
<div class="description">
<xsl:value-of select="@Description" />
</div>
</div>
We want to put the "New!" indicator just after the title of the item. So change this section so it reads (new part in bold):
<div id="linkitem" class="item link-item">
<xsl:call-template name="OuterTemplate.CallPresenceStatusIconTemplate"/>
<a href="{$SafeLinkUrl}" mce_href="{$SafeLinkUrl}" target="{$LinkTarget}" title="{@LinkToolTip}">
<xsl:value-of select="$MyDisplayTitle"/>
</a>
<xsl:if test="ddwrt:IfNew(string(@Created))">
<img src="/_layouts/1033/images/new.gif" alt="New" />
</xsl:if>
<div class="description">
<xsl:value-of select="@Description" />
</div>
</div>
Save the file, then check it in. Now anywhere you use the CQWP with the "Title and description" style, it will display a "New!" indicator if the item was added within the last two days.