Share via


Call Dynamics 365 from Azure App Service

In case you need to get or modify data in Dynamics 365 while calling from Azure Logic App I would recommend to use an action of Http type. This action provides you to request Dynamics 365 endpoint using most of relevant verbs (methods), among each other: GET, POST, PATCH, DELETE along with authentication header.
This is mandatory to bind your 365 user prior to consume Dynamics 365 Web API. Please refer to my previous blog where explained how to do it.

Dynamics 365 exposes RESTFul Web API with a plenty of features even fairly complex.
Dynamics 365 Web API implements OData protocol hence you need to consider it while constructing requests.

Below you can find the example how the Azure Logic App can be implemented. It accepts a CityName as an input parameter and is combined in the query sent to Web API resulted with accounts with a city of specified.

[caption id="attachment_77" align="alignnone" width="820"]Call Dynamics 365 over Web API Azure Logic App is getting accounts from specified city[/caption]

and a source code:

[caption id="attachment_86" align="alignnone" width="820"]Logic App source code Logic App source code[/caption]

Please note the authentication is critical and you must provision Azure AD Application along with the binding prior to implementing the Logic App.
To obtain values retrieved by the Http action you need to use a property named 'value' which is an array. Logic App has a variety of string, conversion functions which would help you in parsing retrieved data. Please refer to the link .

Calling Dynamics 365 in custom API

There is lightweight approach to consume Dynamics 365 over Application User. The instance which you can further leverage is OrganizationWebProxyClient.

Class signature:
public class OrganizationWebProxyClient : WebProxyClient, IOrganizationService

First of all you need to aquuire the token from Azure AD using for instance ADAL library.

var authenticationContext = new AuthenticationContext(authority); var clientCredentials = new ClientCredential(clientId, clientSecret); AuthenticationResult result = authenticationContext.AcquireTokenAsync(organizationUrl, clientCredentials).Result;

In case you are going to use early binding you would need to inject your type assembly. It is not enough to set the useStrongTypes argument to True in the constructor but load the assembly:
public OrganizationWebProxyClient(Uri serviceUrl, Assembly strongTypeAssembly);

The URL must contain the SDK client version. The client version is set to the version of the SDK assemblies that the client application was linked to. For example:
https://myorg.crm4. dynamics.com/XRMServices/2011/Organization.svc/web?SdkClientVersion=8.2

Please refer to organization service endpoint

What you need to provide:

  • clientId = Azure AD Application ID entered to Application ID attribute in your Dynamics Application User
  • clientSecret = Secret key generated on above Azure AD Application
  • authority = https://login.microsoftonline.com/{tenantId}/ {TenantId} - DirectoryID of your Azure AD
  • organizationUrl = Dynamics 365 instance i.e. https://myorg.crm4. dynamics.com
  • organizationEndpoint = Dynamics 365 instance including organization service endpoint, i.e. https://myorg.crm4. dynamics.com/XrmServices/2011/Organization.svc/web?SdkClientVersion=8.2 return new OrganizationWebProxyClient(organizationEndpoint, Assembly.Load({assembly name}))
  • Finaly, you can return an instance: