Saturday, August 25, 2012

Using OData in Windows 8 Apps for Windows 8 RTM

The following is an excerpt from Chapter 6 of my book, Building Windows 8 Apps with C# and XAML.

The Open Data Procotol (OData) is a web protocol used for querying and updating data. It is a REST-based API built on top of Atom that uses JSON or XML for transporting information. Windows 8 applications have native support for OData clients once you download and install the client from:

http://go.microsoft.com/fwlink/?LinkId=253653

To access OData services, you simply add a service reference the same way you would for a SOAP-based web service. A popular OData service to use for demonstrations is the Netflix movie catalog. You can browse the service directly by typing http://odata.netflix.com/catalog/ into your browser. In most browsers you should see an XML document that contains various tags for collections you may browse. For example, the collection referred to as Titles indicates you can browse all titles using the URL, http://odata.netflix.com/catalog/Titles.

The Netflix project (available from Chapter 6 in the download available at http://windows8applications.codeplex.com) shows a simple demonstration of using this OData feed. The main URL was added as a service reference the same way the weather service was added in the previous example. The first step in using the service is to create a proxy to access it. This is done by taking the generated class from adding the service and passing in the service URL:

var netflix =
    new NetflixCatalog(
        new Uri(
            "http://odata.netflix.com/Catalog/",
            UriKind.Absolute));

Next, set up a collection for holding the results of an OData query. This is done using the special DataServiceCollection class:

private DataServiceCollection<Title> _collection;
...
_collection = new DataServiceCollection<Title>(netflix);
TitleGrid.ItemsSource = _collection;
Finally, specify a query to filter the data. This query is passed to the proxy and will load the results into the collection. In this example, the query will grab the first 100 titles that start with the letter "Y" in order of highest rated first:
var query = (from t in netflix.Titles
                where t.Name.StartsWith("Y")
                orderby t.Rating descending 
                select t).Take(100);            
_collection.LoadAsync(query);

Finally, as data comes in you have the option to page in additional sets of data. This is done by checking the collection for a continuation. If one exists, you can request that the service load the next set. This allows you to page in data rather than pull down an extremely large set all at once.

if (_collection.Continuation != null)
{
    _collection.LoadNextPartialSetAsync();                    
}

Run the included sample application. You should see the titles and images start to appear asynchronously in a grid that you can scroll through.

As in the previous example, the results of the web service are bound directly to the grid:

<Image Stretch="Uniform" Width="150" Height="150">
    <Image.Source>
        <BitmapImage UriSource="{Binding BoxArt.LargeUrl}"/>
    </Image.Source>
</Image>
<TextBlock Text="{Binding Name}" Grid.Row="1"/>

The Windows 8 development environment makes it easy and straightforward to connect to web services and pull data in from external sources. Many existing applications expose web services in the form of SOAP, REST, and OData feeds. The built-in support to access and process these feeds makes it possible to build Windows 8 applications that support your existing functionality when it is exposed via web services.

Jeremy Likness