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:
- Create an Install codeunit and here create the web service instance by inserting a record in the Tenant Web Service table
- 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:
- Creates a new table
- Creates a new list page based on that table
- Has an Install codeunit that publishes the Vendor Card as web service
- 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!! 🙂
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. 🙂
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 ?
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 ?
Sorry, not clear. Do you mean that you’ve created a web service definition in XML like described here and your page was not published?
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 ?
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?
Nothing should be changed. Are you using the twebservice snippet?
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
LikeLiked by 1 person
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?
Have you created the XML definition with the snippet? What D365BC version?
Hello I have create the XML definition with the snipet but nothing in my sandbox is publish.
Any idea ?