Export (0) Print
Expand All

Walkthrough: Creating and Consuming a Codeunit Web Service

Microsoft Dynamics Nav 2009

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.

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.

  • 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.

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

  1. Open the Classic client and connect to the CRONUS International Ltd. demonstration company.

  2. On the Tools menu, click Object Designer.

  3. In Object Designer, click Codeunit, and then click New.

  4. On the View menu, click C/AL Globals.

  5. In the C/AL Globals window, click the Functions tab, and then enter Capitalize as the function name.

  6. In the C/AL Globals window, click the Locals button.

  7. On the Parameters tab, enter inputstring in the Name field, and then select Text in the DataType field. Set the length to 250.

  8. 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.

  9. Close the C/AL Locals window, and then close the C/AL Globals window.

  10. In the Capitalize method, insert the following line of code:

    outputstring := UPPERCASE(inputstring);
    
  11. Save and close the C/AL Editor.

  12. When prompted, give the codeunit the ID 50000 and the name Letters.

After the codeunit is created and saved, register and publish it using the RoleTailored client.

To register and publish the Web service

  1. Open the RoleTailored client and connect to the CRONUS International Ltd. demonstration company.

  2. In the navigation pane, click Departments, click Administration, click IT Administration, click General, and then click Web Services.

  3. In the Web Services page, click New.

  4. In the Object Type column select Codeunit, and then enter 50000 in the Object ID column ID and Letters in the Service Name column.

  5. Click to select the field in the Published column.

  6. Click OK to close the New - Web Services page.

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.

NoteNote

When exposing a Web service, you must open the port for other consumers of your Web service to access it. Have your system administrator open 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

  1. Click Start on the computer, point to All Programs, point to Administrative Tools, and then click Services.

  2. In the Services tool, right-click Microsoft Dynamics NAV Business Web Services, and then click Start.

  3. Close the Services tool.

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 specific Web service

  1. Start Internet Explorer.

  2. 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

    NoteNote

    The company name is optional and case-sensitive.

    The page should list the Web service that you just published (Codeunit/Letters).

  3. Close the browser.

Now that the Web service is available, you can create an application to call the Web service.

To call the Web service

  1. In Visual Studio, on the File menu, point to New, and then click Project.

  2. Expand the Visual C# node, select Windows, and then, under Visual Studio installed templates, select Console Application. Enter the name UsingLettersService for the application.

    The sample code in this walkthrough expects this exact application name, so do not change it.

  3. Click OK to close the New Project page.

  4. In Solution Explorer, right-click the References node in the project, and then click Add Service Reference.

    NoteNote

    If you are using Visual Studio 2005, then click Add Web Reference instead.

  5. 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.

    NoteNote

    In Visual Studio 2005, type or paste the URL in the Add Web Reference window, and then click Go.

  6. When the Letters service is displayed, click View Service, rename localhost to WebService, and then click Add Reference.

  7. On the Program.cs tab, replace the existing code with 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();     
            }
        }
    }
    
  8. 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.

  9. Press Enter to close the application.

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.

Community Additions

ADD
Show:
© 2014 Microsoft