Dynamics 365 Business Central: publishing a web service from extensions

In these days I received a (not so common I think) request: how can I have an extension for Dynamics 365 Business Central that, when installed, it automatically published some pages as web services in D365BC?

For doing this task, there are essentially two main methods:

  1. Create an Install codeunit and here create the web service instance by inserting a record in the Tenant Web Service table
  2. Create an XML file with a TenantWebService definition

The second method is in my opinion the best choice and the way to do always.

To create a TenantWebService definition file, you can use the twebservices snippet (we’ll see details later):


To explain the two different methods, I’ve created a small (and unuseful) extension that:

  1. Creates a new table
  2. Creates a new list page based on that table
  3. Has an Install codeunit that publishes the Vendor Card as web service
  4. Has an XML definition file that publishes the Customer Card as web service

The Install codeunit code is the following:


It publishes the Vendor Card page as web service with the name as VendorCardWS.

Then, I’ve created an XML file and here I’ve inserted a TenantWebService definition as follows (for publishing the Customer Card page as web service with CustomerCardWS as service name):


What happens now when you publish this extension? The extension is published to Dynamics 365 Business Central, the Install codeunit started and the OnInstallAppPerCompany trigger does the work of publishing the WS. The result is the following:


In Dynamics 365 Business Central we have now two web service published, where VendorCardWS is published via AL code and CustomerCardWS is published via the XML definition file. Wonderful!!

And now?

What happens now when you uninstall the extension from Dynamics 365 Business Central?


The behaviour that I would like to have is that also the 2 automatically published web services will be removed from Dynamics 365 Business Central during the uninstall phase of the extension. And this is the magic thing here: when you uninstall the extension from Dynamics 365 Business Central, the web service published via XML is automatically removed, while the web service published directly via AL code is not removed.

What’s the conclusion of that? If you want to publish a web service from an extension, always use an XML definition file! This is not well documented now, so remember that

P.S. special thanks to Waldo and DTacconi for playing with me on this.



Comment List