Using Dynamics 365 Business Central webhooks

Webhooks are a way to create event-driven service integrations: instead of polling another system to check whether there are any changes in entities with webhooks, a client subscribes to events that will be pushed to it from the source system. Dynamics 365 Business Central supports webhooks, so a client can subscribe to a webhook notification (event) and will then automatically receive notifications if an entity in Dynamics 365 Business Central changes.

To use webhooks with Dynamics 365 Business Central, we need to perform the following steps:

  1. A subscriber must register the webhook subscription with Dynamics 365 Business Central by making a POST request to the subscription API and by passing a notification URL in the request body. The endpoint URL is as follows: 
https://api.businesscentral.dynamics.com/v2.0/TENANTID/production/api/v1.0/subscriptions

The request body to establish a subscription is the following (here, we're using the customers entity as an example):

{
"notificationUrl": "YourAplicationUrl",
"resource": "https://api.businesscentral.dynamics.com/v2.0/TENANTID/production/api/v1.0/companies(COMPANYID)/customers",
"clientState": "SomeSharedSecretForTheNotificationUrl"
}
  1. Dynamics 365 Business Central returns a validation token to the subscriber.
  2. The subscriber needs to return the validation token in the response body and provide status code 200 (this is the mandatory handshake phase).
  3. If Dynamics 365 Business Central receives the validation token in the response body, the subscription is registered and notifications will be sent to the notification URL.

When a subscription is established, the subscriber receives a notification for every update on the subscribed entity. Webhook subscriptions expire after 3 days if they are not renewed before that.

To renew a webhook subscription, a subscriber must send a PATCH request to the subscription endpoint (this request requires the handshake phase too). The request endpoint to renew a webhook subscription is as follows:

https://api.businesscentral.dynamics.com/v2.0/TENANTID/production/api/v1.0/subscriptions('SUBSCRIPTIONID')
To renew a webhook subscription, you need to pass the @odata.etag tag of your previously established subscription as an If-Match block in the PATCH request header.

This is the HTTP response that you receive when the subscription is established:

If you try to issue a subscription request again to the same endpoint where an active subscription has been established, you receive the following error:

When a subscription is established, a subscriber can receive notifications when the subscribed entities in Dynamics 365 Business Central are modified. This is an example of a notification sent to a subscriber (the notification is a JSON object that contains all of the modified entities):

{
"value": [
{
"subscriptionId": "customers",
"clientState": "someClientState",
"expirationDateTime": "2019-07-20T07:52:31Z",
"resource": "api/beta/companies(80d28ea6-02a3-4ec3-98f7-
936c2000c7b3)/customers(26814998-936a-401c-81c1-0e848a64971d)",
"changeType": "updated",
"lastModifiedDateTime": "2019-07-19T12:54:20.467Z"
},
{
"subscriptionId": "webhookCustomersId",
"clientState": "someClientState",
"expirationDateTime": "2019-07-20T07:52:31Z",
"resource": "api/beta/companies(80d28ea6-02a3-4ec3-98f7-
936c2000c7b3)/customers(130bbd17-dbb9-4790-9b12-2b0e9c9d22c3)",
"changeType": "created",
"lastModifiedDateTime": "2019-07-19T12:54:26.057Z"
}
]
}

Webhooks are also exposed for custom objects in our extensions. A page with PageType = API will expose webhooks with the following limitations (these also apply to standard API pages):

  • The page cannot have composite keys.
  • The page cannot use temporary tables or system tables as SourceTable.

A subscription to a webhook can be deleted by sending a DELETE request to the /subscriptions({id}) endpoint. Also, to delete a subscription, you need to send a request with the If-Match header containing @odata.etag of the subscription to delete.

For more information about Dynamics 365 Business Central webhooks, I recommend checking this post:

http://demiliani.com/2019/12/10/webhooks-with-dynamics-365-business-central/

In this section, you saw how webhooks work in Dynamics 365 Business Central. In the next section, we'll see how to use Microsoft Graph APIs with Dynamics 365 Business Central.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.93.44