Setting a Datetime Value in Dynamics CRM 2016 Using C#

Posted by William Schroeder, Director of Development, IT on January 27, 2017


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 need customizations in your Dynamics CRM, send us an email at Support@Crestwood.com.  We can help with customizing and/or training your team. 


Share this:
 
 

Comments:

Add your comment: