.Net Interop: Consume a third party web service (Check VAT Registration No.)

Some time ago, I did a session at Direction EMEA in Berlin. Apparently, some people were inspired with this. Not only did I introduce "cannibalism" (never expected this to be something "hot, new and inspiring" but rather something "obvious" - let's spend a blogpost on that as well.. :) ), but also I showed an example of consuming a third party web service. And some time ago (quite some time, actually), I promised one of the attendees to blog about the VAT example I showed there.

In fact, you can see this as "consuming a third party web service". There are tons of those out there .. and this example is quite useful, if you ask me (more useful than getting the weather info, or a list of songs of a certain genre, is it.. ;°) ).

What Web Service?

A functional consultant pointed me to a certain official webservice that had some promising capabilities. This is the website that uses that same webservice:


As you can see, you can validate the VAT Number in quite some European countries. This is something customers definitely want .. so what if we could build such a functionality in NAV. We would need a web service, right? Well, here it is, just for free:


Basically, using this web service, is kind of the same way as using any web service - so also quite the same as I described in this blogpost: NAV 2009 R2 .Net Interop: Calling a Web Service (come to think of it - I can make this blog quite short and let's share the objects I created here).

Building the Proxy

This will take the majority of your time - at least 2 minutes (because Visual Studio doesn't load very fast). I'm not going to create a service reference, but a web reference.

  • Create a new project in Visual Studio
  • Make sure you're using .Net Framework 3.5
  • Create a class library called "VATValidation" (at least I did)
  • So you get this:

Now, right click "References" and click "Service Reference":

As I mentioned, I'm going to use a web reference, so let's click Advanced / Add Web Reference and provide the url I mentioned above. Give it a proper name (I used "VATValidationService") and click "Add Reference":

That's it, people! Build you're solution and get the hell out of Visual Studio!


You're safe now .. you're almost back in you familiar habitat (we like to call C/AL). Just first copy the dll (which is the proxy class you'll need in NAV) to your classic client and server:

  • Get the dll from yourproject\VATValidation\bin\Debug
  • Copy it to:
    • C:\Program Files (x86)\Microsoft Dynamics NAV\60\Classic\Add-ins
    • C:\Program Files (x86)\Microsoft Dynamics NAV\60\Service\Add-ins

(in my case, the server is running on my laptop, but obviously, you should put the dll wherever it will be used - NST, failover, ...). In this case, my NST will be executing the business logic, so I don't have to put it client-side.


Let's go to nav and write some very simple code there - it's not functional in any way, it's just a test-codeunit that uses the service. You can build the business logic any way you like.

First declare some variables:







'VATValidation, Version=, Culture=neutral, PublicKeyToken=null'.VATValidation.VATValidationService.checkVatService






















Then, OnRun, I wrote these lines of code:

CheckVATService := CheckVATService.checkVatService(); //the constructor

CountryCode := 'BE';
VATNumber := '0885815777';
//VATNumber := '0863844386';
//VATNumber := '0891609053';


IF Success THEN
MESSAGE(Name + '\' + Address)
MESSAGE('No Success');

I agree, the business logic is not useable at all .. but that was not the intention. I just provided you the way to use the CheckVat function .. and now it's up to you.

The Download

The download contains my test-objects from my demo-db. Take them "as is" .. so if you want to implement this at a customer, do that on your own risk - and reconsider the code (make it "partner ready" :-) ).

The download contains:

  • The RTC codeunit. You'll have to provide an action on some page to be able to run it.
  • A classic codeunit. Did't mention it above, but this is code what it would look like in the classic environment. It also works .. but who wants to write that? :-) It's just there for your reference/comparison.
  • The Proxy class. In the folder mentioned above, you'll find the dll that you'll have to deploy


Comment List