Pages

Sunday 15 May 2016

Navision 2016: Using a Codeunit Web Service (SOAP) for Customer details

Creating and Using a Codeunit Web Service (SOAP)
Web services provide easy communication and data exchange in a secured environment. In Microsoft Dynamics NAV 2016, you can create, publish, and use web services. For example, you can publish a web service that return customer’s details in response.

Enable SOAP Web service
















Creating a codeunit in Microsoft Dynamics NAV.

















Publish Web Service
1.     Open the RoleTailored client, and then connect to the CRONUS International Ltd. company.
2.     In the Search box, enter Web Services, and then press Return.
3.     In the Web Services page, choose New.
4.     In the Object Type column, select Codeunit, then in the Object ID column enter 50000, and then enter Letters in the Service Namecolumn.
5.     Mark the check box in the Published column and choose OK to close the New - Web Services page.









Using the web service

1.     In Visual Studio, on the File menu, select New, and then choose Project.
2.     Under Installed Templates, expand the Visual C# node, select Windows, and then, select Console Application.
3.     Enter the name UsingLettersService for the application and choose OK to close the New Project page.The sample code in this walkthrough expects this exact application name, so do not change it.
4.     In Solution Explorer, right-click the References node in the project, and then choose Add Service Reference.
5.     In the Add Service Reference window, choose the Advanced button.
6.     In the Service Reference Settings window, choose the Add Web Reference button, type or paste the URL that you used when you checked the WSDL, such as http://localhost:9047/DynamicsNAV90/WS/Services, and then choose Go (the green button with the arrow).



7.     When the Letters service is displayed on the discovery Page, choose View Service, then in the Web reference name text box, renamelocalhost to WebService, and then choose Add Reference.
8.     In Solution Explorer, choose Program.cs and replace the existing code with the following code:

using System;
using System.Collections.Generic;
using System.Text;
namespace UsingLettersService
{
    // Imports newly generated web service proxy.
    using WebService;
    class Program
    {
        static void Main(string[] args)
        {
            // Creates a new instance of the service.
            Letters ws = new Letters();          
           
            // Uses default credentials for authenticating
            // with Microsoft Dynamics NAV.
            ws.UseDefaultCredentials = true;
            ws.Url = "http://localhost:9047/DynamicsNAV90/WS/CRONUS%20International%20Ltd./Codeunit/Letters";   
            // Declares variables to work with.
            string inputstring, outputstring;  
            inputstring = "20309920";

            // Calls the Microsoft Dynamics NAV codeunit web service.
            Outputstring = ws.CustomerDetails(inputstring);
            // Writes output to the screen.
            Console.WriteLine("Result: {0}", outputstring); 
            // Keeps the console window open until you press ENTER.
            Console.ReadLine();    
        }
    }
}

On the Build menu, select Build Solution to build your project and then, from the Debug menu, choose Start Debugging to run the application in debug mode.

Output:
Name      Email Id