CDS Web API - Why and How with Logic Apps
  • 31 May 2020
  • 5 Minutes to read
  • Contributors
  • Comment
  • Dark
    Light
  • PDF

CDS Web API - Why and How with Logic Apps

  • Comment
  • Dark
    Light
  • PDF

Article summary

Recently Ive been doing some integration with Common Data Service (CDS) from Logic Apps. One of the cool things with Logic Apps is that there are connectors for many different out of the box connectors for applications and CDS is no different. Ive used both the CDS and Dynamics connectors before and they both allow you to integrate with CDS and Dynamics fairly interchangably because they are really the same product under the hood.

CDS does however have other ways which you can integrate with it and the Flow/Logic App connectors are really just an abstraction from the Web API to make it slightly easier to integrate with CDS. The problem with simplifying things is that while simplicity can make your life simpler in some cases, there are other cases where the simplicity of the connector transfers to complexity in your logic process if you need to do a scenario which is not so simple.

The CDS API is pretty powerful with lots of features and the connectors for Logic Apps only support a subset of what the API can do. Fortunately when you are in this situation you can use the Web API directly from Logic Apps using the HTTP connector to open up these advanced scenarios. In this article I would look at the why and how to do this.

Why

I have touched on the reasons why to a degree above already but to elaborate on these with some examples, a couple of cases where I would definitely consider Web API are below:

JSON Data Mapping

If I had some complex data mapping requirements then one of the challenges with the connector is that I could have complex input data and need to transform the data. I could do something like a Liquid map to transform the input json to a format compatible to send to the CDS web api. In some cases there wouldnt be much difference between the API and Connector and in other cases there could be good benefits in reducing complexity.

Upsert Support

The Web API supports upsert capability via the PATCH http method. This is not supported by the connectors. There is only insert and update. In some cases you might be able to get rid of multiple OOTB actions using the CDS connector and replace them with fewer HTTP ones. An example is an upsert would involve a "List Record" followed by a check if the record exists and then both a "Create Record" and an "Update Record" to cover the 2 cases and probably a loop or query to check the array response from the Get. This could all be potentially replaces with a single HTTP action. 4 or more shapes reduced to 1.

Simplified Configuration

As mentioned above there is lots of opportunity to reduce the number of shapes you need, but also when it comes to DevOps considerations and deployment there is the configuration of the connector to think about and also configuration releated to each action using it. When you move logic apps across environments this can amount to a significant amount of configuration. The HTTP action is really easy to configure and is very little overhead from a DevOps perspective.

Trade Off's

The main trade off when making this decision is that the connector does offer some abstraction and in theory will protect you from some changes related to CDS. For example the url often includes a version number. The connector should handle the version number ever changing without you worrying about it. There are ways you can handle this with configuration management if you use the Web API so its not a significant challenge.

I think it really boils down to the connector being great for getting started and simple scenarios and the Web API is good for advanced scenarios. I also dont see an issue with combining both approaches in your solution. Both are straightforward enough and I think once you are doing serious integration with CDS your team should find both easy enough to use.

How

In one of my demo projects that you may have seen before with the unicorn shop I had a Model Driven Power App which had an orders entity which looked a bit like below.

image.png

I want to walk through a Logic App I have which demonstrates a few simple scenarios.

Authentication

The HTTP connector has an option for the Authentication type called Active Directory OAuth. This will allow you to create a Service Principal in Azure AD and to register it as an Application User in CDS. You would then give the user access to the entities it needs.

In the HTTP connector you would add the details of your service principal to the connector authentication settings like in the below picture.

image.png

Upsert

First off we will use the PATCH method to insert a record. I can use the POST method to just do a create but a PATCH will insert if it doesnt exist and update if it does. When specifying the url for your upsert you need to provide the value you have specified as the primary key for your entity. Lets assume you have chosen to use some business value like an order number as the primary key. If the ID doesnt exist then the record is created and if it does then the record is created.

The below picture shows this request.

image.png

If I were to execute the exact same action again with the same data then the record would be updated.

Get Record

If I want to retrieve the data I have just created then I would provide a GET operation with the same url as earlier to retrieve the record via the id. This is shown below.

image.png

Query Record

If I want to query the record I can use the OData filter format and it would allow me to search for a record via different properties.

An example of this is below:
image.png

Summary

In summary you can see its very easy to use the CDS Web API from Logic Apps via the HTTP connector. I think in practice the more development I am doing with CDS and Logic Apps the more I am finding direct Web API integration over HTTP is more productive than the connector and in professional developer scenarios the XRM Toolbox


Was this article helpful?