1

Does anyone know a way using either the SharePoint 2010 Client Object Model or the SharePoint 2010 web services to evaluate the formula for a given field? For example, if I have a DateTime column whose default value is set to a formula like:

=DATEDIF([Column1], [Column2],"d")

or

=MONTH([TODAY])

I would like to be able to somehow evaluate this formula from a client application.

Thanks in advance for any help anyone can offer.

EDIT:

I am dealing specifically with DateTime calculated default values which seem to be handled differently than a typical "calculated" field.

Steve Danner
  • 21,818
  • 7
  • 41
  • 51

3 Answers3

3

If you just get the value for that field it'll evaluate. The formula itself is stored in the field definition for the list.

So just getting MyListItem["MyCalculatedColumn"] will evaluate the formula.

James Love
  • 1,025
  • 9
  • 16
  • These are great answers, but unfortunately, I do not have a list item when I am attempting to evaluate the formula. – Steve Danner Feb 18 '11 at 19:00
  • In that case, from code, you could maybe use SPListItemCollection.Add(), get the value, but do not do an Update(), and the item won't be written. – James Love Feb 18 '11 at 19:37
2

There is no client side way to evaluate such formula by itself using SharePoint APIs and I don't think server side API is publically available.

Your best option is to update an item and read the field back.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • These are great answers, but unfortunately, I do not have a list item when I am attempting to evaluate the formula. – Steve Danner Feb 18 '11 at 19:00
0

You will find the answer on StackOverflow here.

The part of the object model you need is SPFieldCalculated.

Community
  • 1
  • 1
Nat
  • 14,175
  • 5
  • 41
  • 64
  • Thanks Nat, but these are DateTime columns specifically, I should have said that in the text rather than just stating it as an example. I have edited to show this. – Steve Danner Feb 22 '11 at 22:19
  • Does the SPFieldCalculated.OutputType=SPFieldType.DateTime Property not help? – Nat Feb 23 '11 at 02:09