Compartir a través de


Walkthrough: Creating and Interacting with a Page Web Service (OData)

This walkthrough describes how to register and publish a page as an OData web service. For more information about how to interact with page data with an SOAP web service, Walkthrough: Registering and Using a Page Web Service (SOAP) and Web Service Alternatives: SOAP and OData.

About This Walkthrough

This walkthrough provides an overview of how to expose a page as a web service and how to use the web service in a C# console application. The walkthrough illustrates the following tasks:

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

  • Verifying web service availability from a browser.

  • Using the published web service that uses a C# console application that you create in Visual Studio 2010.

Prerequisites

To complete this walkthrough, you will need:

  • Microsoft Dynamics NAV 2013 with a developer license.

  • Visual Studio 2010 or Visual Studio 2008. You can use any edition of Visual Studio that supports adding web references. In this walkthrough, you will use Visual Studio 2010. You also have the option to use service references instead of web references, or use the web service proxy generating tools svcutil.exe and wsdl.exe, which are included in the Microsoft .NET Framework SDK.

Publishing a Page as a Web Service

You publish a web service by 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 Search box, enter Web services, and then choose the related link.

    The Web Services page opens.

  3. In the Web Services page, on the Home tab, choose New.

  4. In the Object Type column, select Page. Enter 21 in the Object ID column and then enter Customer in the Service Name column.

  5. Select the check box in the Published column.

  6. Choose the OK button to exit the New - Web Services page.

Verifying Web Service Availability

Note

After publishing a web service, verify that the port that web service applications will use to connect to your web service is open. The default port for OData web services is 7048. You can configure this value by using the Microsoft Dynamics NAV Server Administration Tool.

To verify availability of a Microsoft Dynamics NAV web service

  1. Start Internet Explorer.

  2. In the Address field, enter a URI in this format: http://<Server>:<WebServicePort>/<ServerInstance>/OData. For example:

    https://localhost:7048/DynamicsNAV70/OData

    The browser should now show the web service that you have published, in the format of an AtomPub document.

    Basic AtomPub document for a page

Calling the Web Service

In this procedure, we use Visual Studio 2010 to call and use the web service.

To call the web service

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

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

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

  3. Choose the OK button to exit the New Project page.

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

  5. In Visual Studio, type or paste the same URI that you used in your browser to check the availability of the OData service, for example, https://localhost:7048/DynamicsNAV70/OData.

    You should see the notification 1 service(s) found at address ‘https://localhost:7048/DynamicsNAV70/OData.

  6. In the Namespace field, type NAVWS. Choose the OK button.

  7. On the Program.cs tab, replace the existing code with the following code.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace OdataCustomerList
    {
        class Program
        {
            static void Main(string[] args)
            {
                NAVWS.NAV nav = new NAVWS.NAV(new Uri("https://localhost:7048/DynamicsNAV70/OData/"));
                nav.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
                var customers = from c in nav.Customer
                                where c.Location_Code == "YELLOW"
                                select c;
                foreach (var c in customers)
                {
                    Console.WriteLine(c.Name);
                }
            }
        }
    }
    
  8. On the Build menu, select Build Solution to build your project and then, on the Debug menu, choose Start Debugging 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. demonstration company.

  9. Press Enter to dismiss the application.

See Also

Concepts

Overview of Microsoft Dynamics NAV Web Services
Web Service Walkthroughs