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

  • Event Date: April 15, 2014 The ability to effectively set sales goals, assign quotas and territories, bring new people on board and quickly make adjustments to the sales force is often crucial to success--and to the field experience! But for sales operations leaders, managing the administrative processes, systems, data and various departments to get it all right can often be difficult, inefficient and manually intensive. Register for this webinar and learn how you can: Align sales goals, quotas and …

  • "Security" is the number one issue holding business leaders back from the cloud. But does the reality match the perception? Keeping data close to home, on premises, makes business and IT leaders feel inherently more secure. But the truth is, cloud solutions can offer companies real, tangible security advantages. Before you assume that on-site is the only way to keep data safe, it's worth taking a comprehensive approach to evaluating risks. Doing so can lead to big benefits.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds