Pages

Sunday, June 28, 2015

Register and Consume Codeunit as a Web Service in NAV 2013 R2

Hi Guys,



Today we will be learning :

1. How to Register a Web Service in NAV 2013 R2.
2. How to Consume it using Visual Studio.

Step 1. Register a Web Service in NAV 2013 R2


To Register a Web Service we need to open Web Services in RTC.
Department > Administration > IT Administration > General > Web Services


Step 1.1 - To Register a new Codeunit web service we need to create a Codeunit first (and then we will register it). For this article, I will Create a Codeunit which will Sum 2 NumbersI am taking a very basic program of Sum to start with, so that all of us developers are able to do this exercise easily and then go-to more advanced programs & services.
  • Open the Development Environment.
  • Create a new Codeunit.
  • Open C/AL Globals and Under the Functions Tab, create a new function with the Name 'Sum' .
  • Open C/AL Locals of 'Sum'. Define Locals as shown in below screenshots,

  • Write code in the function just defined as shown below,
  • Save the Codeunit and exit the development environment.
Step 1.2 - Open Web services in RTC and click New.
  • Enter Codeunit in Object Type.
  • Select the Object ID on which you saved the Codeunit.
  • Enter a Service Name (Mandatory Field).
  • Mark the Published field to True.
  • As soon as Published field becomes True, the OData URL & SOAP URL are filled automatically. (OData field will show 'Not applicable' as a Codeunit can not be consumed with OData web services.)

You have Registered the Web Service Successfully

You can test your Web Service,
- By clicking small globe icon which appears on the right side as soon as you click on SOAP URL field.
OR
- Copy and paste the SOAP URL in any of your Internet Browser.

Step 2. Consume a Web Service in NAV 2013 R2


Step 2.1 - Open Microsoft Visual Studio and Create a new project. 
  • Click File > New > Project.
  • New Project Window opens. 
  • Expand Installed node > Expand Templates node > Expand Visual C# node > Click Windows > Select Console Application.
  • Enter Name of the project as Numbers.
  • Click OK.

VS will open a new window having name Program.cs as shown below,


Here we will write the C# code to consume the Codeunit Web Service created in Step 1.
But first we need to add reference of our web service to our Project in VS.

Step 2.2 - Add Service Reference to our project in VS.
  • In the Solution Explorer which on the right hand side in VS. Right-Click on the Project you created and select Add Service Reference option.
  • Add Service Reference window opens. Click on Advanced...
  • Service Reference Settings window opens. Click Add Web Reference...
  • Add Web Reference window opens. Type the URL same as this one "http://localhost:7047/DynamicsNAV71/WS/Codeunit/Numbers"
And if you are working with a Remote Server, Replace localhost with that Server's IP Address and rest of the URL remains same.

  • Press Enter OR click on the arrow image placed to the right of URL Text box.
  • Enter the Web reference name such as 'NumbersWS'.
  • Click Add Reference.

And the web service will be added to your Project.


Step 2.3 - Add the following code to Program.cs

//---------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Numbers
{
    // Import the newly created Web Service
    using NumberWS;

    class Program
    {
        static void Main(string[] args)
        {
            // Create an instance of the Web Service
            Numbers ws = new Numbers();
            
            // Use Default credentials for authentication to Microsoft Dynamics NAV
            ws.UseDefaultCredentials = true;

            // Declare Variables
            decimal a, b, result;
            
            a = 15;

            b = 17;
            
            // Call the Codeunit web service
            result = ws.Sum(a,b);

            // Print the Output
            Console.WriteLine("Result: {0}", result);

            // Keep the console open until we press ENTER
            Console.ReadLine();
        }
    }
}
//---------------------------------------------------------------------------------
  • Once you have added the code, Press F5. And see the output which will print the sum of the two numbers.

  • Press ENTER to close the console.
And now you're ready to create Codeunit web services in Microsoft Dynamics NAV 2013 R2. Try other examples such as UPPERCASE, LOWERCASE etc. C/AL functions and create web services to consume these.



Cheers!!!
Ishwar
Post your comments and doubts I will be happy to answer them.

2 comments:

  1. Thanks! All examples are simple and very beautifully explained, even someone who does not know much about Web services can get the hang of them.
    web designing company Pakistan

    ReplyDelete
    Replies
    1. Hi +Tooba Waqar Thank you very much for your comment. And you're welcome! :)

      Delete