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)
{
base.ItemAdded(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.Update();
}
else
{
currentItem[columnName] = startValue + incrementValue;
currentItem.Update();
}
}
}
}
}
}
}

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.

sharepoint_autoincrement_solutions

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

sharepoint_autoincrement_site_feature

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.

sharepoint_autoincrement_settings_list

Here is an example of my “AutoIncrementLists” list:

sharepoint_autoincrement_settings_list_example

Here is the example of the “AutoInc1” list:

sharepoint_autoincrement_example1

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

sharepoint_autoincrement_example2

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.

Sources:

http://social.technet.microsoft.com/forums/sharepoint/en-US/39743bb3-872d-4788-96a8-2b9fb8813011/create-auto-increment-value-in-sharepoint-column

http://faisalrafique.wordpress.com/2011/03/19/creating-auto-increment-field-in-sharepoint-list/

http://stackoverflow.com/questions/15929903/sharepoint-list-item-throws-null-error

http://stackoverflow.com/questions/3903986/programmatically-setting-field-value-for-sharepoint-listitem

https://theezitguy.wordpress.com/2014/08/22/sharepoint-event-receiver-to-prevent-documents-from-being-deleted/

Advertisements
Tagged ,

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

%d bloggers like this: