Connecting to NAV Web Services from C# using Web Reference


Please read this post to get a brief explanation of the scenario I will implement in C# using Web References.

For C# we can leave the Service Tier running Negotiate or we can use Ntlm as PHP and Java. In this example I will assume that the Service Tier is running SPNEGO (which is the default)

BTW. Basic knowledge about C# is required to understand the following post:-)

Version and download

I am using Visual Studio 2008 professional with SP1 when writing this sample, to be honest I have NOT tried to see whether this will work in the Express versions of Visual Studio, but they do have Service Reference and Web Reference so I cannot see why not.

What is the difference between a Web Reference and a Service Reference?

In short, the Web Reference is a .net 2.0 compatible Web Service reference, the Service Reference is a .net 3.5 WCF based Service Reference.

Add Web Reference is a wrapper over wsdl.exe and can be used to create proxies for .NET 1.1 or 2.0 clients. Of course this means when you are pointing to a WCF service you have to be pointing to an endpoint that uses basicHttpBinding.

Add Service Reference is a wrapper over svcutil.exe and also creates clients proxies. These proxies, however, can only be consumed by .NET 3.5 clients.

How to add Web References

Select Add Service Reference 


Click Advanced 


Click Add Web Reference


Type in the URL and specify the namespace for the SystemService Web Reference


and for the Customer Page Web Reference


When adding a Web Reference, Visual Studio will create a config file in which it stores stuff like the URL for the Reference. In my samples I will set the URL in code and due to this, the config file is not needed.


In the following sample I use Windows Authentication. In Web References you just need to set the property UseDefaultCredentials in the service class to true, then .net will automatically try to use your windows credentials to connect to the Web Service.

If you want to connect to a Web Reference using a specific username/password you need to exchange this line:

someService.UseDefaultCredentials = true;

with this:

someService.Credentials = new NetworkCredential("user", "password", "domain");

The code

First a couple of using statements (including the two reference namespaces) and the main body of a console app:

using System;
using System.Net;
using testApp.CustomerPageRef;
using testApp.SystemServiceRef;

namespace testApp
    class Program
        static void Main(string[] args)
            // main program code

The main code follows

First, connect to the System Web Service and list all companies:

string baseURL = "http://localhost:7047/DynamicsNAV/WS/";

SystemService systemService = new SystemService();
systemService.Url = baseURL + "SystemService";
systemService.UseDefaultCredentials = true;

string[] companies = systemService.Companies();
foreach (string company in companies)
string cur = companies[0];

Now I have the company I want to use in cur and the way I create a URL to the Customer page is by doing:

string customerPageURL = baseURL + Uri.EscapeDataString(cur) + "/Page/Customer";
Console.WriteLine("\nURL of Customer Page: " + customerPageURL);

and then I can create a Service Class to the Customer Page:

Customer_Service customerService = new Customer_Service();
customerService.Url = customerPageURL;
customerService.UseDefaultCredentials = true;

and using this, I read customer 10000 and output the name:

Customer cust10000 = customerService.Read("10000");
Console.WriteLine("\nName of Customer 10000: " + cust10000.Name);

Last, but not least – lets create a filter and read all customers in GB that has Location Code set to RED or BLUE:

Customer_Filter filter1 = new Customer_Filter();
filter1.Field = Customer_Fields.Country_Region_Code;
filter1.Criteria = "GB";

Customer_Filter filter2 = new Customer_Filter();
filter2.Field = Customer_Fields.Location_Code;
filter2.Criteria = "RED|BLUE";

Console.WriteLine("\nCustomers in GB served by RED or BLUE warehouse:");
Customer_Filter[] filters = new Customer_Filter[] { filter1, filter2 };
Customer[] customers = customerService.ReadMultiple(filters, null, 0);
foreach (Customer customer in customers)

Console.WriteLine("\nTHE END");

All of the above will output the following to a console prompt (on my machine running NAV 2009SP1 W1)


I hope this is helpful.

Good luck

Freddy Kristiansen
PM Architect
Microsoft Dynamics NAV

Comments (12)
  1. Sven says:

    Hello Freddy,

    thank you for your inspiration. It si a very nice Blog. :- )

    but i cannot find a solution for my problem :- (

    i have a codeunit published (per Dynamics NAV 2009 R2 webservice) and i use this in a c# project.

    it works fine but i cannot change the language for a call.

    i dont want to change the language in C/Side this is to late.

    Can you help me, please? What can i do that the language is for example france?

    Best regards Sven

  2. hello, this post is verygood in my local machine says:

    but when i use the customer server's database, when i add the web reference, the navision webservice return error:

    You must choose a company before you can use the Web Service table

    can you help me for  the error?

    good luck!

  3. Martin says:

    Hello Freddy

    Is it posible to make a C# code exampel that calls a web service with some parameters to insert a record in Nav?

    Best regards


    1. Pritam says:

      Hello. I am looking for the same thing.
      Are you able to create customer with C#?

      1. FreddyDK says:

        This is a very old sample for NAV 2009. If you are running newer versions of NAV it is easier and a lot of info on other blogs (Vjeko and mibuso)

  4. Pooria says:

    Hello, thank you man

  5. Peter says:

    Is there a chance for multiple access to the proxy (i am using the readmultipe method with set size 1000 or more)?

    Best regards


  6. Peter says:

    Is there a chance for multiple access to the proxy (i am using the readmultipe method with set size 1000 or more)?

    Best regards


  7. Emmanuel says:

    How would you change the sort key for the customers set?

  8. polis says:

    What to do when the webservices isnt responding ? is there a way to check is it respongid and if not then restarting the webservice ?

  9. carl kassar says:

    Hi ,

    thank you for the post. But I'm getting an error 401 unauthorized , when executing the code. Any idea why ?

  10. Harik Mehta says:

    Hello freedy ,
    nice artiale 🙂

Comments are closed.

Skip to main content