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)
{
base.ItemAdding(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;
}