Tuesday, November 07, 2006

Synchronous list event handlers ruin datasheet view

I have written a synchronous list event handler, using the ItemUpdating event to manipulate some data. Now when I try to edit data in the datasheet view I get conflict errors, and I cannot save my changes. Using the regular edit form works. I guess this is a problem in the product, and I hope it will be fixed in RTM. However, since I couldn't find anything about it on the news groups, I guess no one reported it and MS may not know about it.

On a similar matter, some people have been telling me that my code sample for updating list items without firing the events doesnt work for them. I suspect the problem is with different events. The synchronous events ("ing"s) require you to edit the afterproperties, while the a-synchronous ones ("ed"'s) require you to edit the item itself.

Here is a code sample from an event handler I wrote. its not perfect, but its a good sample for what you need to work on.

this.DisableEventFiring();
SPList list; SPListItem item;
switch (properties.EventType)
{
case SPEventReceiverType.ItemAdded:
list = properties.ListItem.ParentList;
item = list.GetItemById(properties.ListItemId);
item[siteLinkFieldName] = itemSiteWrapper.SiteLinkFormatted;
item.SystemUpdate(false);
break;
case SPEventReceiverType.ItemAdding:
break;
case SPEventReceiverType.ItemUpdated:
list = properties.ListItem.ParentList;
item = list.GetItemById(properties.ListItemId);
item[siteLinkFieldName] = itemSiteWrapper.SiteLinkFormatted;
item.SystemUpdate(false);
break;
case SPEventReceiverType.ItemUpdating:
properties.AfterProperties[siteLinkFieldName.Replace(" ", "_x0020_")] = itemSiteWrapper.SiteLinkFormatted;
break;
}
this.EnableEventFiring();

No comments: