For calculated columns, SharePoint doesn't allow the use of
volatile functions like "Today" and "Me", although they are allowed to
be used in views.
Internally they are translated in to CAML with <Today/> and <UserID/>
If
you try to create a column with a field type : calculated and if you
try to add ‘[Today]‘ in the formula, it gives the error : “Calculated
columns cannot contain volatile functions like Today and Me”
There are several tricks to overcome this.
The simplest and most popular one is by creating dummy column:
- Open up the List Settings page where you want this column.
- Create column named “Today”. The type doesn’t matter here, so leave the type as “Single Line of Text” and just click “Ok”
- Now create the calculated column where you need to use current date within the formula and add “Today” from available columns
- Once
this is done, we no longer need our own generated column “Today” so you
can delete it (Although when you need to edit the formula you will
need this again, so either recreate this dummy Today field or make it
hidden using code behind. Otherwise SharePoint will again show that
error screen).
- This will trick SharePoint to use [Today] (current date) as part of the formula.
- The
same trick goes for the current user [Me] too. i.e. Create a dummy
field named “Me”, use it in your formula, save the calculated column,
and delete that dummy “Me” field.
This works
well and gives the illusion that now for that calculated column you are
always going to see the value updated as per current date (i.e. Today)
But wait, people forget the most basic thing about SharePoint.
"Calculated fields doesn't update themselves until item is updated."
The
process is when you create new item in list, the calculated field
calculates the value as per the formula and stores it in database. This
cycles happens again when item is updated.
So the value of calculated column is only updated when item goes thru new/update cycle.
This trick works, but the value doesn’t change when it is the next day
i.e.
The calculation is “static”. Do not expect the value to automatically
refresh everyday, it will only be updated the next time you edit the
list item.
Another solution (if you need to dynamically update the Date)
is to use "Computed Fields" instead of calculated fields. Unlike
calculated fields, computed fields recreates its value each time it
displays.
Finally another option is to create a custom DataView using
SharePoint Designer and have one more column which renders the value
using CAML - <Today/> and some nifty XSLT. Each time the DataView
is rendered, it calculates and displays the value.