2

hi we have a list in SharePoint 2013 and we want after create a new item in SharePoint 2013 it automatically generate a Title or Code Like HSE -1 and the number increase for each new item in SharePoint list we don't want to use the SharePoint ID

| improve this question | | | | |
2

Your question has two sub questions.

  1. How to create Auto Increment column for SharePoint list?
  2. How to customize the Title or Code (the auto incremented column)?

1. How to create Auto Increment Column for SharePoint list?

Below are two possible and easy ways which you can use.

1. 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

2. 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
            }
        }

See here for more details.

2. How to customize the Title or Code ( the auto incremented column)?

For that you can either modify the same column that incremented or create a new column. If you want to modify the same column, make your change in it while making it a calculated field to add "HSE - " with your ID.

OR

You can create a new calculated column which will add up "HSE -" with the Auto Incremented column. I'll prefer this one as the original Auto Incremented column will be separate from my customized one.

Hope that helps.

| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.