Tired of googling and reading about OData 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 REST) 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.
The Service Document
The Service Document is a list of all the top-level entities and is typically available at the Service Root URI.
Retrieving entities and their properties
Retrieving entities is simple, just add the entity type to the URI
A single entity can be retrieved through its key.
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.
Retrieving the metadata
The metadata model can be retrieved by adding the suffix $metadata to the URI
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.
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).
$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.
$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.
$expand=: identifies the Collection of Categories as well as each of the Products associated with each Category.
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).