Using Web Services to Access Microsoft Dynamics NAV 5.0


Dynamics NAV 2009 contains a new subsystem for dealing with Web Services. This feature has been well received by partners and customers alike. Partners have expressed interest in having web services available for earlier versions of Dynamics NAV. This feedback resulted in a technology talk at Directions2007 in Florida, where the topic was what could be done to day. The conclusion of the talk was that everything we where intending to deliver was already possible today, yes some code is needed but strictly from function/feature perspective all of it is possible, and it is not even all that ugly. Dynamics NAV 2009 will provide out-of-the-box programmatic web service access to the application and will therefore remove the need for this additional technology plumbing described here.

I have to say that the response to my talk has been tremendous. After the response to my talk on Web Services in NAV 5.0 and previous versions I decided to write this blog post and make the source files available.

This post is about how to bridge the gap between the need for web services now and the current platform, it will help you understand how you can provide Web Services directly from Dynamics NAV today, in a “simple” and flexible way, already today.

To work with the samples in this post you will need: Visual Studio 2005, Dynamics NAV 5.0 and .Net 3.0 installed on your system. This sample should work on Dynamics NAV 4.0 to but has not been tested on that version.


The system we will build contains 4 different components/moving parts: Web Service Listener, Event Dispatcher, Codeunit Eventhandler and XMLPort for stream handling.


Web Service Consumer

Any client that understands how to communicate with Web Services; like InfoPath, Visual Studio, SharePoint or any custom application written by you.


Is the physical communication port that the WCF listens to.

WCF Web Service

Defines the data contracts and service contracts for the Web Service, it also implements the concrete service and opens for listening in the WCF subsystem, it then delegates the requests to the COM Event Dispatcher component.

COM Event Dispatcher

This component provides the hookups for Dynamics NAV, both to activate the service and to register event sinks. It defines 2 IDispatch interfaces the IServiceEvents and the IWebServiceListner, as well as the concrete implementation of the IWebServiceListner in the WebServiceListner class that provides the actual code for hooking up the WCF Web Service to Dynamics NAV.


We are using the CLR runtime for writing our Web Service component and our COM plugin. Some of this blog entry is about interop between Dynamics NAV and .NET through COM.

Codeunit Event Handler

Is responsible for starting up the WCF Web Service through the COM interface, it then registered for events coming from the WCF Web Service Component. The events routed to XMLPort for processing.

XMLPorts for datastreams

It deals with the actual business logic and data coming from or going to the Web Service.


The implementation is in 2 programming languages: C# and C/AL.

Please take a look at the provided code sample, for the rest of the information contained in the posting. It can be found here:

I have included comments in the code that should explain what is going on, if you feel something is missing, first look at the documentation for the WCF or post a comment to this post and I will try to answer it.

Deployment of Sample

To deploy the sample you will first have to download it, unpack it.

Then open it up with Visual Studio and compile.

Then import the codeunit.txt and xmlport.txt into your NAV installation and compile those objects, starting with the XMLPort

To run the service simply open the Object Designer in NAV, find the Codeunit that you just imported and press run.

There is no dependency on IIS or other external components. No further deployment steps should be needed.

In the Visual studio solution is a ConsoleTestApp project. After you have followed the steps above you can run that project, it will test if your install was successful, as well as provide sample on how to use the web service.

Special considerations

In this sample I’m using XMLPort to handle the XML stream that is provided.

You can take many different approaches to this, and still reuse large please of the code provided in the sample.

To use the XMLPort as handler you will have to set the encoding property to UTF-8. This is due to a null termination bug in stream handler in NAV.



With this approach you can already today, incorporate web services in your projects in straightforward way.

The appropriate usage is whenever you need to give external application access to Dynamics NAV data or business process.

For any questions or comments please feel free to ask them in the comment section of this blog post.  I will answer questions to best of my ability on this post in the comments section as well.

One last thing:  This is a sample code.  It has not been tested, you should thoroughly test this code before usage.

Best regards,
Kris Rafnsson