DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Using OData in Windows 8 Apps for Windows 8 RTM

Jeremy Likness user avatar by
Jeremy Likness
·
Aug. 25, 12 · Interview
Like (0)
Save
Tweet
Share
6.22K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Remember the Milk Web Service app

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Best Practices to Succeed at Continuous AWS Security Monitoring
  • Three SQL Keywords in QuestDB for Finding Missing Data
  • How to Cut the Release Inspection Time From 4 Days to 4 Hours
  • SAST: How Code Analysis Tools Look for Security Flaws

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: