OData for Pragmatists

July 30 2015

0 comments 0

Tired of googling and reading about without having a chance to play with it? Get happy then, this post is for those pragmatics like me that enjoy learning by doing rather than dealing with theory. If you enjoy the trial and error process, I can assure you that you won’t be disappointed.

What is OData?
Open Data Protocol (OData) is an open protocol to allow the creation and consumption of queryable and interoperable RESTful APIs in a simple and standard way. It defines an abstract data model (EDM) and a protocol (based on ) that let any client access information exposed by any data source.

The anatomy of an OData service
A URI used by an OData service has up to three significant parts: the service root URI, resource path and query string options.

Screen Shot 2015-07-31 at 5.21.49 PM

The Service Document
The Service Document is a list of all the top-level entities and is typically available at the Service Root URI.
ex. http://services.odata.org/OData/OData.svc/

Retrieving entities and their properties
Retrieving entities is simple, just add the entity type to the URI
ex. http://services.odata.org/OData/OData.svc/Products

A single entity can be retrieved through its key.
ex. http://services.odata.org/OData/OData.svc/Products(1)

A query can be refined as much as needed to retrieve related entities:

Counting the entities
Counting the entities is as easy as adding the $count suffix to an entity URI.
ex. http://services.odata.org/OData/OData.svc/Products/$count

Retrieving the metadata
The metadata model can be retrieved by adding the suffix $metadata to the URI
ex. http://services.odata.org/OData/OData.svc/$metadata

Formatting the response
With the exception of the metadata of the service (which is expressed in CSDL), all responses may be formatted in Atom or JSON, either by using the $format=json or $format=atom options or by setting the “Accept” header (i.e. “application/json”). The default is Atom/AtomPub.
ex. http://services.odata.org/OData/OData.svc/?$format=json

Querying the service
To query the service, the following options can be combined to refine the search:

$top=n: Returns only the first n entities in an entity set (or in Atom terms, the first n entries in a feed).
ex. http://services.odata.org/OData/OData.svc/Products?$top=2

$skip=n: Skips the first n entities in an entity set. Using this option lets a client retrieve a series of distinct pages on subsequent requests.
ex. http://services.odata.org/OData/OData.svc/Products?$skip=2

$orderby=: Orders results, in ascending or descending order, by the value of one or more properties in those results.
ex. http://services.odata.org/OData/OData.svc/Products?$orderby=Name desc

$filter=: Returns only entities that match the specified expression.
ex. http://services.odata.org/OData/OData.svc/Products?$filter=substringof(‘Ounce’, Description) eq true

$select=: Returns only the specified properties in an entity.
ex. http://services.odata.org/OData/OData.svc/Products?$select=Price,Name

$expand=: identifies the Collection of Categories as well as each of the Products associated with each Category.
ex. http://services.odata.org/OData/OData.svc/Categories?$expand=Products

These operations can be combined to form complex queries, like

As you have seen, consuming an OData service is simple and yet powerful.
Hope you enjoyed this post, see you soon.

P.S.: This is not meant to be a comprehensive description about OData (for that you can find proper documentation here).

We'd love to hear your opinion on this post