Dynamics NAV Generic SOAP Client

Last summer, when I posted my first article I though blogging is very easy and I’ll post an article every week. Actually, it’s not that easy. Anyway, after few months here is the article I promised: a 100% generic web service client for NAV.

I was looking for a way to automate all my web service calls without managing, deploying and struggling with all Dlls generated by Visual Studio. I was hoping to use CU 1290 “SOAP Web Service Request Mgt.” like described here. I quickly noticed that it is not working if the web service comes from another NAV Database (maybe, due to SOAP version used natively by NAV). So, I started looking around how Visual Studio generates Dlls I was used to use. I found the answer in this wonderful article from msdn.

This introduction begins to be longer than expected. So, let’s get into the bare bones of the topic.

As you guessed, I tried to translate the msdn article using .NET interoperability in NAV. The result was astonishing. I let you admire the similarities:

pure-c

Pure C#

pure-cal

Pure C/AL

The C/AL code remembers me this great article. My first reaction was: What the he**, I’m writing code like the great Vjekoslav Babić.

Now, let me explain how it works. The CU uses four functions:

  1. InitClient: compiles the WSDL on the fly and prepares a ready to use client (proxy, dll… or whatever you want to call it) in memory.
  2. InitCallMethod: prepares the method to call with its parameters.
  3. CallMethod: invokes the method and retrieves the web service result.
  4. GetObjectValue: use the web service result in whatever way you want (result may be simple Text,Decimal… or complex Customer Record…).

Here is an example:

how-to-use

and this is the result:

example1

Enough talking, you can download the code here gws-with-examples.

Some readers may say:

But this needs to compile the client every time; this is huge for the memory.

This is absolutely true. The answer will be the subject of one of my next posts.

This blog is available also on Microsoft Dynamics NAV Community
Advertisements

8 thoughts on “Dynamics NAV Generic SOAP Client

  1. I’d love to try that, but I can’t import the fob into 2009 R2. It would be great if you could provide the objects in text format!

    Like

  2. Hi Dr. Zappergeck,

    The code is for NAV 2016 and later. I did not test it in old versions. It could work for NAV 2015, 2013 R2 and 2013. But I will be surprised if it works for NAV 2009 R2 RTC. For NAV 2009 R2 Classic Client it won’t work for sure.

    I can upload the text format and let you try to downgrade it ;).

    Like

    • Thank you very much! As you expected, it will not work in the classic client, as .Net variables only work in RTC. But it will be usful for later versions.

      Like

  3. Thank you, a very interesting post, but I am getting an error :

    A call to System.Web.Services.Description.ServiceDescription.Read failed with this message: Root element is missing.

    This kind of error happens in C# when the XML file is not correct, but in our case, the XML file is generated by Nav is supposed to be correct, so why I am getting this error, anyone has an idea, please.

    Thank you

    Like

    • Hello Abdelatif,

      When you say it “is generated by NAV and it is supposed to be correct”, you’re using an XMLport? XMLports usually add an XML tag to your file structure. Maybe you should read your XML file in a string and check what is wrong with it?

      Like

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 )

Google+ photo

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

Twitter picture

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

Facebook photo

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

w

Connecting to %s