Web Service

– Creating and Consuming a Codeunit Web Service

Posted on Posted in Uncategorised

Web services allow easy communication and data exchange in a secured environment. In Microsoft Dynamics NAV 2009 or above , 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.

About This Walkthrough

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 c#.

Pre-requisites

To complete this walkthrough, you will need:

  • Microsoft Dynamics NAV 2009 or above with a developer license.
  • Visual Studio 2005 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.

C:\Users\ARUN\AppData\Local\Microsoft\Windows\INetCacheContent.Word\IC616529.png

Creating a Codeunit

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:
  11. outputstring := UPPERCASE(inputstring);
  12. Save and close the C/AL Editor.
  13. When prompted, give the codeunit the ID 50000 and the name Letters.

Registering the Web Service

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.

Verifying the Web Services Availability

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.

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.

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

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

  1. Close the browser.

Consuming the Web Service

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.

  1. In Visual Studio, on theFile menu, point to New, and then click Project.
  2. Expand theVisual 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. ClickOK to close the New Project
  4. In Solution Explorer, right-click theReferences node in the project, and then click Add Service Reference.
  5. In Visual Studio 2008 or above, click theAdvanced 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.
  6. When theLetters 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:

 

C#

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

 

Leave a Reply

Your email address will not be published. Required fields are marked *