In my last post I described how we can access Dynamics NAV 2009 SOAP web services from F# and the benefits we get by using a type provder. Since version 2013 it’s also possible to expose NAV pages via OData. In this article I will show you how the OData type provider which is part of F# 3 can help you to easily access this data.
Exposing the data
First of all follow this walkthroughÂ and expose the Customer Page from Microsoft Dynamics NAV 2013 as an OData feed.
Show the available companies
Let’s try to connect to the OData feed and list all available companies. Therefore we create a new F# console project (.NET 4.0) in Visual Studio 2012 and add references to FSharp.Data.TypeProviders and System.Data.Services.Client. With the following snippet we can access and print the company names:
As you can see we don’t need to use the “Add Service Reference” dialog. All service type are generated on the fly.
Access data within a company
Unfortunately Dynamics NAV 2013 seems to have a bug in the generated metadata. In order to access data within a company we need to apply a small trick. In the following sample we create a modified data context which points directly to a company:
Now we can start to access the data:
As you can see this approach is very easy and avoids the problem with the manual code generation. If you expose more pages then they are instantly available in your code.
As with the Wsdl type provider you can expose the generated types from this F# project for use in C# projects.
- Walkthrough: Accessing an OData Service by Using Type Providers (F#)
- Walkthrough: Creating and Interacting with a Page Web Service (OData)