Wednesday, November 18, 2009

Updating Fields in Current ListItem from SPItemEventReceiver.ItemUpdating

First, a thanks to @hafthor for the help.  If you're on Twitter, follow him.  He's full of all kinds of development knowledge, not just .NET/SharePoint.

I remember doing this quite a bit...once upon a time.

The project: IS Service Request Form (SRF) System based in a Custom List.
The task: Send notification emails and update SRF Status field.
The problem: Changing the Status field from the ItemUpdating event.

Setting the status in the ItemAdded event is easy because it's asynchronous.  The synchronous events (ending in -ing), require a different approach.

I had found a couple of blogs that suggested:

this.DisableEventFiring();
properties.ListItem.SystemUpdate(false);

That didn't work.  There was a variation that replaced the ".SystemUpdate(false)" with ".Update()" as well.  That was also incorrect.

So after flailing around for way longer than I should have, I turned to Twitter to save me.
#SharePoint folks, I think I left my brain at #spconn09. I need to change a field value from an ItemUpdating event. How do I do it?
Hafthor replied after a while with the correct answer:
@SamLarko properties.AfterProperties("fieldname")=value
So, to sum it all up, set the field's AfterProperties value to whatever you want it to be, and that will be committed to the database at the end of the procedure.

1 comment:

  1. thanks. i really appreciate your blog. it saved me lot of time.

    ReplyDelete