locked
Is there easy way to create auto increment number? RRS feed

  • Question

  • Hello SharePoint Fam,

    Is there a easy solution to have a auto increment column that just creates new number on each new item.

    Wanting something like:

    14-0001
    14-0002
    14-0003
    14-0004
    14-0005

    Thanks n advance

    Monday, February 10, 2014 4:33 PM

Answers

  • The item already has an ID. You can run a workflow to put the prefix ("14-") and then the ID at the end.

    Andy Wessendorf SharePoint Developer II | Rackspace andy.wessendorf@rackspace.com

    • Proposed as answer by Jason Warren Wednesday, February 19, 2014 7:07 PM
    • Marked as answer by Patrick_Liang Thursday, March 6, 2014 2:56 AM
    Monday, February 10, 2014 5:37 PM
  • You should be careful using the SharePoint ID column, as when you copy sharepoint items to other lists, etc... the ID doesn't necessarily follow.  Even with 3rd party tools, you can have problems, and you need to make sure that you specify for the list item to keep the ID after it is copied/moved. 

    ID is probably the best way to handle this, although you could use custom code.

    I've done things such as store values in a property bag SPWeb object Properties["customkeyname"], but you have to be careful, and make sure you get the number and increment it within a short amount of time, otherwise, you can run into issues where two items will grab the same ID and it will mess up your numbering system. 

    It seems that you are looking to reset the number based on the year, also (at least from gleaning requirements off of your sample).  In this case, the SharePoint ID will not work, but in your custom code, you would need to account for the year changing, and reset the number...

    • Proposed as answer by Jason Warren Wednesday, February 19, 2014 7:08 PM
    • Marked as answer by Patrick_Liang Thursday, March 6, 2014 2:56 AM
    Monday, February 10, 2014 6:09 PM
  • you can use either calculated field or Item added event event reciever

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


    Hope that helps|Amr Fouad|MCTS,MCPD sharePoint 2010

    • Marked as answer by Patrick_Liang Thursday, March 6, 2014 2:57 AM
    Monday, February 10, 2014 6:17 PM
  • I think that solution was my bad...With [ID] column every time you have to re-submit formula i guess

    I got this screen shot from a forum thread...you can try this:

    I found this screen shot here

    --------------------------------------------------------------

    ***If my post is answer for your query please mark as answer***

    ***If my answer is helpful please vote***


    • Edited by _ShashanK_ Wednesday, February 19, 2014 6:51 PM
    • Marked as answer by Patrick_Liang Thursday, March 6, 2014 2:57 AM
    Wednesday, February 19, 2014 6:49 PM

All replies

  • The item already has an ID. You can run a workflow to put the prefix ("14-") and then the ID at the end.

    Andy Wessendorf SharePoint Developer II | Rackspace andy.wessendorf@rackspace.com

    • Proposed as answer by Jason Warren Wednesday, February 19, 2014 7:07 PM
    • Marked as answer by Patrick_Liang Thursday, March 6, 2014 2:56 AM
    Monday, February 10, 2014 5:37 PM
  • If you need a simple auto increment column then you can use "ID" field which is already present in Sharepoint. 

    It will increment in this fashion: 1,2,3,4...........

    or

    you can take field as "Calculated" and use formula (="14-" & [ID]) without parentheses.

    it will increment in this fashion 14-1, 14-2, 14-3............

    or

    try this link...may be you will find something helpful here...

    http://dlairman.wordpress.com/2011/01/10/add-a-unique-auto-incrementing-column-to-a-sharepoint-list/

    --------------------------------------------------------------

    ***If my post is answer for your query please mark as answer***

    ***If my answer is helpful please vote ***

    • Proposed as answer by Jason Warren Wednesday, February 19, 2014 7:07 PM
    Monday, February 10, 2014 5:51 PM
  • You should be careful using the SharePoint ID column, as when you copy sharepoint items to other lists, etc... the ID doesn't necessarily follow.  Even with 3rd party tools, you can have problems, and you need to make sure that you specify for the list item to keep the ID after it is copied/moved. 

    ID is probably the best way to handle this, although you could use custom code.

    I've done things such as store values in a property bag SPWeb object Properties["customkeyname"], but you have to be careful, and make sure you get the number and increment it within a short amount of time, otherwise, you can run into issues where two items will grab the same ID and it will mess up your numbering system. 

    It seems that you are looking to reset the number based on the year, also (at least from gleaning requirements off of your sample).  In this case, the SharePoint ID will not work, but in your custom code, you would need to account for the year changing, and reset the number...

    • Proposed as answer by Jason Warren Wednesday, February 19, 2014 7:08 PM
    • Marked as answer by Patrick_Liang Thursday, March 6, 2014 2:56 AM
    Monday, February 10, 2014 6:09 PM
  • you can use either calculated field or Item added event event reciever

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


    Hope that helps|Amr Fouad|MCTS,MCPD sharePoint 2010

    • Marked as answer by Patrick_Liang Thursday, March 6, 2014 2:57 AM
    Monday, February 10, 2014 6:17 PM
  • I tried the calculated column and it works initially but for some reason it stops working after first item.....I have to go back to column and add [] around ID each time to make it work.....is there something I am missing to make sure that [] stays permanetley in column???

    Thanks so much n advance

    Wednesday, February 19, 2014 4:45 PM
  • I think that solution was my bad...With [ID] column every time you have to re-submit formula i guess

    I got this screen shot from a forum thread...you can try this:

    I found this screen shot here

    --------------------------------------------------------------

    ***If my post is answer for your query please mark as answer***

    ***If my answer is helpful please vote***


    • Edited by _ShashanK_ Wednesday, February 19, 2014 6:51 PM
    • Marked as answer by Patrick_Liang Thursday, March 6, 2014 2:57 AM
    Wednesday, February 19, 2014 6:49 PM
  • Using event receiver you can also get the auto increment column.

    Please mark as answer if it work for you.

    Use your column name instead of "PONumber".

    Public override void ItemAdding(SPItemEventProperties properties)    {

               try
               {
                   this.EventFiringEnabled = false;
                   string columnName = "PONumber";
                   SPSecurity.RunWithElevatedPrivileges(delegate()
                   {
                   using (SPWeb web = properties.OpenWeb())
                   {
                       web.AllowUnsafeUpdates = true;
                       /*get the current list*/
                       SPList list = web.Lists[properties.ListId];
                       int highestValue = 0;
                       foreach (SPListItem item in list.Items)
                       {
                           if (item[columnName] != null && item[columnName].ToString().Trim() != "")
                           {
                               string value = item[columnName].ToString();
                               try
                               {
                                   int currValue = int.Parse(value);
                                   if (currValue > highestValue)
                                       highestValue = currValue;
                               }
                               catch (Exception ex)
                               {
                                   Trace.WriteLine(ex.Message);
                               }
                           }
                       }
                       SPListItem currItem = list.Items.GetItemById(properties.ListItem.ID);
                       currItem[columnName] = (highestValue + 1).ToString();
                       currItem.SystemUpdate(false);
                       web.AllowUnsafeUpdates = false;
                   }
                   });
                   this.EventFiringEnabled = true;
               }
               catch (Exception ex)
               {
                  Trace.WriteLine(ex.Message);
               }
           }

    Thursday, June 25, 2015 8:57 AM