Monday, May 18, 2009

Setting AfterProperties in Event Receiver

They just couldn’t be the same.

I needed to capture the current date and user when a field was changed. So in the event receiver’s ItemAdding event, I thought I would set the AfterProperties the same way you would normally set an item’s field value:

properties.AfterProperties[userField] = user;
= DateTime.Now;

No dice: Error on both lines of code. So how to set them? To find out I set the fields manually and examined the AfterProperties during the event receiver’s invocation. Here is what I found:

A SPUser field is not an SPUser in the AfterProperties dictionary, it is the SPUser.ID converted to a string. And why wouldn’t it be…

A DateTime field is also a string, except not just a string produced by the DateTime.ToX functions – since that would be too logical. The DateTime AfterProperty is actually a string produced by the SPUtility.CreateISO8601DateTimeFromSystemDateTime method. No – I did not make that up – it is very real. So I guess DateTime in AfterProperties goes all international on you, since that is what that naming convention implies. (see here)

SO here is the working code that captures user and current datetime in the AfterProperties:

properties.AfterProperties[userField] = user.ID.ToString();
= SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Now);


  1. Tried with user.ID.ToString() and... nothing happend. It does not work

  2. Спасибо! Спасли целый день!

  3. ^ That right there is Russian for 'Hell yeah, it works!'

    You're welcome, comrade!


© I caught you a delicious bass.
Back to top