“Open” for business: using OData in Windows Phone apps

Everyone who has used a modern smartphone knows that with very few exceptions, apps that have the most value to its users are those that are connected to services. Standalone apps just don’t cut it anymore. With that being said, finding data sources through web-based services can be difficult and even when you find them, the format those services return data in can be messy. OData (short for Open Data Protocol) is a service format that has received the backing of many major and public data source providers as a way to standardize the way apps consume data. This standardization allows developers like you to deliver great app experiences on a number of client form factors while not having to worry about how the data gets to your app. This is also true for apps built on Windows Phone that use OData – it works great with our mobile platform and you can build amazing, data driven apps using it! This purpose of this post shows you how easy it is to integrate OData feeds into your Windows Phone app. If you’ve built an OData-driven Windows Phone app, feel free to share a link to it by commenting!

Open Data Protocol (OData for short) is a standardized way of getting information from web-based services and is growing in popularity as a way for apps to consume data from the web, regardless of their form factor.  They are lightweight (meaning the extraneous, non-content data is kept to a minimum) which is ideal for mobile scenarios over carrier networks where data usage fees are involved.

Basically, OData is built on existing web communication technologies, including HTTP, AtomPub and JSON, which means that it makes use of methods you may already be familiar with.  For more info on how to develop using OData, you should check this subsite out.

To build a Windows Phone app using OData, you need to first download and install the free Windows Phone developer tools and SDK.  If you’re new to Windows Phone development, I strongly encourage you to take a look at our Windows Phone Developer Resources Page that provides you with a number of great places to start learning Windows Phone development.

Once you’ve done that, you’ll need to download and install the OData client library for Windows Phone, which you can get here.  Then, once you’ve done that, you need to generate a stub file for the OData service that you want to consume.  The way you do this is to get the URL for your data service (for example, https://services.odata.org/Northwind/Northwind.svc/ or https://odata.netflix.com/Catalog/) and then you go to the command prompt and type in a command similar to the following:

DataSvcutil.exe /uri:https://odata.netflix.com/Catalog/ /DataServiceCollection /Version:2.0 /out:netflixClientTypes.cs

This command will create a C# file (“netflixClientTypes.cs” in this example) for the OData service specified in the URI parameter (in this example, the URI is https://odata.netflix.com/Catalog).  In essence, this C# file is a description of the service in class format, meaning your app can make use of the service via classes and object-oriented programming rather than parsing through XML.

Once this file has been generated, you need to add the file to your Visual Studio project (right-click on the Visual Studio project you built in the Solution Explorer, select “Add” from the context menu that appears and then select “Existing Item” from the sub-menu that appears from there – see image below).

AddExistingItem

At this point you may wish to build the solution so that the intellisense will pick up the classes and entities inside those classes from the stub file. 

Once that is done, you should be able to programmatically access the classes in the stub file within your app and you can use the data from the feed programmatically to data bind content to controls and the like. 

For an example of how to do this with the Netflix OData feed that was mentioned in this post, you should read this MSDN article as it goes through what I have discussed as well as how to implement and databind controls to the content in the feed within your Windows Phone app.  If you are looking for code, you should check out this great OData feed example (it’s a zipped up Visual Studio project) from Mark Arteaga of Redbit Development where he uses the City of Edmonton data feed to create a Windows Phone app listing historical buildings found in the city.

As always, happy coding!