Jaa


NAV 2009 - Sample C# project to consume a NAV web service

This post is a step-by-step guide, how to make a very simple C# project (3 lines of code) to integrate to NAV 2009 via a web service.

== Blog updated on 21/01 2009:

Line added in the sample code below, to avoid error “Path property must be set before calling the Send method” when making an update via the web service

==

 

For simplicity, make a new codeunit in NAV with one function, for example like this:

 

OBJECT Codeunit 78000 Test Web Service
{
  OBJECT-PROPERTIES
  {
    Date=15/08/08;
    Time=07:40:24;
    Modified=Yes;
    Version List=;
  }
  PROPERTIES
  {
    OnRun=BEGIN
          END;

  }
  CODE
  {

    PROCEDURE <AddX@1102601000(Value@1102601000> : Text[30]) : Text[30];
    BEGIN
      EXIT(Value + 'x');
    END;

    BEGIN
    END.
  }
}

 

The function just adds 'x' to whatever Text-parameter you give it, and returns the new value.

 

Publishing this codeunit as a web service is a simple as just specifying it in form 810 "Web Services", giving it a name - for example "TestWebService" and tick "Publish".

Before you continue, check that your new web service is available by going to this link in your internet browser (modify it to match your system):

https://[MachineName]:[Port]/[InstanceName]/ws/[CompanyName]/Services, for example 

https://MyMachine:7047/DynamicsNAV/ws/CRONUS_International_Ltd./Services

If you can't see your new web service here, then first check if the "Microsoft Dynamics NAV Business Web Services"-service is running. Then (re)start it and check in the application log which port it is listening to. For more details about publishing a Web Service from NAV, go to this post: "NAV 2009 - How to publish a web service". 

 

When you can see that your web service is available, then you are finished with NAV. The rest of the work happens in Visual Studio (VS). You can use either VS2005 or 2008. Follow these steps:

1)  After opening VS, select File -> New -> Project. Select a Visual C# project, and  under templates, select a "Windows Forms Application" (in VS2005 it is just called "Windows Application").

2)  This step depends a bit on whether you use VS2005 or 2008. In VS2005, just rightclick on "References" in the Solution Explorer, and select "Add Web Reference". In VS2008, to get to the same place, rightclick on "References", then select "Add Service Reference", then click the Advanced button, and then click the "Add Web Reference" button.

3)  In URL, enter the link to your web services - the same link as above, and click "Go". This will list available web services. Find your TestWebService, and click on "View Service". The Web reference name defaults to the machine name. Change that to "WS", and then click the "Add Reference" button.

4)  Now to the C# code. Open the Toolbox and add a  button and two TextBoxes to your form, then doubleclick on the button to get to the code. This is the code that you need:

WS.TestWebService NAVWebService = new WS.TestWebService();

NAVWebService.UseDefaultCredentials = true;

// Line below added 21/1 2009:

NAVWebService.Url = "https://[MachineName]:7047/DynamicsNAV/WS/CRONUS_International_Ltd./Codeunit/TestWebService";

textBox2.Text = NAVWebService.AddX(textBox1.Text);

 

This is what the first line refers to:

WS: The name you gave to your reference in step 3.

TestWebService: This is the name of the you typed into form 810 in NAV.

NAVWebService: This is the name that you assign. You could call it anything.

5)  Now run the project (F5), enter something into TextBox1, and hit the button.

 

 

Lars Lohndorf-Larsen

Microsoft Dynamics UK

Microsoft Customer Service and Support (CSS) EMEA

These postings are provided "AS IS" with no warranties and confer no rights. You assume all risk for your use.

Comments

  • Anonymous
    August 14, 2008
    PingBack from http://housesfunnywallpaper.cn/?p=891

  • Anonymous
    October 28, 2008
    My first experience of using web services in the Marketing Release (CTP4) for Dynamics NAV 2009 resulted