Sunday, January 31, 2016

Windows app for Microsoft Dynamics CRM in 5 minutes

 

Thanks to open source components for Microsoft Dynamics CRM, you can develop a WinForm application for CRM in 5 minutes.

 

In this blog article, I will go through a few simple steps to get up to speed developing a client that connects to and shows information from Microsoft Dynamics CRM by using two open source spinoff components: ConnectionManager from XrmToolBox and CRMGridView from FetchXML Builder.

 

These four simple steps are all that is required:

  1. Create project and add NuGet packages
  2. Make VS aware of the imported user control
  3. Configure a form with CRMGridView
  4. Add a few lines of code

1. Create project and add NuGet packages

In Visual Studio, create a new WinForm Project.

image

Right click the solution, select Manage NuGet Packages for Solution.

In the search field, type Cinteros.Xrm

image

Install Cinteros.Xrm.CRMWinForm.

In the search field, type MscrmTools.Xrm

image

Install MscrmTools.Xrm.Connection.

 

2. Make Visual Studio aware of new user control

Open the designer for Form1 that was automatically created.

Open the Toolbox.

Right click in the Toolbox and select Choose Items…

Click Browse…

Find Cinteros.Xrm.CRMWinForm from the downloaded package.

image

Make sure the CRMGrivView is now available.

image

 

3. Configure form with CRMGridView

Add a CRMGridView to the form.

image

I also added a top panel with buttons to connect and retrieve data, and a label for information.

image

 

4. Add a few lines of code

Add the following event handler for click on the Connect button, to connect to CRM using the ConnectionManager provided by MscrmTools:

private void btnConnect_Click(object sender, EventArgs e)
{
    var cs = new ConnectionSelector(false, true);
    if (cs.ShowDialog(this) == DialogResult.OK)
    {
        var conn = cs.SelectedConnections.First();
        ConnectionManager.Instance.ConnectToServer(conn, null);
    }
}

To subscribe to the event of a successful connection, add an event listener in the constructor of the form.

image

In the event handler, replace the throw clause with just one line to get hold of the CRM service.

private void ConnectionSucceed(object sender, ConnectionSucceedEventArgs e)
{
    crmGridView1.OrganizationService = e.OrganizationService;
}

Now add code to retrieve data from CRM and bind it to the CRMGridView.

private void btnRetrieve_Click(object sender, EventArgs e)
{
    var qex = new QueryExpression("account");
    qex.ColumnSet.AddColumns("name", "accountnumber", "primarycontactid", "accountratingcode");
    crmGridView1.DataSource = crmGridView1.OrganizationService.RetrieveMultiple(qex);
}

 

5. Try it!

Run the program, click Connect, and enter url to a CRM instance.

image

After connection is established, click Retrieve Data to populate the gridview.

image

 

6. Polish it!

The view above shows the data retrieved. It contains quite technical information with guids representing lookup fields etc. By altering some properties of the CRMGridView we can produce a more visually appealing view.

image

And by altering a few properties inherited from the DataGridView we can polish the appearance even more.

image

 

Complete code sample for the form:

namespace CRM_viewer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            ConnectionManager.Instance.ConnectionSucceed += ConnectionSucceed;
            ConnectionManager.Instance.ConnectionFailed += ConnectionFailed;
        }

        private void ConnectionSucceed(object sender, ConnectionSucceedEventArgs e)
        {
            crmGridView1.OrganizationService = e.OrganizationService;
            lblInfo.Text = "Connected to: " + e.ConnectionDetail.OrganizationServiceUrl;
            btnRetrieve.Enabled = true;
            UseWaitCursor = false;
        }

        private void ConnectionFailed(object sender, ConnectionFailedEventArgs e)
        {
            lblInfo.Text = "Failed connection: " + e.FailureReason;
            UseWaitCursor = false;
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            var cs = new ConnectionSelector(false, true);
            if (cs.ShowDialog(this) == DialogResult.OK)
            {
                UseWaitCursor = true;
                crmGridView1.OrganizationService = null;
                lblInfo.Text = "not connected";
                btnRetrieve.Enabled = false;
                var conn = cs.SelectedConnections.First();
                ConnectionManager.Instance.ConnectToServer(conn, null);
            }
        }

        private void btnRetrieve_Click(object sender, EventArgs e)
        {
            UseWaitCursor = true;
            var qex = new QueryExpression("account");
            qex.ColumnSet.AddColumns("name", "accountnumber", "primarycontactid", "accountratingcode");
            qex.Criteria.AddCondition("primarycontactid", ConditionOperator.NotNull);
            qex.TopCount = 25;
            try
            {
                var result = crmGridView1.OrganizationService.RetrieveMultiple(qex);
                crmGridView1.DataSource = result;
            }
            finally
            {
                UseWaitCursor = false;
            }
        }
    }
}

1 comment:

  1. Easy to follow and informative directions. Thank you for posting!

    Dynamics GP Ecommerce

    ReplyDelete