This walkthrough shows how you can publish and consume a Microsoft Dynamics NAV 2009 page as a Web service and how you can extend the default set of operations that are available on a page with codeunit functionality. Adding an extension codeunit to a page is useful if you want to perform operations other than create, read, update, or delete.
About This Walkthrough
This walkthrough illustrates the following tasks:
-
Registering and publishing a page as a Web service.
-
Creating an extension codeunit.
-
Connecting the extension codeunit to the published page.
-
Verifying the service's availability.
-
Calling the Web service from a console application created in Visual Studio.
Prerequisites
To complete this walkthrough, you need:
-
Microsoft Dynamics NAV 2009 with a developer license.
-
The CRONUS International Ltd. demo data company.
-
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.
Registering and Publishing a Page as a Web Service
You register and publish a Web service using the RoleTailored client.
To register and publish a page as a Web service
-
Open the RoleTailored client and connect to the CRONUS International Ltd. demonstration company.
-
In the navigation pane, click Departments, click Administration, click IT Administration, click General, and then click Web Services.
-
In the Web Services page, click New.
-
In the Object Type column, select Page, and then enter 21 in the Object ID column ID and PageWithCapitalization in the Service Name column.
-
Click to select the field in the Published column.
-
Click OK to close the New - Web Services page.
Do not close the RoleTailored client or navigate away from the Web Services page.
Creating an Extension Codeunit
After you publish the page, you create a codeunit that extends its functionality. Follow the steps in this procedure to create a codeunit called CapitalizeName, which takes a customer name as input and outputs the same customer name capitalized. The codeunit must take the record type of the page as the first parameter.
To create an extension codeunit
-
Open the Classic client and connect to the CRONUS International Ltd. demonstration company.
-
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 CapitalizeName as the name of the function.
-
From the C/AL Globals window, click the Locals button.
-
On the Parameters tab, enter Cust in the Name field, and then select Record in the DataType field.
-
In the SubType field, click the arrow to open the Table list window and select Customer.
-
Click OK to close the Table list window.
-
Close the C/AL Locals window, and then close the C/AL Globals window.
-
In the
CapitalizeName
method, insert the following lines of code:Copy Code Cust.Name := UPPERCASE(Cust.Name); Cust.MODIFY(TRUE);
-
Close the C/AL Editor.
-
When prompted, give the codeunit the ID 50012 and the name CapitalizeName.
Connecting the Codeunit to the Exposed Page
After the codeunit is created and saved, register it under the published page so that the codeunit extends the published page. The codeunit itself should not be marked as Published.
To connect the codeunit to the exposed page
-
Switch back to the RoleTailored client and the Web Services page.
-
Click New.
-
In the Object Type field, select Codeunit, and then select object ID 50012. In the Service Name field, enter PageWithCapitalization, which is the same name that you gave the page Web service.
-
Clear the Published check box.
-
Click OK to close the New - Web Services page.
Verifying Web Services Availability
You should verify that the Web service is available to consumers. Start the Microsoft Dynamics NAV Business Web Services service, 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.
If the service is already running, then you must restart it. For information on starting, stopping, and configuring Windows services, see How to: Configure Windows Services.
To verify availability of the 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 services that you have published:
Page/PageWithCapitalization
andCodeunit/PageWithCapitalization
. -
Close the browser.
Calling the Web Services
Now you can create an application that uses both the page and the codeunit Web services. When you add a reference in Visual Studio, methods from both the page and the extension codeunit are available.
To call the Web services
-
In Visual Studio, on the File menu, point to New, and then click Project.
-
Expand the Visual C# node, select Windows, and then, under Visual Studio installed templates, select Console Application. Enter the name UsingExtensionCodeunit for the application.
The sample code in this walkthrough expects this exact application name, so do not change it.
-
Click OK to close the New Project page.
-
In Solution Explorer, right-click the References node in the project, and then click Add Service Reference.
Note If you are using Visual Studio 2005, then click Add Web Reference instead.
-
In Visual Studio 2008, click the Advanced button, click the Add Web Reference button, type or paste 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 2005, type or paste the URL in the Add Web Reference window, and then click Go.
-
When the services are displayed, select the Page service (
Page/PageWithCapitalization
). Do not select the Codeunit service (Codeunit/PageWithCapitalization
). Then click View Service. -
Rename localhost to WebService, and then click Add Reference.
-
On the Program.cs tab , replace the existing code with the following code:
C# Copy Code using System; using System.Collections.Generic; using System.Text; namespace UsingExtensionCodeunit { using WebService; class Program { static void Main(string[] args) { // Create instance of service and set credentials. PageWithCapitalization_Service service = new PageWithCapitalization_Service(); service.UseDefaultCredentials = true; // Create instance of customer. PageWithCapitalization cust = new PageWithCapitalization(); cust.Name = "Louis Taylor"; // Insert customer using the default operation. service.Create(ref cust); Console.WriteLine(cust.Name); // Call extension codeunit to capitalize customer name. service.CapitalizeName(cust.Key); // Refresh customer. cust = service.Read(cust.No); Console.WriteLine(cust.Name); Console.ReadLine(); service.Delete(cust.Key); } } }
-
Press F5 to run the application in debug mode. You should now see a console window that creates a new customer using a default page operation and then capitalizes the customer name using the extension codeunit.
See Also
© 2010 Microsoft Corporation. All rights reserved.