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.

Tuesday, May 17, 2011

Writing into Visual Studio Output Window. Part 2


In Part 1 we set up OutputWindowWriter class which performs writing to the Visual Studio output window. What else can we add here?
It would be great if everything worked fine from the first attempt, but this not always happens, especially when dialing with someone’s else code. In our situation we have problem - when output window is initially hidden (by auto hide option at the top right corner of the window next to the cross) and we are writing something there we may get something like this
image

Sunday, May 1, 2011

Selecting first item in WPF ListView and keyboard navigation

I have been playing with WPF ListView recently and I’ve met its following not quite
comfortable behavior – when ListView is focused and I’m trying to navigate inside it using keyboard (in my case it was down arrow) I had to press down key twice to move to the second item and then once for each next item. My goal was to fix that twice pressing (and also enable correct navigation when some other item is selected, not the first one). I thought that it can be archieved by some simple property like SelectedIndex, but despite setting it to necessary value it still didn’t work.

Tuesday, April 12, 2011

Writing into Visual Studio Output Window. Part 1


Lets assume that we have some Visual Studio 2010 Isolated Shell application (I suppose everything mentioned here should be valid for ordinary vspackages, but didn’t test, so cannot be sure). It may perform some operations (actually it is usually written to perform some operation, isn’t it?) and our task is to put log of these operations somewhere. Visual Studio has such nice feature like output window. And it is perfect candidate for such log, so post creates simple class to make it easy to write anything into log.

Thursday, March 31, 2011

Visual Studio Isolated Shell: ToolWindowPane close and its detection

Currently I'm using C# every day. And sometimes I'm coming across situation when solution isn't obvious or easy for me to find. Maybe I’ve just used wrong keywords when performed my search queries but I still hope that this can be useful for someone (at least for me to remember those things and recall them if necessary in the future). I'm going to post such situations here (maybe not only them in the future, but currently it is the main idea of this blog). And since I’ve been working with Visual Studio 2010 Isolated Shell recently, I’m going to start with it.

Here it is the first problem.

What do we have? We have package for VS (Visual Studio) 2010 Isolated Shell mode. It was created for custom project type in VS 2005 then it was migrated to VS 2008 and then changed to support VS 2010 Isolated Shell.

What should we do? One of the tasks was to create wizard based on Microsoft.VisualStudio.Shell.ToolWindowPane. This wizard should query user for some data and create appropriate files after it finishes.


Obviously we should have ability to
  1. Close window when user presses Finish or Cancel buttons
  2. Detect when user closes the wizard