Export (0) Print
Expand All

Walkthrough: Registering and Consuming a Page Web Service

With Microsoft Dynamics NAV 2009, you can create, publish, and consume Web services. This walkthrough focuses on registering and consuming a page as a Web service. When a page is published as a Web service, a default set of operations is exposed to developers for managing common record-handling tasks. The walkthrough shows how to use some of these operations.

This walkthrough provides an overview of how to expose a page as a Web service and how to manipulate data from that page. The walkthrough illustrates the following tasks:

  • Registering and publishing an existing page as a Web service.

  • Verifying the service's availability using the WSDL.

  • Consuming the Web service from a console application that is created in Visual Studio.

  • Making calls to the Create, Read, Update, and Delete functions on a Page Web service.

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.

You register and publish a Web service using the RoleTailored client.

To register and publish a page as a 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 Page, and then enter 21 in the Object ID column and Customer in the Service Name column.

  5. Click to insert a check mark in the Published column.

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

You should verify that the Web service is available to consumers. Start the Microsoft Dynamics NAV Web services service, 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 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 the Microsoft Dynamics NAV Server computer. 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 running Microsoft Dynamics NAV Server, point to All Programs, point to Administrative Tools, and then click Services.

  2. Right-click Microsoft Dynamics NAV Business Web Services, and then click Start on the shortcut menu.

  3. Close the window.

If the service is already running, 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

  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 have published: Page/Customer.

  3. Close the browser.

Now that the Web service is exposed, use Visual Studio to create an application that calls the Web service. This procedure assumes that you are using Visual Studio 2008 but provides alternate steps for Visual Studio 2005.

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 PrintCustomerList for the application.

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

  3. Click OK to exit the New Project page.

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

    NoteNote

    If you are running 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 Customer 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 PrintCustomerList
    {
        // Import newly generated Web service proxy.
        using WebService; 
        
        class Program
        {
            static void Main(string[] args)
            {
                // Create instance of service and set credentials.
                Customer_Service service = new Customer_Service();
                service.UseDefaultCredentials = true;
                // Create instance of customer.
                Customer cust = new Customer();
                cust.Name = "Customer Name";
                Msg("Pre Create");
                PrintCustomer(cust);
                
                // Insert customer.
                service.Create(ref cust);
                Msg("Post Create");
                PrintCustomer(cust);
    
                // Create filter for searching for customers.
                List<Customer_Filter> filterArray = new List<Customer_Filter>();
                Customer_Filter nameFilter = new Customer_Filter();
                nameFilter.Field = Customer_Fields.Name;
                nameFilter.Criteria = "C*";
                filterArray.Add(nameFilter);
    
                Msg("List before modification");
                PrintCustomerList(service, filterArray);
    
                cust.Name = cust.Name + "Updated";
                service.Update(ref cust);
    
                Msg("Post Update");
                PrintCustomer(cust);
    
                Msg("List after modification");
                PrintCustomerList(service, filterArray);
                service.Delete(cust.Key);
    
                Msg("List after deletion");
                PrintCustomerList(service, filterArray);
    
                Msg("Press [ENTER] to exit program!");
                Console.ReadLine();
            }
    
            // Print list of filtered customers.
            static void PrintCustomerList(Customer_Service service, List<Customer_Filter> filter)
            {
                Msg("Printing Customer List");
                
                // Run the actual search.
                Customer[] list = service.ReadMultiple(filter.ToArray(), null, 100);
                foreach (Customer c in list)
                {
                    PrintCustomer(c);
                }
                Msg("End of List");
            }
    
            static void PrintCustomer(Customer c)
            {
                Console.WriteLine("No: {0} Name: {1}", c.No,c.Name);
            }
    
            static void Msg(string msg)
            {
                Console.WriteLine(msg);
            }
        }
    }
    
  8. Press F5 to run the application in debug mode. You should now see a console window that prints a list of customers from the CRONUS International Ltd. demo company. Note how one customer in the list is created, then modified, and finally deleted.

Community Additions

ADD
Show:
© 2015 Microsoft