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.



About the Author

Vipul Vipul Patel

Vipul Patel is a Software Engineer currently working at Microsoft Corporation, working in the Office Communications Group and has worked in the .NET team earlier in the Base Class libraries and the Debugging and Profiling team. He can be reached at vipul_d_patel@hotmail.com

Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: September 17, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Another day, another end-of-support deadline. You've heard enough about the hazards of not migrating to Windows Server 2008 or 2012. What you may not know is that there's plenty in it for you and your business, like increased automation and performance, time-saving technical features, and a lower total cost of ownership. Check out this upcoming eSeminar and join Rich Holmes, Pomeroy's practice director of virtualization, as he discusses the …

  • Not long ago, security was viewed as one of the biggest obstacles to widespread adoption of cloud-based deployments for enterprise software solutions. However, the combination of advancing technology and an increasing variety of threats that companies must guard against is rapidly turning the tide. Cloud vendors typically offer a much higher level of data center and virtual system security than most organizations can or will build out on their own. Read this white paper to learn the five ways that cloud …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds