Del via


Modify a date-time value field in Event Handlers – Through AfterProperties

Recently I had a requirement from my customer where it was required to update a date time field through the event handlers and that had to be done in the asynchronous events. Well you may think that this is very simple and setting it with specific date time format would work. But sadly the fields accept a special format which way beyond the formats people normally know.

I tried all the below ways and it failed in all the formats. It will give the following error:

Invalid date/time value. A date/time field contains invalid data. Please check the
value and try again.

properties.AfterProperties["ApprovedTimeStamp"] = DateTime.Now ;
properties.AfterProperties["ApprovedTimeStamp"] = DateTime.Now.ToString(); //
properties.AfterProperties["ApprovedTimeStamp"] = new DateTime(2008, 11, 08) ;
properties.AfterProperties["ApprovedTimeStamp"] = DateTime.Now.ToLongDateString()
properties.AfterProperties["ApprovedTimeStamp"] = DateTime.Now.ToShortDateString()

The correct way to do so is in one of the ways

properties.AfterProperties["ApprovedTimeStamp"] =
DateTime.Now.ToString("yyyy-MM-ddThh:mm:ssZ"); // Works Fine
properties.AfterProperties["ApprovedTimeStamp"] =
Microsoft.SharePoint.Utilities.SPUtility.CreateISO8601DateTimeFromSystemDateTime(Dat
eTime.Now); // Works Fine

Comments