Filburt
Filburt

Reputation: 18092

Set Date/Time form field from Silverlight WebResource using .Invoke("setValue")

I try to set a Date field on my form from inside a Silverlight Web Resource but the form field remains empty:

var xrm = (ScriptObject)HtmlPage.Window.GetProperty("Xrm");
var page = (ScriptObject)xrm.GetProperty("Page");
var startdate = (ScriptObject)page.Invoke("getAttribute", "scheduledstart");
startdate.Invoke("setValue", DateTime.Now.ToUniversalTime().ToString("MM/dd/yyyy"));

While the above code works fine for other field types (string, int, OptionSet, etc.) there seems to be an error in the way I try to set the date value.


Side note: I don't use dynamic because it seems not to work when deployed in an MS CRM online environment (but works on-premise).

Upvotes: 1

Views: 319

Answers (1)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241920

You're formatting the date to a string, so it's trying to pass a string into a Javascript Date field. Just pass it as a DateTime. The ScriptObject class is designed to convert properly from .Net to Javascript. reference here

startdate.Invoke("setValue", DateTime.Now);

Upvotes: 1

Related Questions