Wednesday, June 30, 2010

Using the Row ID value in a calculated column

Row ID cannot be referenced directly in a calculated column. Well that’s not exactly true. You can do it once and the values will stay after the calculated column has been defined, meaning you just put [ID] in the field. But once an Item is added to the list or a record is edited it will no longer have the right reference in the field, it loses the brackets around ID.

Summary of MS reference doc on this issue:
http://office.microsoft.com/en-us/windows-sharepoint-services-help/introduction-to-data-calculations-HA010121588.aspx
- You cannot reference the ID of a row for a newly inserted row. The ID does not yet exist when the calculation is performed.
- You cannot reference another column in a formula that creates a default value for a column.
- This is also valid for other system columns like [Version] or [Modified].
- Applies to SharePoint 2007 and 2010

There are two ways to address the problem.

Short term / non-permanent: The calculated filed can be reset after any adds or edits by adding the brackets around the ID reference, so ID will look like [ID] again.

Long term / permanent:
1. Create a number column w/ 0 deciaml places (Via web portal)
     a. I usually call it LookupID
     b. Create a calculated column that references the LookupID column (E.g. =[LookupID])
          i. Or replace the reference of ID with LookupID for an existing calculated column
2. For lists with existing records
     a. Create a special data sheet view that has ID and LookupID in it. (Via web portal)
     b. Copy ID value to LookupID
3. Now Create a workflow that populates the LookupID column with the actual row ID for new records (Via SharePoint designer)
     a. Set the step action to be “Set Field in current Item”
     b. Select LookupID as the Field
     c. For Value use define workflow value button
          i. Leave Current Item as DataSource
          ii. Select ID as Field from Source
     d. Set LookupID to CurrentItem:ID

10 comments:

Simmi said...

can u plz illaborate how to create the workflow step by step?

Simmi said...

i have followed all the steps but somehow itz not working..can u plz help me in this

Devotion I love to Share said...

Thanks.. It is a cool idea to use Workflow.

Jessica said...

Thanks for the solution. This worked for me!

Carlos said...

Thanks, just what I needed.

dbtech said...

13 years later and it still helped me! thank you for posting.

Shaji Khan said...

Great solution. Learned and applied it today!

Thank you!

Denis Molodtsov said...

25 years later and still valid

Unknown said...

61 years later and this still works

Unknown said...

i ...i just had to write this...

6102 years later... and this still works...