Posts Tagged ‘Today’

How not to develop a SharePoint [Today] Calculated column

Date:August 22nd, 2011 Author: Tags: , ,
Category: Calculated Columns, Highlighter, Training Comments:4 ;

After developing SharePoint Highlighter, we seriously considered expanding on this area with a Calculated Column that would allow you to use [Today] in the formula. If you’re thinking “But you can already!” I suggest looking at Ryan’s article about the [Today] column trick.

Needless to say, this would be a very useful tool for almost any SharePoint user. After much researching and prototyping we came to an unfortunate conclusion: Although it was possible to make a Custom Field Type to do this, it was almost certainly impractical (certainly for us).

The avenues we investigated finished in three types of dead-end:

  • Brick wall: A completely impassable system limitation.
  • Overgrown with brambles: Nothing but pain for all involved.
  • Swamps: A long unpleasant slog, with an uncertain outcome.

The Brick Wall

The first thought in any developer’s mind when making a variation on a class should be “Inherit it”. It allows you to take advantage of the existing class’ methods with minimal difficulty and yet offers (almost) total control over its behaviour.

Attempting to do so will reward you with the slightly misleading error:

The type ‘Microsoft.SharePoint.SPFieldCalculated’ has no constructors defined

A brief search in the MSDN documentation for SPFieldCalculated shows that the absence of public constructors is intentional:

Windows SharePoint Services 3.0 does not support inheriting from this class.

And so this avenue ended in a brick wall. More worryingly; disallowing the inheritance of a class is usually a sign that something so heinous is happening inside that Microsoft doesn’t want it reproduced.

Brambles

Intrigued by the promise of horrifying creatures dwelling within, I had a look inside the class to see what was happening (using my own powers of code intuition, and not any kind of questionable reverse engineering method).

It seems the actual calculations take place outside of the SPFieldCalculated class, using instead a call to SPRequest. This new lane of research quickly ended in spiky brambles, as using a direct call to SPRequest would make our product (and hence anything it’s installed on) unsupported by Microsoft.

Undeterred, I took inspiration from the billing system at my old work and looked at keeping the ‘black box of calculation mystery’ class running in the background, with the new class acting as a wrapper around it.

Creating a Custom Field Type that kept a hidden field for data storage in the background was an architecture we’d experimented with when creating Highlighter, so adding a relatively minor call to update the formula with the current date and time seemed quite easy.

The thorny ending in this case was the enormous potential server load; updating the formula every time the list was viewed (to keep it up to date) caused the entire list’s worth of calculations to be refreshed. Viewing just 20 items would make up to 5000 items recalculate their values for each of our columns on the view.

Swamps

The final and most desperate option was to do it ourselves: Completely rewrite all or a subset of the calculated column functions, and handle the associated function nesting.

I’m DIM: Doin’ It M’self.

Following this path would mean wading through the boggy and unpleasant process of exactly reproducing Microsoft’s function calls; avoiding the murky bottomless pools of performance issues. All the while hoping we wouldn’t run into any of the hungry and sharp-toothed show stoppers along the way.

As you may have guessed, we didn’t venture down this path.

Why are you telling me all this?

Because although these paths aren’t available to us as an ISV, it doesn’t mean they aren’t options to you (except the brick wall, of course).

Brambles: Using SPRequest is unsupported, but if this doesn’t deter you then this is still very much an option. Similarly, if you have an abundance of processing power (or really need a space heater in your server room) performing complete column recalculations on every view shouldn’t bother you.

Swamps: If you have very specific requirements you could get away with only recoding a few functions, which would greatly reduce the chances of you disappearing forever. Alternatively, if you have a large herd of idle developers you could set about recreating the whole set. However, I doubt this is a serious option for anyone except the head of the Microsoft Silverlight team.

SharePoint Highlighter – color coding date ranges based on [Today]

Date:August 17th, 2011 Author: Tags: , , ,
Category: General, Highlighter, SharePoint Ideas Comments:2 ;

It is sooo much easier to keep on top of your tasks if you can see at a glance what is due when.

Tasks Highlighted by due date

 

It’s easy to add this color coding to your SharePoint lists with SharePoint Highlighter in just a few clicks, but setting up the rules correctly is a bit of a mind-bender, so in this post we are going to show you how.

So above you can see:

  • Tasks due in less than 7 days highlighted in orange
  • Tasks due in 7-14 days highlighted in yellow.
  • Tasks due in more than 14 days highlighted in green.

So first add a Highlighter column to your list:

http://www.pentalogic.net/sharepoint-products/highlighter/h-manual/h-configuration

Give it a name – I’ve called mine “Due When”.

From the 3 Highlighting Styles choose Highlighting, and choose to Highlight Rows, though this method would work equally well with icons or cell highlighting.

As we are highlighting the whole row we don’t really need to see the actual Highlighter column, so choose to put this to the right of the view, out of the way.

If we were using Auto Setup Rules we would now base the Highlighter column on your Due Date column – this is where it would get its information from.  But what we want to do is a bit too complex for the Auto Setup, so you can leave this box blank and go straight to to the Advanced Rules – click Add Custom.

Scary!  Not really, we just need to think logically about when we are trying to achieve.

The main thing you need to remember is that Highlighter will apply the first rule it finds that is true.  Once it has applied one rule to a row, it will ignore all other rules.

So to get the Highlighting shown above we need this setup:

So, when Highlighter checks the list, first it checks to see if the item’s due date is less than 7 days after today, if it is Highlighter colors the row orange and moves on to check the next list item.  If this rule isn’t met Highlighter checks to see whether the item’s due date is less than 14 days after today, and so on.

It’s all a matter of getting your rules in the right order.  So for example, if we change the order of the rules so the 14 day rule comes first, like this:

This is what happens to the list:

As you can see we have lost our orange highlighting for items that are “Due in Less than 7 days”.  Because Due in less than 14 days is now at the top of our list of rules this is what Highlighter is checking for first, so it is missing the more urgent items.

It’s all about getting your rules in the right order, and as you can see, Highlighter gives you the ability to move items up and down the list with the up/down arrows, or insert or delete a rule at any point.

I hope this has helped to make sense of the advanced rules, and if you have any scenarios that you would like us to work through for you then we would love to hear from you.

Using [Today] in SharePoint calculated default values

Date:September 2nd, 2010 Author: Tags: , , ,
Category: Calculated Columns Comments:16 ;

Despite the infamous “fake today column trick” still appearing in new blog posts on a monthly basis you can’t use Today in calculated columns in SharePoint.

I’ve seen people assume (not unreasonably) that the same rule must apply with calculated default values used in columns that are not calculated columns as well – but strangely,  you actually can use Today in calculated default values, so I am going to explain why it works and how you might use this.

What is the difference between Calculated Column and Calculated Default value?

A calculated column, erm… calculates values from other columns in your list.

SharePoint - add Calculated Column field

For example if you have an orders list with a Quantity and Price column you can calculate a Grand Total of Price * Quantity.

With a calculated default value you use a calculation to set the default value for a column that is not a calculated column – this is the value that is entered automatically when a new list item is created and may be over-written by the user.

SharePoint - setting a calculated default value

So why may I want a calculation using Today as the default value?

Suppose we have a list containing customer complaints and we have a target to resolve everything in 3 days. Sometimes though for complex queries or if there are public holidays we may give more time. In that case we can set the default value as

= Today + 3

But the user can override that if necessary (public holiday etc).

So why can I use Today here but not in calculated columns?

With a calculated column the calculation is only reworked every time a record is updated. When it’s just viewed then it picks up the saved result from the last edit. This makes perfect sense for most calculated values, but not for values using Today or Me.

So for example if we tried to calculate the days left until a due date we might want to use

Days left = Due Date - Today

If we added the complaint on Monday then it would show that we have 3 days left to do it (Thursday). If we just look at the record on the Wednesday it would still show 3 days (as this was the calculation saved on Monday) when in fact we only have 1 day left to keep the customer happy.

That’s why SharePoint wont let you use Today in calculated columns.

However with a default calculated value the result is only worked out once when the record is being created – it’s understood that what is saved in this field is not updated automatically so there is no problem with using Today.

For a bonus point – you can’t reference other fields in calculations for default values (e.g. Start Date or Order Date) as before a record is being created the fields don’t yet contain values.

What else could I do with this?

The OP in this thread is using it to set default financial years. Suppose that your company’s financial year starts in April then you could use this formula to default to 2009/10 or 2010/11 on or after April.

=IF(MONTH(Today)>=4,"2010/11","2009/11")

or generating the year 20xx/xx notation automatically :-

=IF(MONTH(Today)>=4,YEAR(Today) & "/" & (YEAR(Today)+1),(Year(Today)-1) & "/" & YEAR(Today))

It’s also worth noting that you can use a similar formula in calculated columns – suppose you have an Order Date field and the financial year depends upon when the order was placed then simply replace Today with Order Date.

NB – This has been tested with SharePoint 2007/2010.

Free SharePoint Calculated Column Cheat Sheet
a handy 3 page reference for calculated column functions and formulas.

The … ehem… Truth about using [Today] in SharePoint Filters

Date:November 25th, 2008 Author: Tags: , ,
Category: Calculated Columns, Filter, SharePoint Ideas Comments:12 ;

Back in the day of Windows SharePoint Server 2 and SharePoint Server 2003 you couldn’t use [Today] in Filters.

For example if you tried to setup a filter like this to show items added in the last 7 days


"Filter value is not in a supported date format"

Created is greater than [Today] - 7

Created is greater than [Today] – 7

You would receive an error “Filter Value is not a valid date” when clicking OK

(more…)

The Truth about using [Today] in SharePoint Calculated Columns

Date:November 22nd, 2008 Author: Tags: , ,
Category: Calculated Columns, SharePoint Ideas Comments:32 ;

If you try to create a calculated column with a formula containing [Today] or [Me] SharePoint gives the error :

“Calculated columns cannot contain volatile functions like Today and Me”

There are a few reasons you might want to do this. One would be to display how many days left you have to complete something, the other is to only show things added in the last X days or due in the next X days.

There are many pages mentioning this workaround :-

1) Create a dummy column called “Today”, then add your calculated column using [Today] in the formula, finally delete the dummy column. If you need to edit the formula then you have to go back to the start and add the dummy column again.

2) Optionally you can create a calculated column called “My Today” and set its formula to =[Today] using the trick above. Reference [My Today] in your other calculated columns. Thus you reference [My Today] which in turn references [Today]. Then you will not have to add the dummy column back in every time you want to edit formulae.

You then pat your self on the back and head home after another successful day of harassing SharePoint to do what you want, Hurrah!

But the next day you take a look at your list and its wrong. The formula has not updated and its still showing yesterdays value. DOH! SharePoint has made a fool of you after all.

Turns around there is actually a good reason for SharePoint not allowing volatile functions like [Today] or [Me] in calculated columns.

“Calculated fields don’t update themselves until item itself is updated.”

Read that again.

The value of a calculated column is only updated when item is edited and in the mean time you will see incorrect calculations every time you look. In fact, for most uses, this trick don’t give you anything more than just referencing the [Modifed] column.

One workaround to the workaround problem is to write a console app to find all these lists that use the [Today] trick and update them automatically once per day. But this workaround to the workaround problem may not be without its own problems that need to be worked around….

Depending on your need there may be a simpler way – turn the problem on its head!

In the example of using a calculated column to filter out items due in x days or overdue SharePoint will accept [Today] as part of the Filter but will not as part of a calculated column – so give it what it wants

[EDIT- WSS 3 / MOSS 2007 now accepts this]

  • Use a filter of [Created] is greater than [Today]-7

(Note – its essential to have no spaces; [Today] – 7 will not work but [Today]-7 does.

WSS 2 / SPS 2003

  • Create a column called “Filter 7 Days” and formula as =Created+7
  • Then use a filter of “Filter 7 Days” is less than [Today]

See this page for more details – http://blog.pentalogic.net/2008/10/advanced-sharepoint-view-and-filters/

So the calculated column is not dynamic (it will never need updating unless the record itself changes) and SharePoint is happy – but the filter is dynamic and you are happy!

If you want a countdown in your view (i.e. Days left to complete a task) then take a look at this post – http://pathtosharepoint.wordpress.com/2008/08/25/a-countdown-for-tasks-lists/

Shameless plugs

– check out our Highlighter product if you want count-down to Today (e.g. 5 days left, 10 days overdue)

– or check out Reminder if you want “Alert Me” type emails when a task is due soon or overdue.

[EDIT] As it’s been pointed out to me the tone of this article was off from what I intended – the intention is to make clear that this well known and oft mentioned trick does not work as you might expect and to offer alternatives.

Free SharePoint Calculated Column Cheat Sheet
a handy 3 page reference for calculated column functions and formulas.