SharePoint – Use Event Receiver to Auto-Increment columns

Hello everyone. Today a customer wanted a solution to automatically increment column values. Since he wanted to be able to configure the solution in a simple manner I decided to create an Event Receiver solution.

To create the solution log into your development machine and open Visual Studio. Choose Event Receiver as Project Template and deploy it as a Sandboxed solution.

Here is the code:

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace AutoIncrement.EventReceiver1
/// <summary>
/// List Item Events
/// </summary>
public class EventReceiver1 : SPItemEventReceiver
/// <summary>
/// An item was added.
/// </summary>
public override void ItemAdded(SPItemEventProperties properties)

using(SPWeb web = properties.Web)
int startValue = 0;
int incrementValue = 1;
String columnName = "";
int highestValue = 0;

//Query "AutoIncrementLists" for information
SPList list = web.Lists["AutoIncrementLists"];
SPQuery query = new SPQuery();
query.Query = String.Format("<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>" + properties.ListTitle + "</Value></Eq></Where>");
SPListItemCollection items = list.GetItems(query);

if (items.Count > 0)
//iterate through each Query Result
foreach (SPListItem item in items)
startValue = int.Parse(item["StartValue"].ToString());
incrementValue = int.Parse(item["IncrementValue"].ToString());
columnName = item["ColumnName"].ToString();

//Query current List for highest value
SPList incrementList = web.Lists[properties.ListTitle];
SPQuery incrementQuery = new SPQuery();
incrementQuery.Query = String.Format("<OrderBy><FieldRef Name='" + columnName + "' Ascending='False' /></OrderBy><RowLimit>1</RowLimit>");
SPListItemCollection incrementItems = incrementList.GetItems(incrementQuery);

//Check if query return items
if (incrementItems.Count > 0)
//Check if there are items with a value for the column columnName
if (incrementItems[0][columnName] != null)
highestValue = int.Parse(incrementItems[0][columnName].ToString());

SPListItem currentItem = properties.ListItem;

//Increment column
if (highestValue >= startValue)
currentItem[columnName] = highestValue + incrementValue;
currentItem[columnName] = startValue + incrementValue;

The code does the following:

It iterates through a Custom List called “AutoIncrementLists” and gets information from the columns Title, ColumnName, StartValue and IncrementValue. This information is used to update the current List Item which was added recently. (The solution I created only applies to Custom Lists.)

After you have created the solution you need to upload the solution to your Test site and activate it.


Now activate your Site feature. Mine is called “AutoIncrement for Custom Lists”.


Next you need to create a Custom List called AutoIncrementLists and add the following columns:

  • Title (Single line of text): The Custom Lists Title
  • ColumnName (Single line of text): The Column Name
  • StartValue (Single line of text): The “Begin” of your Counter
  • IncrementValue (Single line of text): The value added to your Counter for each new item

You need this list on every site you want to use this feature.


Here is an example of my “AutoIncrementLists” list:


Here is the example of the “AutoInc1” list:


And here is the example of the “AutoInc2” list:


That´s it. Now you have a solution which allows you to automatically increment your columns every time an item is added. I hope you liked my post. See you next time.

As always you can download my code from here.


Tagged ,

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: