Using oData Services in a Windows Phone Application

Introduction

Windows Phone
application developers will be excited to hear that the platform has support
for OData – the Open Data Protocol
championed by Microsoft. In an earlier article, we learned about the OData Protocol. The CodePlex site
for OData http://odata.codeplex.com/ provides
many client libraries to allow .NET
developers to consume oData data services in their applications.

One such library that the Codeplex site offers is the client
library for the Windows Phone platform.

This client library can be downloaded from http://odata.codeplex.com/releases/view/54698#DownloadId=161665

The client library contains System.Data.Services.Client.dll,
System.Data.Services.Design.dll and DataSvcUtil.exe, among other files.

Let us use the client library to create a working Windows Phone
application that uses an oData data service.

Hands-on

Let’s create an application that lists all of the available
languages in which Netflix offers movies.

To begin with, create a data class for the Netflix oData
data service.

The URI of Netflix oData data service is http://odata.netflix.com/v1/Catalog/

You create the data classes using DataSvcUtil.exe.

datasvcutil /uri:http://odata.netflix.com/v1/Catalog/ /out:.\Netflix.cs /Version:2.0 /DataServiceCollection

This will create the Netflix.cs data class.

Now create a new Silverlight
for Windows Phone application called WindowsPhoneApplicationODataDemo and add a
reference to the System.Data.Services.Client.dll. Also add the Netflix.cs to
the project.

Next, modify the MainPage.xaml to add a ListBox, which will
display the listings of the languages Netflix offers. The modified xaml will
look like this:

<!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
 
        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="Netflix Offers" 
                        Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="Customers" Margin="9,-7,0,0" 
                        Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <ListBox x:Name="MainListBox" Margin="0,0,-12,0" ItemsSource="{Binding}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Margin="0,0,0,17" Width="432">
                            <TextBlock Text="{Binding Path=Name}" TextWrapping="NoWrap" 
                                       Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                        </StackPanel>
                    </DataTemplate>
            </ListBox.ItemTemplate>
            </ListBox>
        </Grid>

Next, create an event handler for the MainPage’s PageLoaded
event. In this event hander, add the code to create a DataServiceContext and
DataServiceCollection object.

Also, in this method, create an eventhandler for the
DataServiceCollection’s LoadCompleted event.

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            // Initialize the context and the binding collection 
            netflix = new DataServiceContext(netflixUri);
            languages = new DataServiceCollection<Language>(netflix);
 
            // Register for the LoadCompleted event.
            languages.LoadCompleted
                += new EventHandler<LoadCompletedEventArgs>(languages_LoadCompleted);
 
            // Load the languages feed by using the URI.
            languages.LoadAsync(languagesFeed);
 
        }

Now, add the code in the languages_LoadCompleted to display
the data returned from the data service.

void languages_LoadCompleted(object sender, LoadCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                // Handling for a paged data feed.
                if (languages.Continuation != null)
                {
                    // Automatically load the next page.
                    languages.LoadNextPartialSetAsync();
                }
                else
                {
                    this.LayoutRoot.DataContext = languages;
 
                }
            }
            else
            {
                MessageBox.Show(string.Format("error occured"));
            }
        }

Compile and run the application. You will notice that when
the DataServiceCollection object is loaded, the application shows all the
languages Netflix offers movies in.

The DataServiceCollection object is loaded
Figure 1: The DataServiceCollection object is loaded

If you scroll down, you will find more languages.

Summary

In this article, we saw how to create a Windows Phone
application which consumes oData data services. Hope this article inspires you
to write more data centric applications that utilize oData services.

More by Author

Must Read