Monthly Archives: March 2011

Creating auto-increment field in SharePoint List

In SharePoint, we can create the auto-incremental field by many ways, I am going to discuss two ways of doing it.

  1. Calculated field
  2. Item Event Receiver

1. Using Calculated field:

Using this we can accomplish it without doing any programing and it is a relatively simple way of doing it. By using “Calculated” column in SharePoint List we can create auto-increment field. We can accomplish this by creating a new column and choosing the column type as “Calculated (calculation based on other columns)”. And in Formula field, we have to enter [ID]

In fact this will be using the values from “ID” field from SharePoint list that starts from 1.

For example, if we want to start our auto-increment column from 100, we can modify the “Formula” field of Create New column screen, we can have to enter [ID] + 99

Auto-Increment field using Calculated column in SharePoint List

2. Using Item Event Receiver:

By using this strategy, users have advantage to edit the existing values, we can also avoid it by making field read-only on feature activation. Using item event receiver, on ItemAdded event, we have to find the highest value among previously added items and then save the incremented value to current newly created auto-incremental column.

In code given below, its looks up for the highest value in the existing items, and adding the incremented value to the newly added item.

    public override void ItemAdded(SPItemEventProperties properties)
        {
            SPWeb web = properties.OpenWeb();
            bool allowUpdates = web.AllowUnsafeUpdates; //store original value
            this.EventFiringEnabled = false;

            try
            {
                web.AllowUnsafeUpdates = true;
                /*get the current list*/
                SPList list = web.Lists[properties.ListId];
                var highestvalue = 0;
                var objQuery = new SPQuery
                                   {
                                       Query ="<OrderBy><FieldRef Name='" + ColumnName 
                                       +"' Ascending='False' /></OrderBy><RowLimit>1</RowLimit>",
                                       Folder = list.RootFolder
                                   };

                SPListItemCollection colItems = list.GetItems(objQuery);
                if (colItems.Count > 0)
                {
                    highestvalue = int.Parse(colItems[0][ColumnName].ToString());
                }

                var currItem = properties.ListItem;
                currItem[ColumnName] = highestvalue + 1;
                currItem.SystemUpdate(false);
                
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
            finally
            {
                this.EventFiringEnabled = true;
                web.AllowUnsafeUpdates = allowUpdates; //restore original value
            }
        }

Happy Coding 😉