Question

The following articles...

http://moonistahir.wordpress.com/2008/05/05/properties-listitem-in-itemadded-is-null-workaround-to-get-listitem/

http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/8712648e-cf09-4f7b-ab13-1c6aacdf588a/

...attempt to address a problem that I just cannot seem to solve.

Basically, I've deployed an event receiver that deals with the item added event to a custom list I have created. This all works fine, and my code is hit (breakpoints) but I cannot find the newly added list item anywhere in the properties of SPItemEventProperties.

I've tried the two proposed solutions I've posted links to above and my code now currently looks like:

public class EventReceiver1 : SPItemEventReceiver
{
   List<String> explosives = new List<string>() { "gunpowder", "dynamite", "fireworks", "fuel" };

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

       SPSite CurrentSite = new SPSite(properties.SiteId);
       SPWeb CurrentWeb = CurrentSite.OpenWeb(properties.RelativeWebUrl);
       SPList CurrentList = CurrentWeb.Lists[properties.ListId];
       //The line below is where the error is ("Can't find item, may have been deleted by another user")
       SPListItem item = CurrentList.GetItemById(properties.ListItemId);

       if (explosives.Contains(item["Title"].ToString().ToLower()))
       {
           item["Title"] += " (DANGEROUS!)";
           item.Update();
       }
   }
}

But no joy. Any help is greatly appreciated.

EDIT: I should also mention that as an alternative to GetItemById, I did just try using CurrentList.Items.Count - 1 hoping the last item would be the new one, but it wasn't there either.

Was it helpful?

Solution

If you are calling the ItemAdding event, the ListItemId is not going to exist yet. Try using the ItemAdded event instead.

OTHER TIPS

ItemAdded never fires in debug mode, lots of people having same issue

ItenAdded even is asynchronous, but it is firer before item is actually added.

To fix this issue you can sleep thread where ItemAdded method is executed and wait while this property will not be null

int counter = 5;
while (counter > 0 && properties.ListItem == null)
{
    System.Threading.Thread.Sleep(1000);
}
//your code
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top