Content Query Web Parts (CQWP) “New” Item Notification

The defaults Content Query Web Parts (CQWP) don’t display the icon next to the titles of the items being brought back. We needed a way to show this icon, or some indicator, if the content is less than 3 days old. I had to add this onto the page somehow. Here is a screen shot of my final result: 
 
CQWP "New" Notification

I first wanted to add the icon to the title of the News section as well as the title of item with JavaScript or jQuery.  But that would be difficult to control and target what I needed based on my data range rules, which item to the icon next to, how to position it, etc. So I resulted in tweaking the XSLT style sheet that the CQWP uses to display my information since it managed the style for each item returned. I referenced 2 Custom Site Columns in order to calculate the article date vs. todays date.  (Heather Solomon has articles talking about the customizing the StyleSheets for CQWP)

I referenced a few posts that I found about doing some date manipulation, specifically the Microsoft SharePoint Designer Team Blog. One article by Andy Lewis talked about date calculations and helped me do what I wanted to do; display the  icon if the content is less than 3 days old.

The Rundown

  • Download the date_templates.xsl file from the MSDN blog here.
  • Upload the date_template.xsl file into your site collection, preferably in your “/Style Library/XSL Style Sheets” folder in MOSS. Or create a separate document library or folder to store this file in.
  • Include the templates in your XSLT style sheet for the CQWP.  If you stored your date_template.xsl file in the location above, add the <xsl:import> line into your DVWP immediately below the
 <xsl:stylesheet
version=”1.0″
exclude-result-prefixes=”x d xsl msxsl cmswrt”
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:ddwrt=”http://schemas.microsoft.com/WebParts/v2/DataView/runtime”
xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” xmlns:msxsl=”urn:schemas-microsoft-com:xslt”>
<xsl:import href=”/Style Library/XSL Style Sheets/date_templates.xsl”/>
  •  Call the template that we need to calculate the days differnce, passing in the appropriate parameters.  For example, I needed to call the getDayDelta template:
<xsl:call-template name=” getDayDelta “> 
<xsl:with-param name=” paramDateA ” select=”@ArticleDate“/> 
<xsl:with-param name=” paramDateB ” select=”ddwrt:Today()“/>
xsl:call-template>

 

Note: You can use other templates from the external template file the same way. You would just need to call them appropriately. The external file is commented to denote what parameters are needed and the name of each template and a brief description of what it does.

The Actual

I designed my style to test to see if the date range between my custom field of “Article Date” and today was less than 3 days. If so, display the  icon in the section head and next to the title of the item.

I first created variables to represent the 3 parts I needed to make the calculations

<!– Grab the articles dates. –> 
       <xsl:variable name=”article_date”>
                <xsl:value-of select=”ddwrt:FormatDate(string(@ArticleStartDate), 1033,1 )”/>
        xsl:variable>
        <!– What is todays date. –>
        <xsl:variable name=”today”>
                <xsl:value-of select=”ddwrt:FormatDate(string(ddwrt:Today()), 1033, 1)”/>
        xsl:variable>        <!– Calculate the difference using the external style sheet "date_templaces.xsl“. –>
        <xsl:variable name=”date_diff”>
                <xsl:call-template name=”getDayDelta”>
                 <xsl:with-param name=”paramDateB” select=”ddwrt:FormatDateTime(string(@ArticleStartDate), 1033,’yyyy-MM-dd’)”/>
                     <xsl:with-param name=”paramDateA” select=”ddwrt:FormatDateTime(string(ddwrt:Today()), 1033,’yyyy-MM-dd)”/>                                                    
        xsl:variable>

 

I then created the style layout that I wanted to use. Basically I added the title of the item linked back to the item and added in the bulleted list style.

<div id=”linkitem”>            
            <a href=”{$SafeLinkUrl}” target=”{$LinkTarget}” title=”{@LinkToolTip}”>               
            </a>
            <br />
        </div>

 

Now lest add in the function of displaying the  icon next to the item title and section title. All we need to do is add in some “if/else” statement wrapped around the icons <img> tag.

                <xsl:if test=”$date_diff <= 3″>
                                <img src=”/_layouts/1033/images/new.gif” id=”new_icon” alt=”New!” style=”border:none;” />
                                    <xsl:text disable-output-escaping=”yes”>
                                                <style type=”text/css”>
                                                                #wellness_new_icon
                                                                 {
                                                                        display:inline!important;
                                                                }
                                                ]]>xsl:text>
                xsl:if>

 

Note: I have some CSS in the <xsl:if> statement to display the actual icon next to the section title. What I did was added the <img> tag in my HTML on the page before hand and made the ID of the image “wellness_new_icon” with an inline style of “display:none;” by default. The <xsl:if> statement will force the image to display if it is true.

The Resources

 

The Done

Here is the completed XSLT style that I used to include the   icon with your CQWP.

 <xsl:template name=”IONNewHomeBullets_CAST” match=”Row[@Style=’IONNewHomeBullets_CAST’]” mode=”itemstyle”>

 <xsl:variable name=”SafeLinkUrl”>
            <xsl:call-template name=”OuterTemplate.GetSafeLink”>
                <xsl:with-param name=”UrlColumnName” select=”‘LinkUrl'”/>
            xsl:call-template>
        xsl:variable>       
        <xsl:variable name=”LinkTarget”>
            <xsl:if test=”@OpenInNewWindow = ‘True'” >_blank
        xsl:variable>

        <!– Grab the articles dates. –>
        <xsl:variable name=”article_date”>
                <xsl:value-of select=”ddwrt:FormatDate(string(@ArticleStartDate), 1033,1 )”/>
        xsl:variable>

        <!– What is todays date. –>
        <xsl:variable name=”today”>
                <xsl:value-of select=”ddwrt:FormatDate(string(ddwrt:Today()), 1033, 1)”/>
        xsl:variable>

        <!– Calculate the difference using the external style sheet "date_templaces.xsl“. –>
        <xsl:variable name=”date_diff”>
                <xsl:call-template name=”getDayDelta”>
                        <xsl:with-param name=”paramDateB” select=”ddwrt:FormatDateTime(string(@ArticleStartDate), 1033,’yyyy-MM-dd’)”/>
<xsl:with-param name=”paramDateA” select=”ddwrt:FormatDateTime(string(ddwrt:Today()), 1033,’yyyy-MM-dd’)”/>
               xsl:call-template>    
        xsl:variable>
        <div id=”linkitem”>

            <xsl:call-template name=”OuterTemplate.CallPresenceStatusIconTemplate”/>
            <a href=”{$SafeLinkUrl}” target=”{$LinkTarget}” title=”{@LinkToolTip}”>
                <xsl:value-of select=”@Title”/>
                <xsl:if test=”$date_diff &lt;= 3″>
                                src=”/_layouts/1033/images/new.gif” id=”new_icon” alt=”New!” style=”border:none;” />
                                    CDATA[
                                                                        <style type=”text/css”>
                                                                                #cast_new_icon
                                                                                {
                                                                                    display:inline!important;
                                                                                }
                                                </style>]]></xsl:text>

                </xsl:if>
            </a>
            <br/>
        </div>
</xsl:template>

One thought on “Content Query Web Parts (CQWP) “New” Item Notification

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.