About Me

Having 12 years experience in Microsoft technologies.Since more than 7 years working in SharePoint technologies. Expert in providing consultation for SharePoint projects. Hands on with development and administration.

Sunday, 6 October 2013

Auto increment SharePoint 2010/2007/2013 List column

It is very simple to increment column automatically. There are many ways of doing so,
1. SharePoint designer workflow
2. Calculated columns
3. SharePoint Visual studio workflows
4. Event receivers.
Out of all, option 4 is best feasible for all scenarios. I am not discussing disadvantages of other approaches. But SharePoint item event receives are very powerful in a multi threading (concurrency) scenarios than all other options.
1.  Get the top value from the list. Put following code in method 

private int GetAutoIncrementNumber(SPItemEventProperties properties)

int autoNumber = 0;
SPQuery query = new SPQuery();

           query.RowLimit = 1;

           query.ViewFields = "<FieldRef Name='ID' /><FieldRef Name='Auto_Increment_Number' />";

           query.Query = "<OrderBy><FieldRef Name='ID' Ascending='FALSE'/></OrderBy>"; 

           SPListItemCollection items = properties.List.GetItems(query); 

           if (items.Count > 0)

               string tmpNumber = string.Empty; 

               if (items[1]["Auto_Increment_Number"] != null)

                   tmpNumber = items[1]["Auto_Increment_Number"].ToString();

                   int validWPDNumber;                 

                   autoNumber = Convert.ToInt32(tmpNumber);


return autoNumber;
Call the method in list item event receiver and get the highest value and increment accordingly. It’s very simple and for me working like charm.

No comments:

Post a Comment