Summary

The OData helper allows you to easily retrieve and update data from any service that expose its data using the OData Protocol. It does not require that you configure any specific client or create any custom class; by pointing the helper to the correct URL, you can have all the data you need ready to be displayed using the Razor syntax.

Getting started in 60 seconds

Download the OData Helper package and extract its content in the root folder of your WebMatrix site. You're good to go!

For details on how to query a service using the OData syntax check out this website: http://www.odata.org/developers/protocols/uri-conventions

Note: Make sure there is a .cshtml file in the root folder of your site. If not, create a new .cshtml file there.

Examples using the Netflix OData Service

Top 5 Horror Titles (Using String Filters)

@{
var result = OData.Get("http://odata.netflix.com/Catalog/Genres('Horror')/Titles","$orderby=AverageRating desc&$top=5");
var grid = new WebGrid(result);
} 

Top 3 Movies in French (Using Query Helpers)

@{
var result = OData.Open("http://odata.netflix.com/Catalog/Languages('French')/Titles")
.Where("Type eq 'Movie'")
.OrderBy("AverageRating desc")
.Top(3)
.Get();
var grid = new WebGrid(result);
} 

Comedy Titles Between 1980 and 1990 (Using String Filters)

@{
var result = OData.Get("http://odata.netflix.com/Catalog/Genres('Comedy')/Titles","$filter=ReleaseYear ge 1980 and ReleaseYear le 1990&$top=4");
} 

Titles with the Word Computer in Synapsis (Using Query Helpers)

@{
var result = OData.Open("http://odata.netflix.com/Catalog/Titles");
.Where("substringof('computer', ShortSynopsis)")
.Top(4)
.Get();
} 

Inserting New Data

@{
var movie = OData.CreateEntity();
movie.Name = "OData Helpers - The Movie";
movie.ReleaseYear = 2010;
movie.BoxArt.LargeUrl = "http://cdn-4.nflximg.com/us/boxshots/tiny/5670394.jpg";
OData.Insert("http://odata.netflix.com/Catalog/Titles", movie);
} 

Updating Data

@{
var movie = OData.Get("http://odata.netflix.com/Catalog/Titles('13kaI')");
movie.Name = "OData Helpers - The Movie";
OData.Update("http://odata.netflix.com/Catalog/Titles('13kaI')", movie);
} 

Deleting Data

@{
OData.Delete("http://odata.netflix.com/Catalog/Titles('13kaI')");
} 

Helper Reference

  • static ODataQuery Open (string serviceUri)
Creates a new OData query instance pointing to the specified service Uri.
  • static ODataQuery Open (string serviceUri, IAuthenticationScheme authScheme)
Creates a new OData query instance pointing to the specified service Uri.
  • static dynamic CreateMediaLink (Stream mediaResource, string contentType)
Creates a new instance of a OData Media Link.
  • static dynamic CreateEntity ()
Creates a new instance of a OData Entity.
  • static void Insert (string entitySetUri, DynamicEntity entity)
Inserts a new a OData Entity in the specified entity set.
  • static void Insert (string entitySetUri, DynamicEntity entity, IAuthenticationScheme authScheme)
Inserts a new a OData Entity in the specified entity set.
  • static void Update (string entityUri, DynamicEntity entity)
Updates the contents of an existing OData Entity.
  • static void Update (string entityUri, DynamicEntity entity, string etag)
Updates the contents of an existing OData Entity.
  • static void Update (string entityUri, DynamicEntity entity, IAuthenticationScheme authScheme)
Updates the contents of an existing OData Entity.
  • static void Update (string serviceUri, DynamicEntity entity, IAuthenticationScheme authScheme, string etag)
Updates the contents of an existing OData Entity.
  • static void Delete (string entityUri)
Deletes an existing OData Entity.
  • static void Delete (string serviceUri, string etag)
Deletes an existing OData Entity.
  • static void Delete (string entityUri, IAuthenticationScheme authScheme)
Deletes an existing OData Entity.
  • static void Delete (string entityUri, IAuthenticationScheme authScheme, string etag)
Deletes an existing OData Entity.
  • static IList< dynamic > Get (string serviceUri)
Performs a request to the OData Service Uri returning a dynamic list with the results.
  • static IList< dynamic > Get (string serviceUri, IAuthenticationScheme authScheme)
Performs a request to the OData Service Uri returning a dynamic list with the results.
  • static IList< dynamic > Get (string serviceUri, string queryOptions)
Performs a request to the OData Service Uri returning a dynamic list with the results.
  • static IList< dynamic > Get (string serviceUri, string queryOptions, IAuthenticationScheme authenticationScheme)
Performs a request to the OData Service Uri returning a dynamic list with the results.


Note: The current version of the OData Helper does not support the following features:
•Manipulating the 'links' of an entry (including the media-links pointing to the Media Resources)
•$expand operation
•Use of continuation tokens
•PUT operations for deleting individual properties. The helper always perform MERGE operations for updates
•Batch operations

Last edited Nov 8, 2010 at 4:32 PM by jsenior, version 15

Comments

camainc Aug 12, 2010 at 3:23 PM 
Does this not work when running locally? When I try the samples, I get a SocketException, like:

"No connection could be made because the target machine actively refused it 65.52.1.155:80"

It looks like WebMatrix is appending the local port (80) to my web request. If I enter the OData query directly into my browser's address bar (not going through IIS Express), I get the data that I am expecting.

What am I missing?