Setting a Datetime Value in Dynamics CRM 2016 Using C#

The below C# pseudocode will set a “Date and Time” attribute to a provided value using the Microsoft Dynamics CRM SDK framework.  This part is very straightforward and can, in fact, be simplified by performing the type casting in one line rather than parsing via multiple lines.

string sSourceValue = “12/01/2016”;
string sDestField = <DateTime_Attribute>;
Entity oEntity = <Defined_Entity>;
DateTime dtValue = DateTime.Parse(sSourceValue);

oEntity[sDestField] = oOutValue;

However, no matter the format or structure of the supplied datetime value, it will never equate to the desired local time without configuring the attribute to display the value as Time-Zone Independent. This is because Dynamics CRM stores all datetime values in UTC format in the database and then displays them based upon the server’s time zone setting.  So, in order to force the supplied value to match precisely what was provided, be certain to configure the attribute Behavior metadata accordingly, as below shown.

If you have questions or need some assistance, visit our support page for more help.

Leave a Reply