So here’s a fun one that we at SharePoint Rx were struggling with the other day.
We were creating a dashboard using the DVWP in SharePoint Designer 2010 and had to perform some date comparisons to control the display of indicators. If you’ve used SPD in the past, you know that the goal is to NOT write code, but to use the “coding by clicking” capabilities of SPD and allow it to write the code (in this case XSL) to control your conditional formatting. Every time I use SPD for something like this, I always seem to run into a new problem, even though I know it should be a simple task.
“Am I the only one with this issue?” he asks.
Here’s the end goal…to display a status indicator for tasks to indicate whether they were completed on time, completed late, not complete and not late, and not complete and late. You can see the four icons I chose to indicate these statuses.
My “conditions” are as follows:
Status equals “completed” and Due Date >= Completed Date
Status equals “completed” and Due Date < Completed Date
Status not equals “completed” and Due Date >= [Current Date]
Status not equals “completed” and Due Date < [Current Date]
One of the issues you immediately experience is the comparison of date values. Using SPD, I did an “un-advanced” comparison in my condition statements and never got the correct results. SPD wrote some XSL like this (I broke it up so it’s a little easier to read):
<xsl:if test=”normalize-space($thisNode/@Status) = ‘Completed’
and
ddwrt:DateTimeTick(ddwrt:GenDisplayName(string($thisNode/@DueDate))) >=
normalize-space(@Completed_x0020_Date)” ddwrt:cf_explicit=”1″>
<img src=”_layouts/images/kpinormal-0.gif” width=”16″ height=”16″ />
</xsl:if>
So we have a “Due Date” that has been operated on by ddwrt:DateTimeTick (see if you can find any documentation on this function) and is returning the number of days from January 1, 1900 comparing to the “Completed Date” in the form “MM/DD/YYYY.” That doesn’t work!
Sidebar rant…Come on, Microsoft! The only documentation on the ddwrt namespace is from a non-MS person (Serge van den Oever ) and is from 2005…for SharePoint 2003?
So, let’s look at the “Advanced” condition criteria that does work.
We used the completely undocumented ddwrt:DateTimeTick function and applied it equally to both dates.
When performing the comparison with [Current Date], here’s what we did.
We were successful with using the $Today variable, but I’ve seen others who have also used ddwrt:Today.
Here are the criteria for all four conditions.
@Status = ‘Completed’ and ddwrt:DateTimeTick(ddwrt:GenDisplayName(string(@DueDate))) >= ddwrt:DateTimeTick(ddwrt:GenDisplayName(string(@Completed_x0020_Date)))
@Status = ‘Completed’ and ddwrt:DateTimeTick(ddwrt:GenDisplayName(string(@DueDate))) < ddwrt:DateTimeTick(ddwrt:GenDisplayName(string(@Completed_x0020_Date)))
$thisNode/@Status != ‘Completed’ and ddwrt:DateTimeTick(ddwrt:GenDisplayName(string($thisNode/@DueDate))) >= ddwrt:DateTimeTick(ddwrt:GenDisplayName(string($Today)))
$thisNode/@Status != ‘Completed’ and ddwrt:DateTimeTick(ddwrt:GenDisplayName(string($thisNode/@DueDate))) < ddwrt:DateTimeTick(ddwrt:GenDisplayName(string($Today)))
Now, I’d like to believe that features in SPD 2010 that were in SPD 2007 were all functioning correctly, but I seem to have lots of problems using the “All formatting visible” functionality. I found that each time I changed this I must
-
Save the page
-
Press F5 to refresh (sometimes more than once)
Not sure what that’s about or if it’s just one of my settings, but it sure is annoying.
Of course, the idea is that you want to make all your conditionally formatted elements visible so you can easily work on them.
FYI, if you want to determine which condition applies to which element, you can select the condition and you should notice a highlighting of the element to which the condition corresponds.
Keywords:
Conditional formatting
SharePoint Designer 2010
Date comparison
Data view web part
ddwrt namespace
Nice post. It helped me a lot because I had a similar problem. Comparing dates was a nightmare for me, because I use Slovene date formating which is ‘dd.MM.yyyy’ and not the US one (‘MM-dd-yyyy’). The problem is that the functions FormatDate and FormatDateTime behave strange. For example I can’t use a dot sign as a delimiter. It only works with the’-‘ sign. In the end I found a workaround and ignored this two functions. My solution for Slovne date format is here http://amavs.wordpress.com/2010/12/16/date-comparison-formatdatetime-function-ddwrt-namespace/.
Very glad it was of some help to you. Great post! Did you experiment any with the DateTimeTick function? I found that to be most useful for date comparison purposes, but not if it doesn’t understand the European date convention.
hi,
i need help in displaying different icons for differernt condition as specified by u.
but at any time i am getting only one icon displayed for any condition.
Please can you give me step by step process of displaying different icons for differernt conditions?
thanks in advance
The decompiled class BaseDdwRuntime (in Microsoft.SharePoint.WebPartPages) shows DateTimeTick returning DateTime.Ticks, which is the number of 100ns intervals since Jan 1, 1900. This means that you can refine your tests to more precise scales than days.
Thanks for the “disassembly!” Good to know.
Man, I wish I would have found this last week! Instead I implemeted a pretty complicated XSL solution to do this when my attempts at using the “OOTB” advanced conditions failed. For future reference, is this methodology useable for date ranges? For example, if an item is due between today and 7 days from today? How would you alter the formatting to add days if it is possible?
Thank you for your help, and great post.
~Tasha
Hi Russell,
Thank you for your post. Great help, but would like to ask how one would set a condition where due date is say 7 days away.
Could you help me with that?
I have a similar issue to the questions about dates within 7 days, I blogged about it here: http://sharepointbydummies.blogspot.com/2011/07/more-problems-with-dates-2010-this-time.html
In summary we have a Due Date field in a list and want to display and alerter depending on if the due date more than a certain number of dates int he future, is closed/recently passed and if it’s passed by more than a certain number of days.
Not solved it yet but there’s a couple of links in my entry that might be useful. Unfortunately we’re all XSL neophytes here and Sharepoint knowledge is just what we’ve picked up as we’ve gone along (mostly in 2007).
It’s fairly easy to do these sorts of calculations in Excel so I’m really surprised the functionality doesn’t exist in Sharepoint.
Any guidance on how we can achieve this would be gratefully received.
Thanks
Stephen
Hi,
I am currently looking for a solution to filter on a dataview to bring back birthdays for current and month and 2 weeks.
I currently use the following:
[substring-after(@ows_BirthMonthNumber,’#’)=number(ddwrt:FormatDateTime(string(ddwrt:Today()),1033,’MM’))]
This brings back for the entire month but i need something that will bring back for 2 weeks in that month.
Any ideas?
Regards
Troy
I have tried to add a column of the 4 status indicators to my dvwp using SPD, but it spits the dummy in IE.
Are there any tricks to adding a column in SPD and the 4 images?
Another solution: http://www.sparqube.com/sharepoint-status-indicator
No code required, condition editor and displayed in list views.
Minuses: it is not free
I have a calculated column of =Today()-[Due Date] which I use to conditionally format an image. My problem is the calculated column does not update each day. It only updates on item creation or edit. Is there a way to “refresh” the item and therefore make the image conditionally format correctly?
No, I believe that is a known issue. You might create a workflow that runs each day to update it. Or you could install and use the HarePoint workflow scheduler, which can take care of lots of issues like these. It’s free!
Hi Russell and thanks for the effort in blogging about this. It has been very useful to me. I have a problem though where I’m working in the context of the English (Ireland) locale (code 6153).
The calculations above only seem to work when I set my site to use the English (United States) locale.
Do you know of any way to get this to work using the English (Ireland) locale? Is there a date transform that can be used?