Connecting to NAV Web Services from C# using Web Reference

Posted on Posted in .Net

Connecting to NAV Web Services from C# using Web Reference

Microsoft introduce Web Services start in Dynamics NAV 2009 version through codeunit and pages. When a page is exposed as a web service it can be accessed from various other programming languages/platforms as below

  • PHP
  • Java
  • C# using Web Reference
  • C# using Service Reference
  • JavaScript
  • Visual Basic using Web Reference
  • Visual Basic using Service Reference
  • Windows Mobile
  • and more…

In this article I am going to explain how to connect from C# using web Reference.

Prerequisites

  • Visual Studio
  • Nav Web Service URL
  • Basic Knowledge about C#
  • Web Services

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 creates clients proxies. These proxies, however, can only be consumed by .NET 3.5 clients.

Scenario

  • Connects to NAV Web Services System Service and enumerates the companies in the database
  • Constructs a URL to the Customer Page (based on the first company in the list)
  • Get the name of Customer number 10000
  • Get the Customers in GB that have Location Code set to RED or BLUE.

How to add Web References

Select Add Service Reference

image

Click Advanced

image

Click Add Web Reference

image

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

image

and for the Customer Page Web Reference

image

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.

Authentication

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.

Connect Web Reference using Windows Authentication:

someService.UseDefaultCredentials = true;

Connect Web Reference using a specific username/password:

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;

Console.WriteLine(“Companies:”);

string[] companies = systemService.Companies();

foreach (string company in companies)

Console.WriteLine(company);

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(customer.Name);

Console.WriteLine(“\nTHE END”);

Console.ReadLine();

All of the above will output the following to a console prompt

image

 

Leave a Reply

Your email address will not be published. Required fields are marked *