Floating FB popout byF5debug

Learn Windows Phone 7 Development in 31 Days – Day 27 – Working with Consuming an ODATA Service in WP7

 

Introduction:

In this article we are going to see how to consume a ODATA service in Windows Phone 7 application. ODATA is Open Data Protocol is an entity model used to access data as a REST service, which can be used to execute queries and to create, update and delete data on to the remote data service. ODATA service allows the consumers to query the data over the http protocol and retrieve the result in a plain XML format. Normally a mobile application development requires data that are available as a service which can be consumed and use on demand, where the ODATA Service plays a major role. We have variety of ODATA services that are available in market, rather we can create our ODATA service which can also be consumes as a WCF Data Services.     [more]

As per ODATA website, The Open Data Protocol (OData) is a Web protocol for querying and updating data that provides a way to unlock your data and free it from silos that exist in applications today. OData does this by applying and building upon Web technologies such as HTTP, Atom Publishing Protocol (AtomPub) and JSON to provide access to information from a variety of applications, services, and stores. The protocol emerged from experiences implementing AtomPub clients and servers in a variety of products over the past several years.  OData is being used to expose and access information from a variety of sources including, but not limited to, relational databases, file systems, content management systems and traditional Web sites.

We have a comprehensive list of public ODATA Services that can be consumed from the client devices using the link http://www.odata.org/producers. Let us see the step by step process on how to consume the ODATA Service in our Windows Phone 7 application and see how to make use of it efficiently.

Steps:

Open Visual Studio 2010 IDE in administrator mode and create a new Silverlight for Windows Phone 7 project with a valid project name as shown in the screen below.

clip_image002

Now we need to design our XAML page where we want to show our consumed ODATA service, we can use the Listbox and dynamically bind the data which we consume. Copy the below XAML code to get the unique design in our sample project as shown in the screen below.

clip_image004

XAML Code:

01

<Grid x:Name="LayoutRoot" Background="Transparent">

02

<Grid.RowDefinitions>

03

<RowDefinition Height="Auto"/>

04

<RowDefinition Height="*"/>

05

</Grid.RowDefinitions>

06

 

07

<!--<span class="hiddenSpellError" pre="">TitlePanel</span> contains the name of the application and page title-->

08

<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">

09

<TextBlock x:Name="ApplicationTitle" Text="F5DEBUG WP7 TUTORIALS" Style="{StaticResource PhoneTextNormalStyle}"/>

10

<TextBlock x:Name="PageTitle" Text="OData Service" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>

11

<!--<span class="hiddenSpellError" pre=""-->StackPanel>

12

 

13

<!--<span class="hiddenSpellError" pre="">ContentPanel</span> - place additional content here-->

14

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

15

<button>OData" Margin="0,507,0,0" Click="Button_Click"></button>

16

<ListBox x:Name="MainListBox"&nbsp; ItemsSource="{Binding}" Margin="0,6,0,106">

17

<ListBox.ItemTemplate>

18

<DataTemplate>

19

<StackPanel Margin="0,0,0,17" Width="432">

20

<TextBlock Text="{Binding Path=Name}" TextWrapping="NoWrap"

21

Style="{StaticResource PhoneTextExtraLargeStyle}"/>

22

<TextBlock Text="{Binding Path=Description}" TextWrapping="NoWrap"

23

Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>

24

<TextBlock Text="{Binding Path=Url}" TextWrapping="NoWrap" Margin="12,-6,12,0"

25

Style="{StaticResource PhoneTextSubtleStyle}"/>

26

</StackPanel>

27

</DataTemplate>

28

</ListBox.ItemTemplate>

29

</ListBox>

30

</Grid>

31

</Grid>

Once we designed the XAML page, now we need to consume the ODATA service, in this sample application we will make use of the Telerik Video’s ODATA service which is available to public. The ODATA service is available using the URI http://tv.telerik.com/services/odata.svc/. To add the ODATA service right click on the project name and select Add Service Reference as shown in the screen below.
clip_image006

Clicking on Add Service Reference will get a pop window where we need to provide the OATA Service uri and click on Go button to discover the service online. Once the service is discovered we need to change the namespace to a user defined name as ODataTelerik as shown in the screen below.

clip_image008

On successfully adding the ODATA service reference to the project we can see a list of files added to the service reference folder. Now we are ready with the service, our next step is write some code that will do the necessary process to get the data from the service and show it to the user interface which we designed. Add the below 2 namespaces which are required to consume and get the data from the ODATA service as shown in the screen below.

Code Behind:

1

using System.Data.Services.Client;

2

using F5debugWp7ODataConsule.ODataTelerik;

Once we added the above 2 namespaces we need to start with our code to consume the service, copy the below code and add it to the Mainpage class. This code creates an instance of the reference which we added to the project and get the ODATA service URI which is used to create a reference while consuming the service as shown in the code below.

clip_image010

Code Behind:

1

private ODataDataSource TelerikEntity;

2

private static string strODataURI = "<a href="http://tv.telerik.com/services/odata.svc/%22;">http://tv.telerik.com/services/odata.svc/";</a>

       

3

private readonly Uri tURI = new Uri(strODataURI);

4

private DataServiceCollection<Video> videos;

Now in the button click event we need to call the ODATA Service and get the data which is required to bind to the controls which we designed in our initial process. Once we created the instance we can query the data from the DataServiceCollection by just using the LINQ query as shown in the screen below. Just copy and paste the below code the button click event to step further.

clip_image012

Code Behind:

01

private void Button_Click(object sender, RoutedEventArgs e)

02

{

03

TelerikEntity = new ODataDataSource(tURI);

04

videos = new DataServiceCollection<video tabindex="0">(TelerikEntity);</video>

05

 

06

var queryvideo = from vid in TelerikEntity.Videos

07

select vid;

08

 

09

videos.LoadCompleted += new EventHandler<LoadCompletedEventArgs>(videos_LoadCompleted);

10

videos.LoadAsync(queryvideo);

11

}

Now we need to add the below code which is basically required to handle the event on the completion of loading the data to the user interface to scroll and have a better look for the end users as shown in the code below.

Code Behind:

01

void videos_LoadCompleted(object sender, LoadCompletedEventArgs e)

02

{

03

if (e.Error == null)

04

{

05

if (videos.Continuation != null)

06

{

07

videos.LoadNextPartialSetAsync();

08

}

09

else

10

{

11

this.MainListBox.DataContext = videos;

12

}

13

}

14

else

15

{

16

MessageBox.Show("Error while fetching the data!!!");

17

}

18

}

Now we are done with our complete code, our final code behind class will look like the below code.

Code Behind:

01

using System;

02

using System.Collections.Generic;

03

using System.Linq;

04

using System.Net;

05

using System.Windows;

06

using System.Windows.Controls;

07

using System.Windows.Documents;

08

using System.Windows.Input;

09

using System.Windows.Media;

10

using System.Windows.Media.Animation;

11

using System.Windows.Shapes;

12

using Microsoft.Phone.Controls;

13

 

14

using System.Data.Services.Client;

15

using F5debugWp7ODataConsule.ODataTelerik;

16

 

17

namespace F5debugWp7ODataConsule

18

{

19

public partial class MainPage : PhoneApplicationPage

20

{

21

private ODataDataSource TelerikEntity;

22

private static string strODataURI = "<a href="http://tv.telerik.com/services/odata.svc/%22;">http://tv.telerik.com/services/odata.svc/";</a>

       

23

private readonly Uri tURI = new Uri(strODataURI);

24

private DataServiceCollection<Video> videos;

25

 

26

// Constructor

27

public MainPage()

28

{

29

InitializeComponent();

30

}

31

 

32

private void Button_Click(object sender, RoutedEventArgs e)

33

{

34

TelerikEntity = new ODataDataSource(tURI);

35

videos = new DataServiceCollection<Video>(TelerikEntity);

36

 

37

var queryvideo = from vid in TelerikEntity.Videos

38

select vid;

39

 

40

videos.LoadCompleted += new EventHandler<LoadCompletedEventArgs>(videos_LoadCompleted);

41

videos.LoadAsync(queryvideo);

42

}

43

 

44

void videos_LoadCompleted(object sender, LoadCompletedEventArgs e)

45

{

46

if (e.Error == null)

47

{

48

if (videos.Continuation != null)

49

{

50

videos.LoadNextPartialSetAsync();

51

}

52

else

53

{

54

this.MainListBox.DataContext = videos;

55

}

56

}

57

else

58

{

59

MessageBox.Show("Error while fetching the data!!!");

60

}

61

}

62

}

63

}

Now build and execute the application by simply pressing F5 from the keyboard and we can see the expected results are shown in the screens below.

Output Screens:

clip_image014

Conclusion:

So in this article we have seen what is ODATA service and how to consume the service from the Windows Phone 7 application to get the data and process it as per the requirement.

About Author: Karthikeyan Anbarasan, Microsoft MVP (Most Valuable Professional) in ASP.NET/IIS Architecture. He is the Founder and Chief Editor of the www.f5debug.net website and has authored books on Windows Phone and Business Intelligence(SSIS). He is also a Passionate Speaker and a Blogger on Microsoft Technologies.

You can Join Me On: Facebook, Twitter, Google Plus, LinkedIn

Comments (1) -

Roopa
Roopa
9/13/2013 5:11:02 AM #

Hi,

Thanks for Your Wonderful tutorial.
I was trying OData but i am not getting ODataDataSource from this link http://tv.telerik.com/services/odata.svc/ i got Only DataManger serveice.

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Recent Posts

Random Posts

Most Viewed

URL Shortner

Create your own short urls!!!

Site Counter

free counters

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer. For accuracy and official reference refer to MSDN/ TechNet. I have documented my personal experience on this blog.

Protected by Copyscape Web Plagiarism Finder

Site Meter