The Art of SharePoint Evolution

Stories from a SharePoint Engineer that isn’t afraid of Visual Studio.

Setting the AfterProperties of a DateOnly Field

with 2 comments

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.

Written by Henry

November 2, 2008 at 1:16 pm

2 Responses to 'Setting the AfterProperties of a DateOnly Field'

Subscribe to comments with RSS or TrackBack to 'Setting the AfterProperties of a DateOnly Field'.

  1. [...] Setting the AfterProperties of a DateOnly Field [...]

  2. Dude, you just save my day !!!!

    Thanks

    Manotas

    16 Dec 08 at 9:26 am

Leave a Reply