Monday, February 19, 2018

Calling Dynamics 365 actions from Logic Apps

Continuing on from my last post, “Calling D365 Web APIs from Logic Apps”, this post will walk you through how to call D365 actions from Logic Apps.
I have created a global action. This action is not doing anything meaningful yet, it simply takes an input parameter and appends “Your Input was” to the front of it, and returns it as an output parameter. Here are the screenshots of the action.
The above screenshot displays the Assign step of the action.

Calling the action from Logic Apps

I have used the Logic App I created in my last blog and modified it slightly. I have only made changes to Step 4 of the app.
For calling an action:
  • Use Post as Method.
  • Provide the D365 Web API Uri. The last part of the Uri is the name of the action I am calling. In this case it is new_ActionLogicApp.
  • Provide the input parameter of the action in the Body.
      “Input”: “MyString”
Save the Logic App and test it.


I have attached a screenshot of Step 4 after execution.

So there we go, we have successfully called a CRM action from the Logic App.

Sunday, February 18, 2018

Calling D365 APIs from Logic Apps

Logic Apps are my favourite topic from last few months. The blog is about calling CRM Web APIs from Logic Apps. Now the question is, why do I want to call the CRM Web APIs directly when Logic Apps provides a Dynamics 365 connector out of the box?

Why call the CRM Web APIs directly?

The main reason is that the Dynamics 365 connector provides only the following actions:
  • Create a new record
  • Update record
  • Delete a record
  • Get record
  • List records
But CRM Web APIs provide much more functionality than listed above. What if you want to call an action, a function, bulk Create/update, transaction integrity, and much, much more? In these scenarios, you want to call the Web APIs directly, instead of using the Dynamics 365 connector.

Authentication Approach

For this blog, we will be using the single tenant server-to-server (S2S) authentication. The best part of using this approach is silent authentication, instead of redirecting the application to the Microsoft login page. Secondly, we can use the “application user” (integration user) to connect to CRM, which does not need a user license, instead of a real user. We can assign the minimum privilege security role to this account as a best practice.

Registering your app to access D365 and creating an application user

Please follow the linked article to register your Logic App in Azure AD and create an application user in D365.
The article’s heading is Use Multi-Tenant Server to Server authentication, but the steps are exactly the same for single tenant.

Creating a Logic App

We will be using the clientid (applicationid in Azure AD) and secret-key to get the authentication token from Azure AD, and use that token to make a Web API call.
The Logic App we are creating for this blog is very simple and has 4 steps.


This is an HTTP trigger that can be called from the external application to trigger the Logic App. This is empty for this exercise, but will be used to pass the information to the Logic App from external data sources in future blogs.

Step 2

This is the main step. I used an HTTP action to call Azure AD for the authentication token.
  • URI- It is an Azure AD authentication URL to get the authorisation token. The guid in the URL is the tenant ID that you can get from the Azure AD’s properties.
  • Headers- I used Fiddler to capture the request and retrieve the headers.
  • Body – In the body I am providing the grant_type, clientid, client_secret and resources values. I got some help from for this. He was using the grant_type of password and providing the CRM user and password in the body.
  • Authentication – it is set to none. I tried to use Oauth 2.0 for authentication and provide the required values, but it never really worked.

Step 3

This step is simply using a JSON parser action to parse the output of Step 2 to use in Step 4.

Step 4

This is the final step that uses an HTTP action to make a CRM Web API call.

Method- I am using a GET method.
URI – The CRM Web API URL. I am using the GET method to retrieve the top 2 Accounts.
Headers – Most of the headers are taken from sample requests from Microsoft websites. The most important of them is Authorization. It is set to the Bearer access_token retrieved in Step 2.

Run the app

Run the app manually. If you get all green ticks then it means everything is working as expected. Double click on the steps to check the input and output of the step.
The following screen shots display the input and output of the HTTP-GetToken and HTTP-Call CRM API steps.

Logic App definition

Replace the clientid and secret-key and test the app.

Thursday, February 1, 2018

OCR Dynamics CRM email attachments

I am very excited about this blog. I was watching a Logic App demo in one of the Microsoft Ignite videos where an order record was created in an ERP system from an order image using Optical Character Recognition (OCR). I am trying to do the something similar using CRM email attachments.


The scenario is when a new CRM email with the image attachments is received/created, the image attachment will be processed by OCR, and the response will be used to create a new Lead record in CRM.

Solution Overview

The most exciting part of this solution is the number of Azure services used in this solution. The Logic App is used as integration layer for the solution. The following diagram depicts the solution architecture and process workflow of the solution.

This is just a learning exercise.  In the above diagram, we could have merged steps 2 to 6 in the Azure Function (webhook receiver) and pass the end result of step 6 to the Logic App.

Process Flow Steps

  • When a new attachment record is created/received in CRM, it passes the RemoteExecutionContext to Azure Function (webhook receiver).
  • The webhook receiver reads the attachment body from the entity attributes and calls the Logic App with the attachment body.
  • The Logic App calls the cognitive services Vision API and passes the body of the attachment.
  • The Vision API runs OCR on the image and passes the response back to Logic App.
  • The response received by the Logic App has some unwanted data, so that response is passed to a second Azure Function (OCR Parser).
  • The OCR parser transforms the response, assigns it to a lead object (class) and sends it back as JSON string.
  • The Logic App parses the JSON string.
  • The Logic App uses the 'Dynamics 365-Create a new record' action to create a new Lead record in D365.


Azure Function as webhook receiver

The first step is to create an Azure function which will receive the RemoteExecutionContext from CRM when a new email attachment is created.This Azure function receives the CRM context, extracts the attachment body and sends it to a Logic App in "binary" format with a content-type of “application/octet-stream”. This is important as Azure Vision API does not receive the image contents in  “application/json” content-types.

Webhook registration in CRM

Register a webhook and add a step on the Create event (Post) of the Attachment entity using the Plugin Registration Tool.

2018-01-31_22-23-36 2018-01-31_22-30-34

Azure Function for OCR parsing

The OCR data has a lot of extra information like regions, lines and boundaries. The parser reads the required data from the response and assigns it to a Lead class. This class is then serialized into a JSON string and sent back to the Logic App.
I got OCR processing classes from another blog. I can't find a name of it now.

Logic App

  1. Create a new Logic App and add an HTTP trigger. The URL of the request is used by the Azure function (webhook receiver).
  2. Add an Optical Character Recognition (OCR) to JSON action. For this step register here to get the URL and key for “Computer Vision API”. Enter the connection information and after the connection is established, set Image Source to Image Content, and Image Content to the Body of the HTTP trigger.

    If successful, this step will return the OCR response.
  3. Add an HTTP action and enter the required information as shown. The action will return the serialized JSON string of lead object.
  4. Add a JSON Parser action that will parse the JSON string received in the last step.
  5. Add a new Dynamics 365-Create a new record action. Assign the values based on the schema of the JSON parser.
  6. Save the Logic App and test the solution.


I tested the solution by sending an email with the following image attachment.

This is the result in D365.

Issues I faced

  1. Originally, I wanted to use a Logic App trigger  (Dynamic 365 -When a record is created) to get the attachment body. For some reason, this trigger does not work for the attachment (activity mime attachment) entity.
  2. Even if you can get the attachment using the Get Lists action by passing the Email entity guid, the result does not contain the body of the attachment. I don’t know if this is by design. In short you cannot get attachment body by using the Logic App D365 connector. This is why I had to use CRM webhooks and Azure functions.
  3. Sometimes the OCR API does not recognize all the text on the image. For example, in the screen shot above letter “l” is missing in the email address ( versus


I believe this integration scenario has massive potential. It can be used to:
  1. Create records based on hand written notes.
  2. Automatically link attachments to appropriate records based on some value on an image or in PDF documents.
  3. Some federal department applications, banking loan applications and RMS (Road and Maritime Services) still use paper based applications. This can save them tons of data entry.