Thursday, June 16, 2011

CRM 2011 ConnectionDialog

 The CRM 2011 SDK contains a really helpful way to connect to CRM using the variety of connection types that CRM offers.

This hidden gem is a WPF control called ConnectionDialog and it can be found in the Microsoft.Xrm.Client.dll.  The SDK lists the ConnectionDialog class but it doesn't contain any examples of how to implement it so I'm providing the code I used to get it to work.

First I create the ConnectionDialog and then Activate it so it will bring the window to the foreground:

            ConnectionDialog dialog = new ConnectionDialog();
            dialog.Activate();

Then you can either show the window modeless or modal.  I chose to show it as modal and capture whether the User cancelled out without entering connection information:


            var result = dialog.ShowDialog();
            if (result == true)
            {
            }

If the User completed the connection wizard then the result will be set to true and then we can use the ConnectionString on the dialog to create an Organization Service like so:


            var connString = dialog.ConnectionString;
            var crmConnection = new CrmConnection(new ConnectionStringSettings("crm", connString));
            var orgService = CrmConfigurationManager.CreateService(_connection);

Now you have your Organization Service created from whichever connection type the User supplied and you can now access the services!

Here is the full snippet:


            ConnectionDialog dialog = new ConnectionDialog();
            dialog.Activate();
            var result = dialog.ShowDialog();
            if (result == true)
            {
                var connString = dialog.ConnectionString;
                var crmConnection = new CrmConnection(new ConnectionStringSettings("crm", connString));
                var orgService = CrmConfigurationManager.CreateService(_connection);
            }

And here is the dialog in action:





Enjoy!
         

Wednesday, June 15, 2011