Microsoft Dynamics NAV - Creating and Consuming a Codeunit Web Service

We can create any code unit in Dynamics NAV and publish it as web-service. To do that we need to go to the page (form in 2009) "Web Services". 
  • In the Web Services page, choose New.
  • In the Object Type column, select Codeunit, then in the Object ID column enter the Codeunit Number, and then enter a name in the Service Name column. This name will be the web-service name to consume. 
  • Mark the check box in the Published column and choose OK to close the New - Web Services page.

Webservice Form (2009)

Verifying the Web services availability. Make sure flowing services are running in server.


  • In Browser write the service URL to check if the service published successfully, Change the localhost with ip for remote server : http://localhost:7047/DynamicsNAV/WS/Services 
  • If everything is ok you should see the published services names in the browser.

Now lets try to consume the service. In this example we will see how to consume the service from a Dot Net application.

  • Consuming the Web service from a .NET application that is created in Visual Studio. Right click on Reference and select Add Service Reference

  • In the add service reference window Copy URL to in the address textbox and press Go button. The existing service should show in the preview box. Give a NameSpace and press Ok.

  • To connect to the service need to write flowing code :
     SCMReqDelayMailService_PortClient MS = new SCMReqDelayMailService_PortClient();  
     var binding = new BasicHttpBinding();  
     binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;  
     binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;  
     MS = new SCMReqDelayMailService_PortClient(binding,  
             New EndpointAddress( "http://172.16.8.254:7047/DynamicsNAV/WS/ICDDR,B/Codeunit/SCMReqDelayMailService"));  
     MS.ClientCredentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Delegation;  
    

  •  After that for this example MS object is connected to the codeunit of NAV. We can access the functions of the codeunit throw MS object as flowing:
Showing the Object is getting properties and function of the Codeunit from NAV published as web-service  

Comments

Popular posts from this blog

Different use of AL Find Functions for Filters in Dynamics 365 Business Central

How to create documentation comment using Custom Snippets in VS Code for AL - D365 Business Central.