0

I am simply asking if there is a way to keep dynamic a field ID auto incrementing in a list:

I have 3 elements with ID 1, 2, 3

When Item with ID 2 is removed or moved, I want the element with ID 3 to have ID 2.

After some search, I haven't found anything talking about that.

Is this possible?

2

no its not possible ootb in 2010, your talking about changing the identifier column. its easy todo but would require coding tho!

it will get messy quickly! say you have

1 , 2 , 3 , 4 and you removed 2 so you now have 1 , 3 , 4 . you would need to not only change 3 to 2 but anything after it down a notch!

you can do this in code using the item deleting and deleted. passing the value of the last ID and moving it along to the next row instead.

for continuity you should keep the ID column as is, create a new column

    /// <summary>
    /// An item was deleted.
    /// </summary>
    public override void ItemDeleted(SPItemEventProperties properties)
    {
        //start the ID count at 1
        int i = 1;
        int paletteInt = 1;
        //need to loop through each item in the whole custom list
        foreach (SPListItem itm in SPContext.Current.Web.Lists["MyCustomList"].Items)
        {
            //as we are going through each item the ID should match our custom ID column. If not update with the correct ID
            if ((int)itm["myID"] != i)
            {
                //update your custom column ID with the new matched ID
                itm["myID"] = i;

               //check every third number of i int, if it is then update the paletteInt number by one 
                if (i % 3 == 0)
                {
                    paletteInt++;
                }

                //update the item after the above has run
                itm["palette"] = paletteInt;
            }
            //increment the ID on each loop
            i++;
        }
        base.ItemDeleted(properties);
    }
  • 1
    ok thanks, if there isn't an easy way, I won't do that so. It's certainly risky and can cause some bugs in the lists – Jey10 Apr 22 '16 at 8:40
  • wont cause bugs, just unnecessary. the code would run fine....... until you have 1000's of items and it will hit performance issues. :) – Ali Jafer Apr 22 '16 at 8:43
  • just looked at your question and what you want is not possible using calculated columns it only occurs once, only when the item is added and never any other time apart from editing the item! based off that you should use the method above or javascript but even with javascript its messy which would leave the method above as the cleanest way. – Ali Jafer Apr 22 '16 at 9:29
  • just updated my answer. its still the same but adds in what you want from the other question using modulus. – Ali Jafer Apr 22 '16 at 9:32
0

Re-indexing the ID in List/Library is introduced in SharePoint 2013. There is no OOB solution available in SharePoint 2010 to re-index the ID column.

You can index SharePoint 2010 columns with the help of topic mentioned in below link.

SharePoint 2010–Indexing columns in a SharePoint List

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.