Calculated column in Sharepoint shema file based on ID Field is not working RRS feed

  • Question

  • I have created a calculated column Which will generate a sequence number like 1,2,3.....

    I am doing this in schema.xml file.Following is my schema file.

     <Field ID = "{AF301763-E1B7-46D2-BA93-6154ED2B88B6}"
                  DisplayName="Calculated Text"
                  Description="A calculated text field based on Custom Single Line of Text."
              <FieldRef ID="{1d22ea11-1e32-424e-89ab-9fedbadb6ce1}" Name="ID"/>

    But it is not generating the sequence number ,each case it is returning 0

    Please help me for the problem.

    Saturday, September 22, 2012 11:59 AM

All replies

  • Hi Samir,

    The reason the ID field doesn't work well in calculated columns is because the item does not get assigned its ID number until the last moment before it is created.  The calculated column value is resolved prior to the list item getting created, so it does not have an ID number yet.

    Normal order of events:

      • User fills out New Item form
      • New SPListItem in memory is created but not yet added to the list
      • Calculated columns resolve
      • Item is added to the list and is assigned an ID number

    The only way I've found around this is to edit the item again after it is created.  There are several ways to update it automatically.

    You can either create a workflow on the list that triggers when items are added, or write and deploy an event handler that does the same.  Then, within the logic of the workflow or event handler, the item ID will be available so you can use it to populate other columns.

    The new order of events will be:

    1. User fills out New Item form
    2. New SPListItem in memory is created but not yet added to the list
    3. Item is added to the list and is assigned an ID number
    4. Event handler or workflow is triggered on item being created
    5. Event handler or workflow changes a field value based on the ID number

    Workflows are probably easier for you to create, since they can be done "no code" with SharePoint Designer.  However, an Event Handler would give you the ability to perform a System Update instead of a regular update after you edit the list item, so it wouldn't increment the Version number.

    • Edited by thriggle Thursday, September 27, 2012 2:53 AM
    Thursday, September 27, 2012 2:52 AM