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:
In Visual Studio, create a new WinForm Project.
Right click the solution, select Manage NuGet Packages for Solution.
In the search field, type Cinteros.Xrm
Install Cinteros.Xrm.CRMWinForm.
In the search field, type MscrmTools.Xrm
Install MscrmTools.Xrm.Connection.
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.
Make sure the CRMGrivView is now available.
Add a CRMGridView to the form.
I also added a top panel with buttons to connect and retrieve data, and a label for information.
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.
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); }
Run the program, click Connect, and enter url to a CRM instance.
After connection is established, click Retrieve Data to populate the gridview.
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.
And by altering a few properties inherited from the DataGridView we can polish the appearance even more.
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; } } } }
Labels: Community, Open Source, Tools