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 ;-)

About these ads

Tagged:

16 thoughts on “Creating auto-increment field in SharePoint List

  1. creating blogs February 25, 2012 at 8:20 am Reply

    creating blogs…

    adwords keywords tool…

  2. Agoes Mulki August 24, 2012 at 9:39 am Reply

    hi Faisal nice to see your blog and useful for me as beginner. base your posted “Creating auto-increment field in SharePoint List” how to create increment by group text. example :
    AAA\1
    AAA\2
    ABB\1
    ABB\2
    ABB\2

  3. faisal December 6, 2012 at 6:10 pm Reply

    Try this formula =”AA\”&ID.

    faisal

  4. Hello there Nice post. Very much appreciated!

  5. solar power cell charger July 17, 2013 at 1:28 am Reply

    Thanks very much, I be grateful for you making this article available, all of those other website is also well done.

    Have a nice day. Thanks a lot!

  6. AB November 19, 2013 at 5:02 pm Reply

    What will happen if multiple user submit the items at the same time? Will it assign the different number? I am having hard time to solve this issue? I am trying with event handler but i am not using your code.

  7. Avneesh December 12, 2013 at 6:21 am Reply

    Very helpful and effective.

  8. Bebo January 20, 2014 at 1:01 pm Reply

    HI faisal!!!!

    I am trying for increment number without ID i.e if we delete any item the series goes off..

    Please help

    • faisal March 10, 2014 at 12:26 pm Reply

      hi Bebo, What if: if you take the code and use the event reciever to make incrementing field.

      Then make another event reciever on item delete. So when an item is deleted, Reset the whole index of this field. Keep in mind delete operation will cause alots of operations.

  9. things to do in miami fl January 31, 2014 at 4:58 pm Reply

    I got this website from my pal who shared with me about this web page and now this time I am
    browsing this web page and reading very informative articles or reviews at this time.

  10. auto-incrementing pain April 10, 2014 at 1:03 pm Reply

    This is not possible in SharePoint 2010, is it?

    • faisal April 10, 2014 at 1:12 pm Reply

      Hi. This is possible in SharePoint 2010. Do you have any doubts?

      • auto-incrementing pain April 11, 2014 at 7:48 am

        Well, I created a calculated column in a list with /* =”C_”&[Type column]&99+[ID] */ and it just keeps printing C_(type letter from choice column)99 with every single entry. We are running 2010.

  11. sUMANTA April 10, 2014 at 6:20 pm Reply

    This Calculated Column Concept is only working with the existing list items, suppose there are 5 items in a list and you created/modified this calculated column.
    This Calculated Column Concept is not working when creating a new item after the calculated column creation/modification AS WHILE CREATING A NEW ITEM IT IS NOT GETTING THE ID WHICH IS GOING TO BE GENERATE.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: