Lars Nielsen's Discoveries

October 27, 2010

Set a date field in AfterProperties in an event receiver

Filed under: Development,SharePoint — Lars Nielsen @ 6:37 pm
Tags: ,

SharePoint event receivers always pass in a properties parameter which contains all kinds of useful information about the item (list item or document) that has triggered the event.  Two very useful ones are properties.AfterProperties and properties.BeforeProperties

I recently had to use the AfterProperties object to set the default values on the EditForm.aspx screen that appears when you check in or edit a new item.  The requirement was for a document management site where each document would pick up metadata from its parent folder, so that each time you load a document into a specific folder, its metadata is populated automatically (or at least the fields in the EditForm.aspx page are populated automatically).  I’ll describe how to do that in another post. 
To do this I created an ItemAdding event receiver.  The parent folder has 4 metadata fields:

  • Last Name
  • First Name
  • Employee Number
  • Date of Birth

In the body of the method, there are a series of lines that copy each of these values from the folder, onto the metadata of the document itself.  It does this by setting the items in AfterProperties.  The first three metadata items are text fields and are easy enough:

properties.AfterProperties["LastName"] = folderItem["LastName"];
properties.AfterProperties["FirstName"] = folderItem["FirstName"];
properties.AfterProperties["EmployeeID"] = folderItem["EmployeeID"];

But then I wanted to set the DateOfBirth field which is a date type field in SharePoint. I fiddled about with DateTime objects for a while.  The confusing thing about this is that the DateOfBirth property in AfterProperties is of DateTime class.  So you’d think you should set its value using a DateTime object.  But no!  Eventually I found this post which put me on track. To modify a date field in AfterProperties, you need to set its value as string in ISO8601 format.  So I ended up with code like this:

DateTime d = (DateTime) folderItem["DateOfBirth"];
properties.AfterProperties["DateOfBirth"] = d.ToString("yyyy-MM-ddT00:00:00Z");

That did the trick.  A typical ISO8601 date is for example “1980-06-23T00:00:00Z”  – notice I set the time to be zero because it’s a date with no time component.



  1. Superb.

    Comment by jossamah — September 20, 2011 @ 12:19 pm

  2. Thanks for your post. I ran into this years ago and could not remember the solution! After seeing this, I remembered there is a built in function for this in SP. You can use SPUtility.CreateISO8601DateTimeFromSystemDateTime

    Comment by mwolffy — February 8, 2012 @ 2:11 pm

RSS feed for comments on this post. TrackBack URI

Leave a Reply

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

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

Google+ photo

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

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

Blog at

%d bloggers like this: