Establishing a Connection
Creating a JDBC Data Source
You can create a JDBC data source to connect from your Java application. Creating a JDBC data source based on the CData JDBC Driver for Microsoft Dynamics CRM consists of three basic steps:
- Add the driver JAR file to the classpath. The JAR file is located in the lib subfolder of the installation directory. Note that the .lic file must be located in the same folder as the JAR file.
- Provide the driver class. For example:
cdata.jdbc.dynamicscrm.DynamicsCRMDriver
- Provide the JDBC URL. For example:
jdbc:dynamicscrm:User=myuseraccount;Password=mypassword;URL=https://myOrg.crm.dynamics.com/;CRM Version=CRM Online; or jdbc:cdata:dynamicscrm:User=myuseraccount;Password=mypassword;URL=https://myOrg.crm.dynamics.com/;CRM Version=CRM Online;
The second format above can be used whenever there is a conflict in your application between drivers using the same URL format to ensure you are using the CData driver. The URL must start with either "jdbc:dynamicscrm:" or "jdbc:cdata:dynamicscrm:" and can include any of the connection properties in name-value pairs separated with semicolons.
Connecting to Microsoft Dynamics CRM
To connect, set Url to the root URL of your organization and set User, Password, and CRMVersion.
Authenticating to Microsoft Dynamics CRM
CRM On-Premises
For CRM on-premises, select an authentication method.
By default, the driver uses Windows (NTLM) authentication. To use another authentication type, such as Kerberos delegation, set AuthScheme.
The following is an example connection string:
AuthScheme=Auto;Url=https://myOrg.crm.dynamics.com/;User=username;Password=password;CRM Version='CRM2011+'"
Internet-Facing Deployments
For Dynamics CRM with IFD, set InternetFacingDeployment to true.
The following is an example connection string:
AuthScheme=Auto;Url=https://myOrg.crm.dynamics.com/;User=username;Password=password;InternetFacingDeployment=True;CRM Version='CRM2011+'"
Azure AD
Microsoft Dynamics CRM supports Azure AD. To authenticate using Azure AD, you must create a custom application to obtain the OAuthClientId, OAuthClientSecret to use custom Azure AD credentials.Set your AuthScheme to AzureAD. The rest of the Azure AD flows assume that you have done so.
When you connect, the driver opens the Azure AD endpoint in your default browser. Log in and grant permissions to the application. The driver then completes the following Azure AD process:
- Gets the callback URL and sets the access token and ADFSServer to authenticate requests.
- Saves OAuth values in your connection settings. These values persist across connections.
- Exchanges the returned refresh token for a new, valid access token.
The following is an example connection string for Internet-Facing Deployments:
AuthScheme=OAuth;Url=https://myOrg.crm.dynamics.com/;CRM Version=CRM2011+;InitiateOAuth=GETANDREFRESH;CallbackUrl=http://localhost:33333;OAuthClientId=ClientID;ADFSServer=https://auth.adfs.com/;"The following is an example connection string for Online Instance:
AuthScheme=OAuth;Url=https://myOrg.crm.dynamics.com/;CRM Version=CRMOnline;InitiateOAuth=GETANDREFRESH;CallbackUrl=http://localhost:33333;OAuthClientId=ClientID;AzureTenant=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx;"
Desktop Applications
To authenticate with the credentials for a custom OAuth application, you must get and refresh the OAuth access token. After you do that, you are ready to connect.Get and Refresh the OAuth Access Token
- InitiateOAuth: GETANDREFRESH. Used to automatically get and refresh the OAuthAccessToken.
- OAuthClientId: The client Id assigned when you registered your application.
- OAuthClientSecret: The client secret that was assigned when you registered your application.
- CallbackURL: The redirect URI that was defined when you registered your application.
When you connect, the driver opens Microsoft Dynamics CRM's OAuth endpoint in your default browser. Log in and grant permissions to the application.
After you grant permissions to the application, the driver then completes the OAuth process:
- The driver obtains an access token from Microsoft Dynamics CRM and uses it to request data.
- The OAuth values are saved in the path specified in OAuthSettingsLocation. These values persist across connections.
When the access token expires, the driver refreshes it automatically.
Web Applications
When connecting via a Web application, you need to register a custom OAuth application with Microsoft Dynamics CRM. See Creating a Custom OAuth Application. You can then use the driver to get and manage the OAuth token values. Get an OAuth Access Token
Set one of the following connection properties groups depending on the authentication type to obtain the OAuthAccessToken:
- Client Secret
- OAuthClientId: The client Id in your application settings.
- OAuthClientSecret: The client secret in your application settings.
- Certificate
- OAuthClientId: The client Id in your application settings.
- OAuthJWTCert: The JWT Certificate store.
- OAuthJWTCertType: The type of the certificate store specified by OAuthJWTCert.
You can then call stored procedures to complete the OAuth exchange:
-
Call the GetOAuthAuthorizationUrl stored procedure. Set the AuthMode input to WEB and set the CallbackURL input to the Redirect URI you specified in your application settings. If necessary, set the Permissions parameter to request custom permissions.
The stored procedure returns the URL to the OAuth endpoint.
- Open the URL, log in, and authorize the application. You are redirected back to the callback URL.
- Call the GetOAuthAccessToken stored procedure. Set the AuthMode input to WEB. Set the Verifier input to the "code" parameter in the query string of the callback URL. If necessary, set the Permissions parameter to request custom permissions.
To connect to data, set the OAuthAccessToken connection property to the access token returned by the stored procedure. When the access token expires after ExpiresIn seconds, call GetOAuthAccessToken again to obtain a new access token.
Headless Machines
To configure the driver, use OAuth with a user account on a headless machine. You need to authenticate on another device that has an internet browser.
- Choose one of these two options:
- Option 1: Obtain the OAuthVerifier value as described in "Obtain and Exchange a Verifier Code" below.
- Option 2: Install the driver on another machine and transfer the OAuth authentication values after you authenticate through the usual browser-based flow.
- Then configure the driver to automatically refresh the access token from the headless machine.
Option 1: Obtain and Exchange a Verifier Code
To obtain a verifier code, you must authenticate at the OAuth authorization URL.
Follow the steps below to authenticate from the machine with an internet browser and obtain the OAuthVerifier connection property.
- Choose one of these options:
- If you are using the Embedded OAuth Application click Microsoft Dynamics CRM OAuth endpoint to open the endpoint in your browser.
- If you are using a custom OAuth application, create the Authorization URL by setting the following properties:
- InitiateOAuth: OFF.
- OAuthClientId: The client Id assigned when you registered your application.
- OAuthClientSecret: The client secret assigned when you registered your application.
- Log in and grant permissions to the driver. You are then redirected to the callback URL, which contains the verifier code.
- Save the value of the verifier code. Later you need this for the OAuthVerifier connection property.
On the headless machine, set the following connection properties to obtain the OAuth authentication values:
- InitiateOAuth: REFRESH.
- OAuthVerifier: The verifier code.
- OAuthClientId: (custom applications only) The client Id in your custom OAuth application settings.
- OAuthClientSecret: (custom applications only) The client secret in the custom OAuth application settings.
- OAuthSettingsLocation: The location of the file where the driver saves the OAuth token values that persist across connections.
After the OAuth settings file is generated, you need to re-set the following properties to connect:
- InitiateOAuth: REFRESH.
- OAuthClientId: (custom applications only) The client Id assigned when you registered your application.
- OAuthClientSecret: (custom applications only) The client secret assigned when you registered your application.
- OAuthSettingsLocation: The location containing the encrypted OAuth authentication values. Make sure this location grants read and write permissions to the driver to enable the automatic refreshing of the access token.
Option 2: Transfer OAuth Settings
Prior to connecting on a headless machine, you need to create and install a connection with the driver on a device that supports an internet browser. Set the connection properties as described in "Desktop Applications" above.
After completing the instructions in "Desktop Applications", the resulting authentication values are encrypted and written to the location specified by OAuthSettingsLocation. The default filename is OAuthSettings.txt.
Once you have successfully tested the connection, copy the OAuth settings file to your headless machine.
On the headless machine, set the following connection properties to connect to data:
- InitiateOAuth: REFRESH.
- OAuthClientId: (custom applications only) The client Id assigned when you registered your application.
- OAuthClientSecret: (custom applications only) The client secret assigned when you registered your application.
- OAuthSettingsLocation: The location of your OAuth settings file. Make sure this location gives read and write permissions to the driver to enable the automatic refreshing of the access token.
Admin Consent
Admin consent refers to when the Admin for an Azure Active Directory tenant grants permissions to an application which requires an admin to consent to the use case. The embedded application within the CData JDBC Driver for Microsoft Dynamics CRM, contains no permissions that require admin consent. Therefore, this information applies only to custom applications.
Admin Consent Permissions
When creating a new OAuth application in the Azure Portal, you must specify which permissions the application requires. Some permissions may be marked stating "Admin Consent Required". For example, all Groups permissions require Admin Consent. If your application requires admin consent, there are a couple of ways this can be done.
The easiest way to grant admin consent is to request that an admin log into portal.azure.com and navigate to the application you have created in App Registrations. Under API Permissions, there is a button for Grant Consent. You can consent here for your application to have permissions on the tenant it was created under.
After an admin grants consent, you can perform authentication as normal.
Client Credentials
Client credentials refers to a flow in OAuth where there is no direct user authentication taking place. Instead, credentials are created for just the application itself. All tasks taken by the application are done without a default user context. This makes the authentication flow a bit different from standard.Client OAuth Flow
All permissions related to the client oauth flow require admin consent. This means the application embedded with the CData JDBC Driver for Microsoft Dynamics CRM cannot be used in the client oauth flow. You must create your own OAuth application to use client credentials. See Creating a Custom OAuth Application for more details.
In your App Registration in portal.azure.com, navigate to API Permissions and select the Microsoft Graph permissions. There are two distinct sets of permissions - Delegated and Application permissions. The permissions used during client credential authentication are under Application Permissions. Select the applicable permissions you require for your integration.
You are ready to connect after setting one of the below connection properties groups depending on the authentication type.
- Client Secret
- InitiateOAuth: GETANDREFRESH. Use InitiateOAuth to avoid repeating the OAuth exchange and manually setting the OAuthAccessToken.
- AzureTenant: The tenant you wish to connect to.
- OAuthGrantType: CLIENT.
- OAuthClientId: The Client Id in your application settings.
- OAuthClientSecret: The Client Secret in your application settings.
- Certificate
- InitiateOAuth: GETANDREFRESH. You can use InitiateOAuth to avoid repeating the OAuth exchange and manually setting the OAuthAccessToken.
- AzureTenant: The tenant you wish to connect to.
- OAuthGrantType: CLIENT.
- OAuthClientId: The Client Id in your application settings.
- OAuthJWTCert: The JWT Certificate store.
- OAuthJWTCertType: The type of the certificate store specified by OAuthJWTCert.
Authentication with client credentials takes place automatically like any other connection, except there is no window opened prompting the user. Because there is no user context, there is no need for a browser popup. Connections take place and are handled internally.
Azure Service Principal
Authentication as an Azure Service Principal is handled via the OAuth Client Credentials flow, and it does not involve direct user authentication. Instead, credentials are created for just the application itself. All tasks taken by the application are done without a default user context but based on the assigned roles. The application access to the resources is controlled through the assigned roles' permissions.Create an Azure AD App and an Azure Service Principal
When authenticating using an Azure Service Principal, you must register an application with an Azure AD tenant. Assign a role to the application To access resources in your subscription, you must assign a role to the application.- Open the Subscriptions page by searching and selecting the Subscriptions service from the search bar.
- Select the subscription to assign the application to.
- Open the Access control (IAM) and select Add > Add role assignment to open the Add role assignment page.
- Select Owner as the role to assign to your created Azure AD app.
- setting the properties described "In Both Methods" below.
- setting group properties. Choosing the group depends on the configured application authentication (client secret or certificate).
In Both Methods
Before choosing client secret or certificate authentication, follow these steps then continue to the relevant section below:
- AuthScheme: The AzureServicePrincipal in your application settings.
- InitiateOAuth: GETANDREFRESH. Use InitiateOAuth to avoid repeating the OAuth exchange and manually setting the OAuthAccessToken.
- AzureTenant: The tenant you wish to connect to.
- OAuthClientId: The client Id in your application settings.
Continue with the following:
- OAuthClientId: The client Id in your application settings.
- OAuthClientSecret: The client secret in your application settings.
Authenticating using a Certificate
Continue with the following:
- OAuthJWTCert: The JWT Certificate store.
- OAuthJWTCertType: The type of the certificate store specified by OAuthJWTCert.
Kerberos
See Using Kerberos for details on how to authenticate with Kerberos.
- Client Secret
- InitiateOAuth: GETANDREFRESH. You can use InitiateOAuth to avoid repeating the OAuth exchange and manually setting the OAuthAccessToken.
- AzureTenant: The tenant you wish to connect to.
- OAuthGrantType: CLIENT.
- OAuthClientId: The client Id in your application settings.
- OAuthClientSecret: The client secret in your application settings.
- Certificate
- InitiateOAuth: GETANDREFRESH. You can use InitiateOAuth to avoid repeating the OAuth exchange and manually setting the OAuthAccessToken.
- AzureTenant: The tenant you wish to connect to.
- OAuthGrantType: CLIENT.
- OAuthClientId: The client Id in your application settings.
- OAuthJWTCert: The JWT Certificate store.
- OAuthJWTCertType: The type of the certificate store specified by OAuthJWTCert.