1

I have a requirement to create a unique ID for a list item in SharePoint 2010 where the ID is in the format of [ACC]-[YEAR]-[SEQUENTIAL_INTEGER] but the [SEQUENTIAL_INTEGER] part needs to reset to 1 each year?

For example, ACC2014000001, ACC2014000002 .. ACC2014000100 and when 2014 ends the integer part reverts to 1 so in 2015 the unique ID's would look as follows: ACC2015000001, ACC2015000002 .. ACC2014000131

What's the most appropriate way to do this in SharePoint 2010?

  • If you can write server side code then Event Receiver is the best option as you need to check the current year, maximum sequential number etc. – Avijit Sur Jan 8 '15 at 11:48
  • @AvijitSur I know about event receivers but part of my question is how do I do the reset the sequential integer part after each year. – motionpotion Jan 8 '15 at 12:13
2

Hers is the code you need to use in Event Receiver...

int newID = 0;

                    //Get current year
                    int currentYear = DateTime.Now.Year;
                    //Your list name
                    string listName = "Test";
                    //CAML string assuming 'Title' is the column contains unique ID
                    string camlString = @"<Where><Contains><FieldRef Name='Title' /><Value Type='Text'>"+currentYear.ToString()+"</Value></Contains></Where>";
                    //Get the list
                    SPList list = web.Lists.TryGetList(listName);

                    //Trigger query and get the result in SPListItemCollection
                    SPQuery query = new SPQuery();
                    query.Query = camlString;
                    SPListItemCollection items = list.GetItems(query);

                    //if already have items with current year
                    if (items.Count > 0)
                    {
                        //convert the SPListItemCollection into List to support LINQ
                        List<SPListItem> listItems = items.Cast<SPListItem>().ToList();
                        //get max id from the result as it will be the max unique value
                        var maxID = (from l in listItems
                                     select l.ID).Max();

                        var item = (from l in listItems
                                           where l.ID == maxID
                                           select l).ToList();
                        //get SPListItem
                        SPListItem maxItem = item[0];
                        //get unique ID from the item
                        string uniqStr = maxItem["Title"].ToString();
                        //extract max unique number 
                        newID = int.Parse(uniqStr.Substring(7));
                    }

                    //increase the unique number
                    newID += 1;

                    //construct the new unique ID
                    string uniqueID = "ACC" + currentYear.ToString() + newID.ToString("D6");

                    //use uniqueID to update your custom unique ID column
| improve this answer | |
  • Can you explain what the "D6" means in your proposed answer and if the answer works when there are no dashes in the ID? – motionpotion Jan 9 '15 at 11:02
  • 1
    I have updated the code which will now take UniqueID without "-". ToString("D6") provides integer formatting with leading zeros...check the link stackoverflow.com/questions/6865325/random-nr-for-000001-000100 – Avijit Sur Jan 9 '15 at 11:27
  • This almost works but as a test I changed and existing item ID ACC2015000006 to ACC2014000006 and added a new item. The expected outcome was that the new ID would be ACC2015000001 (as the requirement is to revert to 1 at the beginning of each year) but the result was that the new item ID was set to ACC2015000006. – motionpotion Jan 9 '15 at 15:20
  • 1
    Is there any other items with ID ACC2015######? – Avijit Sur Jan 9 '15 at 15:27
  • Yes. So I'm confused as to what is supposed to happen. When I deleted all other ACC2015###### items it increments correctly. – motionpotion Jan 9 '15 at 15:34

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.