The true power of OData is that the programming model is the same for any feed. I spend a lot of time building and demoing my own feeds- usually building an OData service around Northwind or AdventureWorks. To realize the power of OData you also need to know that you can consume public feeds. Let’s take a look at consuming the Microsoft TechEd Sessions OData Service. The TechEd service can be found here: http://odata.msteched.com/sessions.svc/
Being a RESTful service, we can drill down a little and investigate our data. I will do some URL querying and look at a list of all the speakers as well as their sessions. For example I can drill down to see all speakers named “Forte”
http://odata.msteched.com/sessions.svc/Speakers?$filter=SpeakerLastName eq 'Forte'
Or all of my sessions:
This is the beauty of OData, we don’t know how it was created, we also don’t care. All we care is if we can consume it easily. Let’s do so with an ASP.net application and the OData client for ASP.NET.
To get started, create a new ASP.NET application. In the application, right click on the References folder of the project in the Solution Explorer and select “Add Service Reference”. Put in the public URL of the TechED 2010 OData Service. This creates a proxy so you can code against the service locally and not know the difference.
Next set a reference to System.Data.Services.Client. This will enable us to use the OData client library and LINQ on the ASP.net client. Then drag a textbox, button, and a gridview to the ASP page. We’ll fill the gridView with the Speaker data filtered on the last name field based on what was typed in to the textbox. We accomplish this with the following code on the button click.
1: //set a reference to ServiceReference1 and System.Data.Services.Client
2: ODataTENA10Entities dat =
3: new ODataTENA10Entities(new Uri("http://odata.msteched.com/sessions.svc/"));
5: //LINQ statement to filter by the textbox
6: var result = from s in dat.Speakers
7: where s.SpeakerLastName.StartsWith(TextBox1.Text.ToString())
8: orderby s.SpeakerLastName
9: select s;
11: //bind the results
12: GridView1.DataSource = result;
Line 2 and 3 sets the data context and the LINQ statement is on lines 6-9. Line 7 is where we do the filtering based on the textbox. Pretty easy stuff.
Page rendered at Thursday, July 2, 2020 5:45:29 AM (Eastern Daylight Time, UTC-04:00)
The opinions expressed herein are my own personal opinions and do not represent
my employer's view in anyway.