OData on Windows 8 and Windows Phone

Last week during the Windows 8 and Windows Phone camp in Zwolle, the Netherlands I talked about Data access on Windows 8 and Windows Phone. This blog post is a recap of the access of OData on Windows 8 and Windows Phone part of my talk, a separate post will go into the “Local” storage side of things (ApplicationData, IsolatedStorage, SQLite).

The first things I mentioned that makes life easier when wanting to access OData on Windows 8 and Windows Phone is to download the OData Client Tools. These tools add “Add Service Reference…” support to your Windows 8 and Windows Phone projects for OData services, as well as adding a set of extensions to generate the proxy classes for the OData feed.

Once you install the tools, you can simply right-click your project and select “Add Service Reference…”, fill in the OData feed URL (the example used in my talk was the Dutch motor vehicle company’s data accessible through Windows Azure Marketplace) and specify a name. The installed extensions will then generate the proxy classes that can be used to access the OData feed. For example:

DataServiceCollection<KENT_VRTG_O_DAT> _kentekens;

[...]

var uri = new Uri("https://api.datamarket.azure.com/opendata.rdw/VRTG.Open.Data/v1/");
var context = new opendatardwVRTGOpenDataContainer(uri);

context.SendingRequest += (s, a) =>
    a.RequestHeaders["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(":[YOUR_ACCOUNT_KEY]"));

_kentekens = new DataServiceCollection<KENT_VRTG_O_DAT>(context);

var qry = from kenteken in context.KENT_VRTG_O_DAT
            where kenteken.Kenteken == kentekenTextBox.Text.Trim().Replace("-", string.Empty).ToUpperInvariant()
            select kenteken;

_kentekens.LoadCompleted += (s, a) =>
{
    MainLongListSelector.ItemsSource = _kentekens.ToList();
};

_kentekens.LoadAsync(qry);

The important thing to note here is the swap out the [YOUR_ACCOUNT_KEY] placeholder above with your actual account key (found on the Account Key page on your profile page of Windows Azure Marketplace). You also notice that I’m not adding the authorization header by adding a NetworkCredential as instructed on Implement HTTP Basic Auth in your Marketplace App (MSDN), because it was giving me some grief, but instead I opted to add the request header manually.

The code is 100% identical on Windows 8 and Windows Phone, so be sure to share it in your code-sharing architecture!

2 thoughts on “OData on Windows 8 and Windows Phone

  1. Pingback: OData on Windows 8 and Windows Phone

  2. Pingback: OData on Windows 8 and Windows Phone

Leave a Reply