0
votes

I have a Sharepoint 2010 list. I have written event receiver code to update a field value in item added event. I am getting correct result, but it's not updating the field value until I refresh the list. Why?

Code:

public override void ItemAdded(SPItemEventProperties properties)       
{
       base.ItemAdded(properties);

       SPWeb web = properties.OpenWeb();
       SPList list = properties.List;
       int highestValue = 0;

       SPQuery query = new SPQuery();
       query.Query = @"<OrderBy>
                         <FieldRef Name='NextNo' Ascending='FALSE' />
                     </OrderBy><RowLimit>1</RowLimit>";

       SPListItemCollection itemcollection = list.GetItems(query);
       if (itemcollection.Count > 0)
       {
           SPListItem item = itemcollection[0];
           highestValue = Convert.ToInt32(item["NextNo"]);
       }

       SPListItem currItem = properties.ListItem;
       currItem["NextNo"] = highestValue + 1;
       currItem.Update();
}      
1

1 Answers

1
votes

You need to use the ItemAdding event receiver as this fires before the list is updated and will show your changes. The ItemAdded event fires after the list has been updated hence the need to refresh.