NAV 2009 Web Services (Part 4 – consuming the web service using C#)

So I'm sure everyone has already read the blogs previously posted by freddyk and waldo on the subject of consuming web services exposed by NAV, but as promised here is the continuation of my journey.

I'm going to create a very simple C# application to read out the list of Vehicles using my vehicle page.

So I start by creating a new project – a Windows Forms Application called 'NAVFuelApp'.

At this point I should be presented with a very simple blank new Form – Form1

 

The next step is to tell the application about my web service. To do this I right-click on the References folder, and select to add a new Web reference

 

In the URL field type in the URL of the web service of the page we exposed. In my case this is:

http://localhost:7047/MicrosoftDynamicsNAVServer/WS/Page/WSVehicle

and I call the Web reference – Vehicle.

 

You may note at this point that there are a few default methods which we 'get for free' when we expose a page as a web service. These are very useful indeed and it is in fact the 'ReadMultiple' method which we will be using to get the list of records we are after.

After clicking 'Add Reference' my application should now have the details required to connect to the web service.

The next step is to add a few controls to my .NET form. In this example I simply want a control which will display a list of records, as well as a button which will actually call the web service to populate the list. For this purpose I add a DataGridView and a Button. In the case of the button I have changed the text to read 'Get Data'.

 

Next we get to some more exciting stuff – writing the function which I will call from the button which should invoke the web service and return my results.

I create a new function called 'LoadVehicles' which has the following code.

private void LoadVehicles()

{

Vehicle.WSVehicle_Service MyVehicles = new Vehicle.WSVehicle_Service();

MyVehicles.UseDefaultCredentials = true;

 

Vehicle.WSVehicle[] Vehicles = MyVehicles.ReadMultiple(null, null, 0);

 

dataGridView1.DataSource = Vehicles;

dataGridView1.Columns[0].Visible = false;

dataGridView1.Columns[4].Visible = false;

dataGridView1.Columns[ 6].Visible = false;

}

The first couple of lines of code instantiate MyVehicles which is a reference to the web service we have exposed, and define which credentials to use.

The third line is the call to the 'ReadMultiple' method of the web service (described above), and this is the line which actually makes the call to the web service.

Line 4 assigns the results returned from the web service as the datasource for the DataGridView.

By default all fields from the page are displayed, therefore lines 5-7 deal with hiding some unwanted columns.

 

The final task for the purposes of this example is simply to call the new function (LoadVehicles) when the 'Get Data' button is pressed. Simply double click the button itself on the form and then add the call to the new function.

private void button1_Click(object sender, EventArgs e)

{

LoadVehicles();

}

So finally there you have it – a very simple C# application which will read data from NAV via a web service.

 

Comment List
Related
Recommended