14
Programming with NAV Web Services – The Basics Bill Burton Solution Systems, Inc. 847-590-3000 [email protected]

Programming With NAV Web Services – The Basics

Embed Size (px)

DESCRIPTION

These are notes and references used when consulting and training on how to use Microsoft Dynamics NAV Web Services to accomplish various tasks.

Citation preview

Page 1: Programming With NAV Web Services – The Basics

Programming with NAV Web Services – The BasicsBill Burton

Solution Systems, Inc.

[email protected]

Page 2: Programming With NAV Web Services – The Basics

These are my notes and references that I use when consulting and training on how to use Microsoft Dynamics NAV Web Services to accomplish various tasks.

The BasicsThere are a handful of steps that are the same regardless of what task you are attempting.

1. Identify the Page2. Publish the Web Service3. Test the URL in a Web Browser4. Add a Service Reference to Your Project5. Write Some Code.

[email protected]

Page 3: Programming With NAV Web Services – The Basics

Identify the PageInteractions with NAV start with your requirements. For this sample, we will assume that you have been asked to display a list of Items for sale.

Any User Interface in NAV can be published as a Web Service. This allows you to manipulate data through Web Services as if you were using NAV itself.

This also means that all of Navision’s validation routines are run when you attempt to modify data.

So, the first step then, is to identify the User Interface or “Page” that an end-user would use to accomplish the task.

Looking around in NAV, you find the Items for sale in a convenient, “Item List” interface.

[email protected]

Page 4: Programming With NAV Web Services – The Basics

This leads to the biggest “gotcha” with NAV Web Services. Since you want to show a list of Items, the intuitive step would be to use the “Item List” interface.

This is wrong. What you want to do is use the single item interface.

For our purposes, that means we want to use the detail view for one Item (the “Item Card”) that is accessed by double-clicking an item in the above list.

Once you are on the detail page, select About This Page from the Help menu.

[email protected]

Page 5: Programming With NAV Web Services – The Basics

This opens up a Help Screen that tells you the Page Name and Number needed for the next step.

It also tells you the Source Table that the data comes from in case you want to poke around in SQL and make sure you are getting the data you think you are.

Now we know we want Page 30 – Item Card.

[email protected]

Page 6: Programming With NAV Web Services – The Basics

Publish the Web ServiceThis is the easy part.

Navigate to the Web Services interface (Departments->Administration->IT Administration->General->Web Services) and select the New button.

Object Type is Page (We’ll talk about the other types later).

Object ID is 30 – This is the Page Number that we identified in the earlier step.

Service Name – This becomes part of the URL that you use to access the Web Service, so pick something simple and memorable. I picked “Item” for this.

Check the Published checkbox.

That’s it. Your Web Service is now available.

[email protected]

Page 7: Programming With NAV Web Services – The Basics

Test the URL in a Web BrowserThis is the pattern for your Web Service URL:

http://<Server>:<WebServicePort>/<ServerInstance>/WS/<CompanyName>/Page/<ServiceName>

Microsoft fully explains each piece of the URL here:

http://msdn.microsoft.com/en-us/library/dd355398.aspx

Most of the pieces of the URL can be discovered using the Select Server dialog in the NAV Role-Tailored Client.

[email protected]

Page 8: Programming With NAV Web Services – The Basics

For this sample, Server = 192.168.10.12 – you can use a Windows Name on your local network (e.g. NAVSERVER) or).you can use an internet address such as (navservices.cronuscorp.netAlthough, if you publish over the internet, you will want to use SSL.

This is a bit beyond the scope of this article, but you need to send a Windows Login to NAV Web Services, so be sure you or your network administrator fully understand the security implications of what you are doing if you expose services over the public internet.

[email protected]

Page 9: Programming With NAV Web Services – The Basics

For SOAP Services Port is 7047 by default.

The Server Instance in this case is commercer2

So, the first part of the URL would be:

http://192.168.10.12:7047/commercer2

Finishing up, the next part is the constant string WS.

[email protected]

Page 10: Programming With NAV Web Services – The Basics

The Company Name is the company you wish to connect to. We will use CRONUS Supply, Inc. from the Select Server dialog.

The rest of the information comes from the Web Services page in NAV where you published the service.

The next piece is the Object Type from the Web Services page. Ours will be Page because that is the only type we know how to use right now.

Finally, the Service Name is the name we gave when we published the service. Item for our example.

This then is our final URL:

http://192.168.10.12:7047/commercer2/WS/CRONUS%20Supply,%20Inc/Page/Item

[email protected]

Page 11: Programming With NAV Web Services – The Basics

If you open that in a Web Browser and everything has been set up properly, you will get this XML to the right in reply

This is the simplest way to be sure you have the URL correct and the infrastructure is available before you sit down to write code. If the Web Service does not send XML to the browser, then you will need to work with your Network Administrator to be sure the services are published and you are using the correct URL.

[email protected]

Page 12: Programming With NAV Web Services – The Basics

Add a Service Reference to Your ProjectAfter all that, we are ready to open up Visual Studio and get to work.

I set up a simple ASP.NET Empty Web Site using the File -> New Web Site dialog in Visual Studio.

Once you have your project started, right-click on your project in Solution Explorer and select Add Service Reference…

[email protected]

Page 13: Programming With NAV Web Services – The Basics

In the Add Service Reference dialog, paste the URL for the service from the earlier step and select the Go button.

The Namespace field is how you will refer to the generated code in your project, so I like to change the default to something a bit more descriptive. I used ItemService for this sample.

[email protected]

Page 14: Programming With NAV Web Services – The Basics

Write Some CodeAt this point, you are ready to write some code to perform the task at hand.

I will break down some of the more common tasks in separate posts, because the above preliminary work is the same regardless of the task.

What’s Next?We get to see some code. The code samples will break out like this:

Simple read of ItemsReading a Header/Line document such as a Sales Quote.Using Filters to limit the data returned. Sample is Sales Quote for Customer 10000 (The Cannon Group PLC)Create a new Sales OrderOverview of Codeunits and other Object Types.ReferencesMSDN Reference “How to build a URL”: http://msdn.microsoft.com/en-us/library/dd355398.aspx

Want More Microsoft Dynamics Tips?

View My Blog

[email protected]