What are the different query option in ODATA-URL

The OData URL can be modified to represent the corresponding results. It provides various Filter options

$Filter: It is like the WHERE condition. The below URL in the browser returns the products with the price less than 100.

https://services.OData.org/v3/OData/OData.svc/Products?$filter=Price lt 100    

$Select: It is used to display only the respective columns of the table.

https://services.OData.org/v3/OData/OData.svc/Products?$select=ID,Categories

In the above example, it displays only the columns ID and Category.

$Orderby: It is used to sort the results. The below url displays the products with the price in decreasing order.

https://services.OData.org/v3/OData/OData.svc/Products?$orderby=Price desc    

$top :  It displays the initial records of the table.

https://services.OData.org/v3/OData/OData.svc/Products?$top=3

$skip: The Data records can be skipped through this option.

https://services.OData.org/v3/OData/OData.svc/Products?$skip=4

$expand: It is used to join two tables and display corresponding results.

https://services.OData.org/v3/OData/OData.svc/Categories(1)?$expand=Products

Multiple query options is possible through the & symbol

InitialURL/Entityset?$option1=……&$option2=….&$optionN

What is an OData service document

OData service document represents a service which includes entity sets, uniform resource identifier (URI)

It can be opened through the browser with the URI / URL

For example, let us consider the following OData service.

http://services.odata.org/v3/OData/OData.svc/

If you open this URL in browser, it shows up the different entity sets.

Entitysets shown in Browser

As you can see in the above image, the entitysets in this OData service are Products, Productdetails, Categories, Suppliers, Persons, PersonDetails, Advertisements. So it includes totally seven entitysets. The entityset ‘Category’ includes information about all categories.

If the categories is added to the above URL like

http://services.odata.org/v3/OData/OData.svc/Categories

The above url in the browser would list down all the categories.

Similarly if you wish to see all the Products. Then follow this link

http://services.odata.org/v3/OData/OData.svc/Products

URL is case sensitive. So the first letter ‘P’ in Products must be written in Capital. Otherwise it does not work.

It is much similar to the Transaction SE11 (ABAP Dictionary) where we give the Table name. The Data contents can be displayed for this table.

The URLs mentioned above executes the HTTP-Get methods, which in turn calls the Read-OData method. In the background, the data would be retrieved and the result is sent back.

Let us look into an example of retrieving data records with single primary key.

Specific line of record can also be retrieved by specifying the primary key value in the URL.

http://services.odata.org/v3/OData/OData.svc/Products(5)

In the above URL, the products with the ID 5 will be retrieved.

Retrieving data records with two primary keys.

/Entityset( p_key1 = ‘Value1’, p_key2 = ‘Value2’ )

In the above case, both the primary key fields are of datatype string. So the value is mentioned in quotes. If the datatype is integer, then quotes should not be used.

Retrieving data records with WHERE clause.

All the products with a particular category ID can also be retrieved. To achieve this, it must be predefined in the services. We can also navigate from one entity set to another. For example, in the below URL, only the data from the product entity set are displayed.

https://services.OData.org/v3/OData/OData.svc/Categories(5)/Products

Retrieving data records from two different entity sets

This option is much like the JOIN query in SQL. The data from two different entity sets are displayed with a WHERE condition.

https://services.OData.org/v3/OData/OData.svc/Categories(2)?$expand=Products