Web services allow easy communication and data exchange in a secured environment. In Microsoft Dynamics NAV 2009, you can create, publish, and consume Web services. For example, you can publish a Web service that lists all your customers and have that Web service immediately available for authorized requests over the network.

About This Walkthrough
This walkthrough provides an overview of how to create and consume a simple Web service with Microsoft Dynamics NAV. The walkthrough illustrates to the following tasks:
-
Creating a codeunit from within Microsoft Dynamics NAV.
-
Exposing the Web service.
-
Verifying the Web services availability.
-
Consuming the Web service from a console application that is created in Visual Studio.
Prerequisites
To complete this walkthrough, you will need:
-
Microsoft Dynamics NAV 2009 with a developer license.
-
Microsoft Dynamics NAV Business Web Services service running. For information on Windows services, see How to: Configure Windows Services.
-
Visual Studio 2005, Visual Studio 2008, Visual C# 2008 Express Edition, or any version of Visual Studio that supports adding Web references. You can also use service references instead of Web references or the Web service proxy generating tools (svcutil.exe or wsdl.exe) that are supplied in the Microsoft .NET Framework SDK.

Creating a Codeunit
The first step is to create a codeunit that can be published and used as a Web service. You will create a codeunit called Letters that takes a lowercase input string and returns a string that is converted to uppercase letters.
To create a codeunit
-
Open the Classic client.
-
On the Tools menu, click Object Designer.
-
In Object Designer, click Codeunit, and then click New.
-
On the View menu, click C/AL Globals.
-
In the C/AL Globals window, click the Functions tab, and then enter Capitalize as the function name.
-
In the C/AL Globals window, click the Locals button.
-
On the Parameters tab, enter inputstring in the Name field, and then select Text in the DataType field. Set the length to 250.
-
On the Return Value tab, enter outputstring in the Name field, and then select Text in the Return Type field. Set the length to 250.
-
Close the Locals window, and then close the C/AL Globals window.
-
In the Capitalize method, insert the following line of code:
outputstring := UPPERCASE(inputstring);
-
Save and close the C/AL Editor window.
-
When prompted, give the codeunit the name Letters and the ID 50000.

Exposing the Web Service
After the codeunit is created and saved, register it in the Web Services form and select the Published check box.
To expose the Web service
-
Open the Classic client.
-
On the Tools menu, click Object Designer.
-
Click Form, and then find the Web Services form with ID 810.
-
Click Run.
-
In the Web Services form, create a new entry.
-
In the Object Type field, select Codeunit, and then enter Letters as the service name and 50000 as the object ID.
-
Select the Published check box to publish the codeunit as a Web service.
-
Close the form.

Verifying the Web Services Availability
You should verify that the Web service that you published is available to consumers. Check that Web services are running, and then browse to the WSDL document to ensure that your Web service is available.
Note |
|---|
|
When exposing a Web service, you must open the port for other consumers of your Web service to access it. Have your system administrator add the port through Windows Firewall on the computer running Microsoft Dynamics NAV Server. The default port is set to 7047 and can be configured in the CustomSettings.config file on Microsoft Dynamics NAV Server. For more information, see Configuring Microsoft Dynamics NAV Server.
|
To start the Microsoft Dynamics NAV Business Web Services service
-
Click Start on the computer, point to All Programs, point to Administrative Tools, and then click Services.
-
Right-click Microsoft Dynamics NAV Business Web Services, and then click Start on the shortcut menu.
-
Close the window.
To verify availability of the specific Web service
-
Start Internet Explorer.
-
In the Address field, enter the following address where the keys are replaced with the values that are specified in the CustomSettings.config file: http://<Server>:<WebServicePort>/<ServerInstance>/WS/<CompanyName>/services. An example WSDL URL is:
http://localhost:7047/DynamicsNAV/WS/CRONUS%20International%20Ltd./services
Note |
|---|
|
The company name is optional and case-sensitive.
|
The page should list the Web service that you just published.
-
Close the browser.

Consuming the Web Service
Now that the Web service is available, you can create an application to call the Web service.
To call the Web service
-
In Visual Studio, on the File menu, point to New, and then click Project.
-
Expand the Visual C# node, and then select Console Application. Enter the name UsingLettersService for the application.
-
In Solution Explorer, right-click the References node in the project, and then click Add Web Reference.
Note |
|---|
|
If you are using Visual Studio 2008, then click Add Service Reference instead.
|
-
In the Add Web Reference window, type the URL that you used when checking the WSDL, such as http://localhost:7047/DynamicsNAV/WS/services, and then click Go.
Note |
|---|
|
In Visual Studio 2008, click the Advanced button, click the Add Web Reference button, type the URL, and then click Go.
|
-
When the Letters service is displayed, click View Service, and then click Add Reference. Rename localhost to WebService.
-
On the Program.cs tab, enter the following code:
using System;
using System.Collections.Generic;
using System.Text;
namespace UsingLettersService
{
// Import newly generated Web service proxy.
using WebService;
class Program
{
static void Main(string[] args)
{
// Create a new instance of the service.
Letters ws = new Letters();
// Use default credentials for authenticating
// against Microsoft Dynamics NAV.
ws.UseDefaultCredentials = true;
ws.Url = "http://localhost:7047/DynamicsNAV/WS/CRONUS%20International%20Ltd./Codeunit/Letters";
// Declare variables to work with.
string inputstring, outputstring;
inputstring = "microsoft dynamics nav web services!";
// Call the Microsoft Dynamics NAV codeunit Web service.
outputstring = ws.Capitalize(inputstring);
// Write output to the screen.
Console.WriteLine("Result: {0}", outputstring);
// Keep the console window open until you press ENTER.
Console.ReadLine();
}
}
}
-
Press F5 to run the application in debug mode. You should see a console window that prints the text 'MICROSOFT DYNAMICS NAV WEB SERVICES' in uppercase letters.
-
Press Enter to exit.

Next Steps
This walkthrough illustrated how you can publish a codeunit as a Web service from Microsoft Dynamics NAV and write a program that uses the Web service. The next step is to expose a page as a Web service and then interact with data from that page. For details, see Walkthrough: Registering and Consuming a Page Web Service.

See Also