D 365 Development – Different ways to connect/authenticate Dynamics 365
There was a question posted on my blog by a Dynamics 365 newbie developer on how to authenticate to Dynamics 365 online from his console application.
In this article I am going to detail various ways to connect to Dynamics 365.
Option 1 – Connect using Dynamics 365 SDK assemblies:
- You need Dynamics 365 SDK assemblies, If you are creating plug-ins, custom workflow activities, or custom XAML workflows and performing operations (i.e., Create/Update/Execute/Retrieve)
- Refer Steps to create a basic plug-in
- Download the latest SDK assemblies from nuget.
Option 2 – Connect using XRM Tooling assemblies:
- If you are building .Net applications (i.e., Console/Web/Windows) use the XRM Tooling assemblies to connect to the Dynamics Application.
- XRM tooling enables you to connect to your Dynamics 365 instance by using connection strings.
- Refer article for different types of Connection strings based on your Dynamics deployment (i.e., One-prem/IFD/Office 365 etc..)
- Below is the sample code to connect to your instance from Console:
Prerequisites:
- Download the latest SDK assemblies from nuget in your console application.
- Make sure you refer below .dlls in your console class file.
- using Microsoft.Xrm.Sdk;
- using Microsoft.Xrm.Tooling.Connector;
App.Config:
// Add below Connection string to your console’s App.config file
<connectionStrings>
<add name=”Xrm” connectionString=”Url=https://{orgname}.crm.dynamics.com; Username=rajeevpentyala@yourdomain.onmicrosoft.com; Password=XXXXXXX;authtype=Office365;Timeout=20″ />
</connectionStrings>
Code:
// Declare the Service Variables
private static OrganizationServiceProxy _serviceProxy;
private static IOrganizationService _service;
// Read the connection string configured in App.config file
var connectionString = ConfigurationManager.ConnectionStrings[“Xrm”].ConnectionString;
var crmConn = new CrmServiceClient(connectionString);
using (_serviceProxy = crmConn.OrganizationServiceProxy) {
_service = _serviceProxy;
var reqWhoAmI = new WhoAmIRequest();
var resp = (WhoAmIResponse)_service.Execute(reqWhoAmI);
var buID = resp.OrganizationId.ToString();
var userID = resp.UserId.ToString();
}
- Refer my post on steps to connect to D 365 using Xrm.Tooling.Connector
Option 3 – Connect using Dynamics 365 Web API:
- What if you want to connect to Dynamics from a Non- .NET applications (i.e., Java/PHP applications), the solution is Web API.
- Web API provides development experience that can be used across a wide variety of programming languages, platforms, and devices.
- Web API uses no DLL approach; Unlike above 2 approaches (i.e., XRM Tooling/SDK Assemblies), you don’t need to refer assemblies to connect to Web API.
- There are 3 different ways to connect to Web API
- Using JavaScript in from Dynamics web resources (i.e., Jscript files, HTML, Ribbon). We don’t need to include any authentication code as the logged-in user is already authenticated by the application.
- Refer this article
- If your Dynamics 365 is On-premise, you can authenticate Web API by passing User’s network credentials.
- If your Dynamics 365 is Online or IFD, you must use OAuth to connect.
- The recommended authentication API for use with the Dynamics 365 Web API is Azure Active Directory Authentication Library (ADAL)
- Using JavaScript in from Dynamics web resources (i.e., Jscript files, HTML, Ribbon). We don’t need to include any authentication code as the logged-in user is already authenticated by the application.
- Refer this article on steps to connect to Web API.
- Web API is very convenient to use and test.
- With Postman tool you can connect to Web API and perform operations. Refer article
Below is the Flow diagram gives idea on when to use which option among the 3 options:
🙂
-
September 18, 2018 at 10:41 PM[Code Snippet] Authenticate and Perform Operations using D365 Web API and C# | Rajeev Pentyala - Dynamics 365 Blog