I have a custom list in sp2013. On this list I have created in vs a eventreceiver on ItemAdding. In this event receiver I would like to get the last item from the list and get some value of a field (number). I would like to increase this number with 1 and set it on the new added item. Now I am wondering what is happen If a normal user is add an item. Does this caml query gets the last item where he has permission on? Or does he get the last item from the list. Also if he dont have permission on the last item ? If it is the last, then I think I need to run the code within RunWithElevatedPrivileges.

public override void ItemAdding(SPItemEventProperties properties)

            string year = DateTime.Now.Year.ToString().Substring(2, 2);

            SPField field = properties.List.Fields[new Guid(properties.Web.Properties[Constants.VOLGNUMMER])];

            SPListItemCollection completedItems = properties.List.GetItems(new SPQuery()
                Query = "<OrderBy><FieldRef Name='ID' Ascending='FALSE' /></OrderBy>",
                RowLimit = 1

            if (completedItems != null && completedItems.Count > 0)
               int test = int.Parse(completedItems[0][field.Id].ToString()) + 1;
                string test2 = test.ToString();

            string nextNumber = string.Format("{0}{1}",
                year, "001");

            if (completedItems.Count > 0)
                nextNumber = (int.Parse(completedItems[0][field.Id].ToString()) + 1).ToString();

            properties.AfterProperties[field.InternalName] = nextNumber;

1 Answer 1


You will only get the last item where the user have access to. You should also consider to make the event receiver from ItemAdding (Synchron) to ItemAdded (asynchron). The item adding might interfere with additional metadata the user enter after uploading and cause "The item was modified by another user exception".

If you like to get the item count from the list you should consider to use SPList.ItemCount. You can also request this directly from the properties.List .

In the item added event receiver you file have also the last last id assigned and you can set the additional field with this ID too.

Another thing you should consider if a item was deleted from the list and you add a new one you might have two items with the same ID.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.