Connecting to Dynamics CRM
In This Topic
This tutorial describes how to connect to Dynamics 365.
In this walkthrough:
Requirements
In order to connect to Dynamics 365 you need to have the corresponding account, dotConnect for Dynamics 365 (formerly Dynamics CRM)
installed and IDE running. You also have
to know the required connection parameters described below.
Note that if you do not use design-time (specifically, if you do not place
designer DynamicsConnection component from the Toolbox to the form), you have to embed licensing information
manually. This is described in topic Licensing.
Required Connection Parameters
To establish a connection to server you have to provide the required connection parameters
to dotConnect for Dynamics 365. This information is used by DynamicsConnection component to connect to Dynamics 365. The parameters are represented
as a connection string. You can compose the connection string manually or have
dotConnect for Dynamics 365 construct it for you.
The following connection string parameters are required:
- Authentication Type - determines the authentication method used: UserNamePassword or RefreshToken (OAuth 2.0).
- Server - The login URL to the Dynamics 365 service;
- User Id - The Dynamics 365 login account. Required for UserNamePassword authentication;
- Password - The password for the Dynamics 365 login account. Required for UserNamePassword authentication;
- Client ID - one of the pair of OAuth 2.0 credentials, that you can obtain when you register an OAuth application in Azure AD tenant. This parameter is used for RefreshToken authentication.
- Client Secret - the second one of the pair of OAuth 2.0 credentials, that you can obtain when you register an OAuth application in Azure AD tenant. This parameter is used for RefreshToken authentication.
- Refresh Token - the OAuth 2.0 refresh token for Dynamics 365.
The easiest way to get the required parameters is to create a connection at design-time or in the Server Explorer, by performing the procedure described below. Actually, in this case the Client Id and Client Secret are not required, because dotConnect for Dynamics 365 implicitly uses its own values for them. These values are obtained from an embedded OAuth app, registered by Devart. However, it's still recommended to register your own application and using your own values.
If you cannot obtain the refresh token using design time or Server Explorer, registering your own OAuth application is required. You can find more information in the Microsoft documentation
Creating a Connection to Dynamics 365
Design time creation
The following assumes that you have IDE running,
and you are currently focused on a form designer.
- Open Toolbox and find DynamicsConnection component in dotConnect for Dynamics 365 category.
- Double-click the component. Notice that new object appears on the designer underneath the form.
If this is first time you create the DynamicsConnection in this application it is named dynamicsConnection1.
- Click on the dynamicsConnection1 object and press F4 to focus on object's properties.
- Enter the required connection string parameters, described above, to the corresponding boxes.
- Notice that as you assign values to these properties the ConnectionString property is automatically
updated to reflect your settings. Now it contains something like
"Server=https://your_company.crm4.dynamics.com;User [email protected];Password=A123456789;".
- If necessary, click the Advanced button and configure other connection string parameters.
You can find the description of these connection string parameters in the
DynamicsConnection.ConnectionString topic.
Run time creation
You can also configure a connection at run-time by setting its ConnectionString property (note that you have to add references to Devart.Data.Dynamics.dll, Devart.Data.SqlShim.dll, and Devart.Data.dll assemblies):
dynamicsConnection1.ConnectionString = "Server=https://your_company.crm4.dynamics.com;User [email protected];Password=A123456789;";
dynamicsConnection1.ConnectionString = "Server=https://your_company.crm4.dynamics.com;User [email protected];Password=A123456789;"
Using connection string builder
If you decide to setup connection by assigning values to several properties, consider using
DynamicsConnectionStringBuilder
class. It has all of the possible connection settings exposed as properties, thus allowing to customize
the connection at full extent.
DynamicsConnectionStringBuilder connectionStringBuilder = new DynamicsConnectionStringBuilder();
connectionStringBuilder.Server = "https://your_company.crm4.dynamics.com";
connectionStringBuilder.UserId = "[email protected]";
connectionStringBuilder.Password = "mypassword";
DynamicsConnection myConnection = new DynamicsConnection(connectionStringBuilder.ConnectionString);
Dim connectionStringBuilder As DynamicsConnectionStringBuilder = New DynamicsConnectionStringBuilder
connectionStringBuilder.Server = "https://your_company.crm4.dynamics.com"
connectionStringBuilder.UserId = "[email protected]"
connectionStringBuilder.Password = "mypassword"
Dim myConnection As DynamicsConnection = New DynamicsConnection(connectionStringBuilder.ConnectionString)
Notice that in this example we used DynamicsConnection constructor that accepts connection string as argument.
For the information on arguments allowed in the connection string, refer to the description of the
DynamicsConnection.ConnectionString
property.
Creating Connection in Server Explorer
To create a Server Explorer connection, you just need to:
- Click Connect to Database on the Server Explorer toolbar
- If the Dynamics 365 Data Source is not selected by default, click the Change button.
- Select Dynamics 365 Data Source (dotConnect for Dynamics 365) and click OK.
- In the Authentication list, select RefreshToken authentication.
- Specify the Server (login url to the data source).
- Optionally select the Use custom OAuth App check box and specify the custom Client ID and Client Secret. You can obtain these parameters when registering an OAuth app in your Azure AD tenant. See this tutorial for more information.
- Click Web Login.
- In the opened Web Login dialog box sign in to your Dynamics 365 account.
- Review the list of required permissions, select the Consent on behalf of your organization check box, and click Accept.
After this you can browse Dynamics 365 objects in Server Explorer.
Opening connection
Opening a connection is as simple as that:
Of course, the myConnection1 object must have a valid connection string assigned earlier. When you call
Open, dotConnect for Dynamics 365 tries to
find the host and connect to Dynamics 365. If any problem occurs it raises an exception with brief explanation
on what is wrong. If no problem is encountered dotConnect for Dynamics 365 tries to establish the connection during
ConnectionTimeout interval.
Finally, when connection is established, the Open method returns and
State property is changed to Open.
In design time you can connect to server in few steps:
- Right-click on the connection object in designer and then click Connect.
- In the dialog window provide the required connection parameters.
- Click on the connection object and press F4 to focus on object's properties.
- If necessary, click the Advanced button and configure other connection string parameters.
You can find the description of these connection string parameters in the
DynamicsConnection.ConnectionString topic.
- Click Connect to establish connection.
Or you can simply change the State property to Open in the Properties window to establish a connection
using the current connection string.
Closing connection
To close a connection call its Close
method, or set its State property to Closed.
The following example summarizes aforementioned information and shows how to create, setup, open,
use and then close the connection.
DynamicsConnection myConn = new DynamicsConnection();
myConn.ConnectionString = "Server=https://your_company.crm4.dynamics.com;User [email protected];Password=A123456789;";
myConn.Open();
MessageBox.Show(myConn.ServerVersion);
myConn.Close();
Dim myConn As DynamicsConnection = New DynamicsConnection()
myConn.ConnectionString = "Server=https://your_company.crm4.dynamics.com;User [email protected];Password=A123456789;"
myConn.Open()
MessageBox.Show(myConn.ServerVersion)
myConn.Close()
The sample code connects to server, shows its version and then closes the connection.
This actually is a rare usage, because in real applications connections are used by other objects like
DynamicsCommand,
DynamicsDataReader and others.
For more information on this please see corresponding tutorials or reference information.
Modifying Connection
You can modify connection by changing properties of DynamicsConnection object.
Keep in mind that while some of the properties can be altered freely, most
of them close connection when new value is assigned.