GetAttributeValue and null DateTime

Previously, I used to access the value of a property like entity[“attributename”] and cast the result into the appropriate type. The preceding line to this, would always be a check to see if the property exists, as it can cause a KeyNotFoundException, without this check. GetAttributeValue is how I access attribute values these days.

These are some influential posts that made me change my behaviour.
David Berry ->
Guido Preite ->

While this does prevent KeyNotFoundException, it is important to understand the behaviour of GetAttributeValue, w.r.t DateTime. When GetAttributeValue is invoked to retrieve a DateTime attribute, and the value of the attribute is null, it returns a DateTime.MinValue, which is 01/01/0001.

In a scenario where a retrieved value is used to update another record, you’ll have to check if this is DateTime.MinValue before updating, or it will cause an exception like the one below.

The exception thrown is “DateTime is less than minumum[sic] value supported by CrmDateTime. Actual value: 01/01/0001 11:00:00, Minimum value supported: 01/01/1900 00:00:00″.

To prevent this exception, I check if the retrieved DateTime value == DateTime.MinValue, and if so, choose not to update the target property, or set it as null, depending on the requirement. It is also a realisation for me, that CrmDateTime still lives on, somewhere in the Sdk assemblies.

EDIT (29/01/15): Following David’s tip from the comment below, the better approach is to use nullable types with GetAttributeValue, so I should be using DateTime? instead of DateTime.



  1. I did use GetAttributeValue. I noticed that it returned DateTime.Min when 1.) The attribute is in the columnset and it's value is null 2.) When a datetime attribute is not mentioned in the columnset e.g. modifiedon. Please check the result of this code snippet.

    var contact = crmSvc.OrganizationServiceProxy.RetrieveMultiple(new FetchExpression(@””)).Entities[0];

  2. Raj, I'm sorry, my comment contained angle brackets that were buried by the HTML. What I meant to write was: Entity.GetAttributeValue(“modifiedon”). Note the cast of the nullable type: DateTime?

Leave a Reply to Dave Berry Cancel reply

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

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

Facebook photo

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

Connecting to %s