Consuming a NAV 2013 Page using C# and ODATA

As you may know, NAV 2013 allows you to access data in NAV via ODATA services as well as SOAP.  While Daniel Rimmelzwaan put together a good overview of publishing ODATA in NAV (which you can read at NAV 2013 Beta - OData Introduction), I didn’t really see any tutorials on how to consume said ODATA once you’ve got it published, so after I spent some time figuring out how to make it happen, I wrote this blog entry.

Start by making sure you’ve got some NAV data published for access via ODATA.  You can do this by pulling up the Web Services page in the NAV client, via Departments – Administration – IT Administration – General – Web Services.  For this tutorial, we’ll be publishing page 21, the Customer Card, with a Service Name value of “CustomerCard”.  Make sure you check the “Published” box, or the page won’t actually be available. 

Next, make sure that your NAV server is set up to publish ODATA services.  You can do this by pulling up the Dynamics NAV Administration Console and making sure that the “Enable OData Services” box under the OData Services FastTab is checked, and that there’s a Port value filled in for OData.  (The default port is 7048, and that’s what we’ll be using for this demo.)

From here, you’re ready to start coding, so fire up Visual Studio and create a new Console Application.  Mine is called “NAV2013_ODATA_Experiments_01”. 

Start by adding a Service Reference for your NAV server by right-clicking References in the Solution Explorer, then choosing “Add Service Reference”.  Enter the URL for the NAV server in the Address bar, using the following format: http://[servername]:[port]/[servicename]/ODATA. The server I’m working with in this example is on the same box as my Visual Studio 2010 install, the port is 7048, and the NAV service name is “DynamicsNAV70”, so my final URL is “http://localhost:7048/DynamicsNAV70/ODATA”. 

In order to make our C# code work, we add a reference to System.Data.Services.Client to make sure that we have our ODATA consuming client object. 

The end C# code looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Services.Client;

namespace NAV2013_ODATA_Experiments_01
{
    class Program
    {
        static void Main(string[] args)
        {
            string theInput = "";
            string instructions = "Press ENTER to start testing, or EXIT to quit.";
            Console.WriteLine(instructions);
            theInput = Console.ReadLine();
            while (theInput.ToUpper() != "EXIT")
            {
                string serviceUri = "http://[yourserver]:7048/DynamicsNAV70/ODATA";
                DataServiceContext context = new DataServiceContext(new Uri(serviceUri));

                NAV_ODATA.NAV theNav = new NAV_ODATA.NAV(new Uri(serviceUri));
                theNav.Credentials = new System.Net.NetworkCredential("yourusername", "yourpassword", "yourdomain");
               
                DataServiceQuery<NAV_ODATA.CustomerCard> q = theNav.CreateQuery<NAV_ODATA.CustomerCard>("CustomerCard");

                List<NAV_ODATA.CustomerCard> ccList = q.Execute().ToList();

                foreach (NAV_ODATA.CustomerCard cc in ccList)
                {
                    Console.WriteLine(string.Format("{0} {1}", cc.No, cc.Name));
                }

                Console.WriteLine("Success!");

                Console.WriteLine(instructions);
                theInput = Console.ReadLine();
            }
        }
    }
}

I noticed two funny things here.  First, the query didn’t work when I used “localhost” or the machine name or 127.0.0.1, but it did work when I used the external IP of the local machine.  Second, the service requires a NetworkCredential object to function properly; it does not pass the local user credentials.  I’m not sure if there are reasons for those or if they’re bugs that will be fixed before the final version of NAV 2013 is released. 

I looked at ODATA Consume Service Operation from C# ASP.NET 4.0 to figure out how to do some of this in C#, so I’d like to express my gratitude and give them appropriate credit.

You may also find OData Web Services to have some useful information on handling ODATA from NAV 2013 as well.

You also may be wondering about how to filter the results; I’ll try to handle that one in a future blog entry.