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.
- @{
- var result = OData.Get("http://odata.netflix.com/Catalog/Genres('Horror')/Titles","$orderby=AverageRating desc&$top=5");
- var grid = new WebGrid(result);
- }
-
- @grid.GetHtml();
What is OData?
OData, or as I like to call it “Oh…. Data” , 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
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)
- @using Microsoft.Samples.WebPages.Helpers
- @{
- var result = OData.Get("http://odata.netflix.com/Catalog/Genres('Horror')/Titles","$orderby=AverageRating desc&$top=5");
- var grid = new WebGrid(result);
- }
Get the top 3 Movies in French (Using Query Syntax)
- @using Microsoft.Samples.WebPages.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);
- }
Inserting new data
- @using Microsoft.Samples.WebPages.Helpers
- @{
- 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 existing data
- @using Microsoft.Samples.WebPages.Helpers
- @{
- 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 existing data
- @using Microsoft.Samples.WebPages.Helpers
- @{
- OData.Delete("http://odata.netflix.com/Catalog/Titles('13kaI')");
- }
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