I need to make a field that increases by 1 with every new item in a sharepoint list. And also make it a read-only field.


Isn't an out-of-the-box ID field enough? It's read only, starts from 1 and is incremented by 1 every time you add an item. It's available in the views as well.

If you need the number to decrement when items are removed, then develop an event receiver which would assign SPList.ItemCount to a read only column.

This probably won't be used, but still:

<Field ID="{347B2202-146B-4ADB-8847-C4EB8996A837}"
    Group="Custom Columns"
    ReadOnly="TRUE" />

Then add an event receiver (make sure the ListTemplateId is correct). You can use ItemAdding instead of ItemAdded - probably would be even better (as you would save the farm from resource-hungry little sucker SPListItem.Update()

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers ListTemplateId="10111">

Override ItemAdded method:

namespace YourNamespace
    public class ReadOnlyIDItemAdded : SPItemEventReceiver
        /// <summary>
        /// An item was added.
        /// </summary>
        public override void ItemAdded(SPItemEventProperties properties)

            var currentList = properties.List;
            var currentItem = properties.ListItem;
            currentItem["ReadOnly_ID"] = currentList.ItemCount;

            base.EventFiringEnabled = false; //In case you have another event receiver attached
            base.EventFiringEnabled = true;

Or ItemAdding:

public override void ItemAdding(SPItemEventProperties properties)
    properties.AfterProperties["ReadOnly_ID"] = properties.List.ItemCount;
    //You might need to increment that by 1 as the ItemCount might not be increased by 1 at that time.
| improve this answer | |
  • that does not work when you delete an item – Atish Dipongkor - MVP Oct 30 '15 at 15:51
  • 2
    Why does it need to work when you delete an item? Should it decrement by 1? As far as I can tell from the requirement "increases by 1 with every new item" the ID field is more than enough. – Paul Strupeikis Oct 30 '15 at 15:56
  • I think OP is already known with this info "increment by 1". No it should not decrement by 1! I just tried to put this info that you have inserted 5 items... then deleted 2..... If you add an item now, what will be the ID? I liked your question Should it decrement by 1?. Nope it should not! – Atish Dipongkor - MVP Oct 30 '15 at 16:05
  • 1
    The decrement part is there because for some people, there cannot be a break in continuity, so an event receiver would have to be used to account for that if desired. – Eric Alexander Oct 30 '15 at 16:11
  • I agree with EricAlexander. But @PaulStrupeikis became angry with my first comment and then he edited! – Atish Dipongkor - MVP Oct 30 '15 at 16:17

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.