Announcing the OData Helper for WebMatrix Beta

I’m a big fan of working smarter, not harder.  I hope you are too.  That’s why I’m excited by the helpers in WebMatrix which are designed to make your life easier when creating websites.  There are a range of Helpers available out of the box with WebMatrix – you’ll use these day in, day out when creating websites – things like Data access, membership, WebGrid and more.  Get more information on the built-in helpers here.

It’s also possible to create your own helpers (more on that in a future blog post) to enable other people to use your own services or widgets.  We are are currently working on a community site for people to share and publicize their own helpers – stay tuned for more information on that. 

Today we are releasing the OData Helper for WebMatrix.  Designed to make it easier to use OData services in your WebMatrix website, we are open sourcing it on CodePlex and is available for you to download, use, explore and also contribute to.  You can download it from the CodePlex website.

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

What is OData?

OData, or as I like to call it “Oh…. Data” Winking smile, is an open specification that makes it possible to have consistent APIs for all the different services out there.  Effectively it’s REST with a powerful query syntax that makes it easy to extract the data you want from a service.  So far there are a bunch of websites out there that have exposed their data using the spec, including Facebook, Netflix, Stackoverlow etc.  There’s a full list over at www.odata.org as well as more information on the spec itself.

Getting Started Video

Get Microsoft Silverlight

How to use the OData Helper

The OData helper supports CRUD (Create, Read, Update and Delete) methods - as you would expect -  but for reading there are a couple of syntaxes available; use the one which feels most natural to you.  Here are a couple of examples of the different syntaxes:

Get the top 5 Horror Titles (Using String Filters)

 

  1. @using Microsoft.Samples.WebPages.Helpers
  2. @{
  3. var result = OData.Get("http://odata.netflix.com/Catalog/Genres('Horror')/Titles","$orderby=AverageRating desc&$top=5");
  4. var grid = new WebGrid(result);
  5. }

 

Get the top 3 Movies in French (Using Query Syntax)

 

  1. @using Microsoft.Samples.WebPages.Helpers
  2. @{
  3. var result = OData.Open("http://odata.netflix.com/Catalog/Languages('French')/Titles")
  4. .Where("Type eq 'Movie'")
  5. .OrderBy("AverageRating desc")
  6. .Top(3)
  7. .Get();
  8. var grid = new WebGrid(result);
  9. }

 

Inserting new data
  1. @using Microsoft.Samples.WebPages.Helpers
  2. @{
  3.    var movie = OData.CreateEntity();
  4.    movie.Name = "OData Helpers - The Movie";
  5.    movie.ReleaseYear = 2010;
  6.    movie.BoxArt.LargeUrl = "http://cdn-4.nflximg.com/us/boxshots/tiny/5670394.jpg";
  7.    OData.Insert("http://odata.netflix.com/Catalog/Titles", movie);
  8. }

Updating existing data

 

  1. @using Microsoft.Samples.WebPages.Helpers
  2. @{
  3.    var movie = OData.Get("http://odata.netflix.com/Catalog/Titles('13kaI')");
  4.    movie.Name = "OData Helpers - The Movie";
  5.    OData.Update("http://odata.netflix.com/Catalog/Titles('13kaI')", movie);
  6. }

 

Deleting existing data
  1. @using Microsoft.Samples.WebPages.Helpers
  2. @{
  3.    OData.Delete("http://odata.netflix.com/Catalog/Titles('13kaI')");
  4. }

With Netflix service the insert, update and delete operations won’t work because they only provide read access – so the examples above are just for illustrative purposes only.

If you are new to the OData query syntax I would recommend checking out the docs that the OData team have put together.

What can you do?

There are bunch of OData services out there (full list here), why not create some wrapper classes for each service with common operations baked in so other developers don’t even have to know the syntax.  You’ll see what I mean if you explore the sample application in the download section of the CodePlex project.  We’ve included a Netflix.cs file in app_code folder – it’s just a wrapper around the OData helper class which performs some commonly used queries for the user.  I’d love to hear what you can do!

Next Steps

OData Helper v2

We’ve already cooked up some enhancements for the next version of the OData helper, you can find the list here.  If you think of anything you would like to see, please reply to the discussion in the forum!

Other Helpers

I’m now working on some other helpers which I think are pretty cool – you’ll hear more about them soon.  I’d love to hear about your ideas for helpers – maybe I can build it for you!  If you have an idea leave me a comment or send me mail – james {at} microsoft.com

Tags:

blog comments powered by Disqus