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

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

  • Comments 2

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):

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

http://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 = "http://[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 (Lohndorf )

Microsoft Dynamics UK


Microsoft Customer Service and Support (CSS) EMEA

Leave a Comment
  • Please add 2 and 1 and type the answer here:
  • Post
  • Hi,

    I did it and I got the error message "Callback functions are not allowed."

    Why? Your example is so simple, just input a word and it give you wordx I dont understand why I am getting "Callback functions are not allowed."

  • Hello,

    "Callback functions are not allowed" typically comes if you try to run any kind of UI via Web Services. So look in the code for any places running CONFIRM, trying to run a Form or page, or any other things that require user intervention which of course the web service can't do. I hope this can explain it?

    Lars

Page 1 of 1 (2 items)