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. 🙂




  1. Hello Stefano,

    I tried to do the same codeunit with another function content but at the installation of the extension is not triggered when the extension is installed or reinstalled.
    Could you help me ?



  2. Hello Stefano,

    I tried to do the same in a install codeunit with another content function but this function is not triggered at the installation or reinstallation of the app.
    Could you see what is the problem ?



      1. Sorry. No I use the OnInstallAppPerCompany trigger in a “install codeunit” to do some code when the extension is installed but this trigger is not triggered (sorry for the repetition) when the extension is installed or reinstalled. I want to know why.
        Do you get the problem ? Any idea ?


  3. Hi Stefano,

    Has anything changed here? I have used your example (both in code and the XML) and none of my web services appear. My objects are Query not Page, would that make a difference?


      1. Just figured what was happening. I wasn’t using the snippet and my xml was bad so it did not install. (no error that I saw anywhere). Never could get the install codeunit options working but I am happy with the XML file.

        Thanks for the post and the reply

        Liked by 1 person

  4. Hello,
    I tried to do the same example as you with the XML but when I uninstall the extension, the web services that creted with this XML have not been deleted automatically.
    Have you any idea how can I manage this problem please?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.