Setting the AfterProperties of a DateOnly Field

November 2nd, 2008 § 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.

Advertisement

Tagged: , ,

§ 2 Responses to Setting the AfterProperties of a DateOnly Field

Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

Please log in to WordPress.com to post a comment to your blog.

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading Setting the AfterProperties of a DateOnly Field at The SharePoint Swiss Army Knife.

meta

Follow

Get every new post delivered to your Inbox.

Join 584 other followers