In this post, you will learn more about OData service URIs.

This is a post from a series Building OData Services. Earlier posts can be accessed at below links.

  1. Building OData Services [1] : OData Overview
  2. Building OData Services [2] : Understanding OData Service
  3. Building OData Services [3] : Create OData Service
  4. Building OData Services [4] : Register and Test OData Service

To work with OData it is very important to understand the URI options that can be used.

Summary of all URI Options

To demonstrate the options, Northwind service is used. This service was first introduced in OData Overview post where we looked at the metadata.

URL for the service : https://services.odata.org/V2/Northwind/Northwind.svc/

The below additions should be added after https://services.odata.org/V2/Northwind/Northwind.svc.

OptionURIDescription
Metadata/$metadataShows metadata for the service
Entity Set/Customers
/Order_Details
Get all records for the entity
Entity Set Count/Customers/$count
/Order_Details/$count
Get count of records in a entity set
Single Entity/Customers(‘ALFKI’)
/Order_Details(OrderID=10248,ProductID=11)
Get single entity record based on entity keys.For single key, it is not mandatory to mention the key name
Association/Customers(‘ALFKI’)/Orders
/Order_Details(OrderID=10248,ProductID=11)/Product
Displays associated entity sets recordsNote , separated keys
In-lining/Customers(‘ALFKI’)?$expand=OrdersShows data for principal entity and associated entity together
Filter/Customers?$filter=City eq ‘London’ and Country eq ‘UK’Used to filter the entity set records based on any property
Search String/Customers?search=’109′Pass the search string which can be used to filter the data
Select/Customers?$select=ContactName,City,CountrySelect only specific fields
Filter and Select/Customers?$filter=City eq ‘London’ &$select=ContactName,City,CountryTo combine multiple options & can be used
Top and Skip/Customers?$top=2&$skip=5Top tells how many records to fetchSkip tells how many records to skip from the first recordTogether these help with displaying contents page by page
Sorting/Customers?$orderby=Country,City
/Customers?$orderby=Country desc,City asc
Sorts the result by properties in Ascending (asc) or descending (desc) order. default is ascending
Function Import/GetFlightNumbers?CarrierID=’AA’GetCarrierFlights is name of the function import and Carrid is the importing parameter. ***This is an example and does not work with Northwind service.
$format=json/Customers?$format=jsonTo get the data in json format
OData URI Options

Now, let us look at each URL more closely.

Metadata

$metadata

Shows metadata for the service. From the metadata you can understand the structure of the service. Metadata is a large document with information on all the Entity Types, Entity Sets, Associations etc.

Entity Set

Get all records for the entity.

/Customers

Another URL that can be checked is – https://services.odata.org/V2/Northwind/Northwind.svc/Orders/

Entity Set Count

/Customers/$count

This gives number of records in entity set.

Single Entity

/Customers(‘ALFKI’)

As this Entity Type has single field as a key, it is not needed to mention the key name.

/Order_Details(OrderID=10248,ProductID=11)

Here, we need to mention the key names and values separated by comma (,) .

Association

/Customers(‘ALFKI’)/Orders

This means get all Orders for Customer entity with key ‘ALFKI’. This is one to many association.

/Order_Details(OrderID=10248,ProductID=11)/Product

This is one to one association where Product details for a product from the selected Order_Detail entity are shown.

Similarly rest of the URLs can be tested.

Function Import

/GetFlightNumbers?CarrierID=’AA’

This is not available in Northwind service. This is an example created to depict how the function import is called. Here, GetFlightNumbers is the name of the Function Import and ‘AA’ is passed to the parameter CarrierID.

Next post – Building OData Services [6] : Implementing all URIs has all the information and code samples required for implementing these URIs.

Visit OData Development in SAP to explore all articles on OData.


If you like the content, please subscribe…

Join 4,024 other subscribers

Discovering ABAP YouTube Channel