I was banging my head for a good 4 hours trying to figure this event handler issue out.

Scenario:
Creating an ItemUpdating event receiver that will intercept and disregard an update on a Date Only column/ field type.

Usual Case with Text Fields:

When working with text fields, you would typically do something like this:

this.DiasbleEventFiring();
SPListItem currentItem = properties.ListItem;
string beforeValue = currentItem[fieldDisplayName].ToString();
properties.AfterProperties[internalFieldName] = beforeValue;
this.EnableEventFiring();

Error: If you tried the above with a Date field, you’ll get an error similar to invalid Date/Time value or format.


Not so usual case with Date Fields:

Using a Date Only field, I had to do this:

this.DisableEventFiring();
SPListItem currentItem = properties.ListItem;
DateTime beforeValue = System.Convert.ToDateTime(currentItem.Fields[displayName].GetFieldValueForEdit(currentItem[displayName]));
properties.AfterProperties[internalFieldName] = SPUtility.CreateISO8601DateTimeFromSystemDateTime(beforeValue.AddDays(1)).ToString();
this.EnableEventFiring();

I went through 3 SharePoint Dev books and countless online samples but couldn’t find anything on using the AfterProperties for Date fields so hope this helps you out! Credit goes to clues found in this MSDN SharePoint Forum post.

Advertisements