Showing posts with label .Net Framework. Show all posts
Showing posts with label .Net Framework. Show all posts

Friday, December 16, 2011

Stopping save operation from Silverlight control inside CRM 2011

CRM 2011 allows to insert Silverlight controls inside forms. And when control is not just to show data, but to interact with user sometimes you may need to prevent form being saved until user finished some operation. It is quite easy to achieve in javascript, but not that obvious in Silverlight.
Let’s start. CRM 2011 SDK help gives us first clue – Xrm.Page.data.entity has addOnSave method. That’s exactly what we need. But how can we attach event to that object?
First thing to do is to get that object. As CRM 2011 SDK and Training Kit recommend us following way:

Xrm.Page.data.entity
  1. dynamic Xrm = HtmlPage.Window.GetProperty( "Xrm" ) as ScriptObject;
  2. dynamic entity = Xrm.Page.data.entity as ScriptObject;

Next step is to call addOnSave method. Handlers of OnSave method returns boolean value so lets attach some method returning boolean like this

  1. entity.Invoke( "addOnSave", new Func<bool>( OnSave ) );

But just returning bool is not enough to stop save operation. And again SDK helps us here: ExecutionObj.getEventArgs().preventDefault(). It ‘Cancels the save operation, but all remaining handlers for the event will still be executed’. ExecutionObj is something passed to the save event handler. Since we don’t know exact type of this object we will use dynamic:

Attach event
  1. entity.Invoke( "addOnSave", new Func<dynamic, bool>( OnSave ) );

And after this event handler looks like this:

Event handler
  1. private bool OnSave( dynamic eventData )
  2. {
  3.   if( !IsReadyToSave )
  4.   {
  5.     eventData.getEventArgs().preventDefault();
  6.     return false;
  7.   }
  8.  
  9.   return true;
  10. }

That’s it.
P.S. You may choose to return true instead of false when stopping save operation depending on whether you need other handlers to be executed or not. But usually false is enough
P.P.S. When your Silverlight control is inside html webresource that is after that placed on crm 2011 form, you will have to change way how you are getting Xrm reference to something like this

  1. dynamic parent = ( ScriptObject )HtmlPage.Window.GetProperty( "parent" );
  2. Xrm = parent.Xrm;

Thursday, October 20, 2011

Internet Synchronized Clock using C#. Part 2

In previous part we got some methods just to get time from internet. But this may be not enough for the real clock. Let’s tell us current UTC time and will synchronize with internet servers from time to time to ensure accuracy.
First of all, lets put all methods from previous part inside some class, let it be called InternetTimeGetter.
We want to have clock which is synchronized with internet. Since synchronization may take some time or even fail we don’t want our application to freeze while performing synchronization. This means that we need background thread for this. We may use Task class for this. Also we will need some variables to keep current time value and last synchronization time. Let’s start

Friday, July 1, 2011

Internet Synchronized clock using C#. Part 1

Task – show clock that is synchronized with some internet time server.

In this part I’ll just try to get UTC time from the server and in Part 2 I’ll use this code to create simple clock.
Some search pointed me to the http://tf.nist.gov/tf-cgi/servers.cgi page. It contains list of servers placed in US which provide UTC time on demand. Another page from that site (http://www.nist.gov/pml/div688/grp40/its.cfm) gives information about supported protocols. One of the is Network Time Protocol (RFC-1305) and another one is Daytime Protocol (RFC-867). Network Time Protocol (NTP) is a little bit more complicated and it provides more features. Daytime Protocol is simpler, but it should be just enough for this task.