Performing OData Operations

Objective

After completing this lesson, you will be able to perform OData read operations

OData Requests

Screenshot of a service document in XML (Atom) vs. in JSON

Each OData service is represented by a URI, called the service root URI. A URI is a uniform resource identifier, which is a string of characters used to identify a resource by name and address. This type of identification enables interaction with representations of the resource across a network using specific protocols like OData.

Note

The URL (Unified Resource Locator) is a subtype of a URI identifying a resource just by its address.

To consume an OData service for read, you just need a browser and the OData service root URI. The service document is the highest-level description of an SAP Gateway service. It shows the basic information about the available data. From here, it is possible to call further information on the service, and of course the data itself, by adding URI parameters.

To get data from the service, add the name of an entity set of the service to the base URI. You get a list of entities of that type, which could be the content of a database table.

Screenshot of the response to query a business partner

We want to read the BusinessPartnerSet entity set from our OData sample service. The Atom XML response is a feed with multiple entries, which are shown here in a collapsed form so that we only see the third entry. Each entry has a content filled with properties. The properties are the actual data set.

Each entry is identified in Atom by an ID, which is in fact the URI to read the entry. In addition, one or more navigation links can lead to dependent entities.

Screenshot of the response to read a business partner

We now follow the first URI in the metadata section to read just the third entry in the business partner entity set. This URI is also called the self-link of this entity. If you compare this response with the previous one, you’ll see that the entity is identical but without the surrounding feed element.

There are three navigation properties pointing to the SalesOrderSet, ContactSet, and ProductSet. All navigation properties are URIs. As a consumer of an OData service, you can just follow this URI and navigate through this web of data. Let’s do so and fire a GET call at the SalesOrders URI.

Screenshot of the response to navigate from a business partner to its sales orders

The result is a feed of sales orders associated with the selected business partner. While we can see the payload of the third entry, the other entries have been collapsed for readability. Beside the properties of the sales order, there are again navigation links available. These lead further on to the LineItems but also back to the business partner we came from.

Schematic illustration of an app showing a business partner with a button to navigate to its sales orders next to screenshots of the metadata and navigation link

One use case for navigation properties is the navigation from one screen that shows the details of a business partner to a second screen that lists all sales orders that are associated with that business partner.

To compare this with the metadata document: in a similar way to properties, navigation properties are specified as part of an entity type. A navigation property is tied to an association. An association is a named relationship between two entity types. Every association includes two association ends, which specify the entity types that are related, and the cardinality rules for each end of the association. Finally, the association set maps the entity sets to the association.

OData Operations

Create, Read, Update, Delete (CRUD) mapped to HTTP POST, GET, PUT, DELETE

One of the main features of OData is that it uses the existing HTTP verbs GET, PUT, POST, and DELETE at addressable resources identified in the URI. Conceptually, OData is a way of performing database-style create, read, update, and delete operations on resources through HTTP verbs.

By building URIs based on the rules of OData, a wide variety of data requests can be performed. These requests can often be mapped directly to SQL requests accessing data in a database.

Server address + service URI + entity set + entity key + navigation property = Entity as XML (Atom)

Calling the service URI of an OData service in a GET request, results in the service document. Adding the name of an entity set to the base URI, results in a list or set of all entities. This is called a query request.

Adding the unique key of an entity in brackets to a query request, results in a single entity having this key. Character-based keys must be put in apostrophes. This is called a read request.

Adding a navigation property of the entity type to a read request separated by a slash, results in a single entity or a set of entities depending on the multiplicity of the association. This is called a navigation request.

Perform OData Read Operations

Business Example

You are a developer or system architect. You want to perform read operations of an OData service in a Web browser.

Template:
GW100_T_BASICGW (SAP Gateway Project)
Solution:
None

Note

This exercise requires an SAP Learning system. Login information are provided by your system setup guide.

Task 1: Perform OData Read Operations

Steps

  1. In a Web browser, compare the service document of https://<server>:<port>/sap/opu/odata/sap/GW100_T_BASICGW_SRV/ using the option $format=json and sap-ds-debug=true.

    1. In the Windows start menu, choose a Web browser.

    2. In the address field, enter the URI https://<server>:<port>/sap/opu/odata/sap/GW100_T_BASICGW_SRV/.

    3. Choose Enter.

      Result

      The service document is shown as XML.
    4. To the URI, add ?$format=json.

    5. Choose Enter.

      Result

      The service document is shown as JSON.

      Note

      Some Web browsers may ask to download JSON as file.
    6. Add &sap-ds-debug=true to the URI.

    7. Choose Enter.

      Result

      The service document is shown as a HTML page consisting of JSON.

      Note

      You are free to use the options $format=json and sap-ds-debug=true in all of the following exercises, even if it is not mentioned explicitly.
  2. What is the URI to request a list of products?

    1. In the URI, replace ?$format=json&sap-ds-debug=true with ProductSet.

    2. Choose Enter.

      Example

      https://<server>:<port>/sap/opu/odata/sap/GW100_T_BASICGW_SRV/ProductSet

      Result

      The products are shown as XML.

  3. What is the URI that points to the details of the product "HT-1000"?

    1. In the URI, add ('HT-1000').

    2. Choose Enter.

      Example

      https://<server>:<port>/sap/opu/odata/sap/GW100_T_BASICGW_SRV/ProductSet('HT-1000')

      Result

      The product "HT-1000" is shown as XML.

  4. What is the URI to navigate to the supplier of the product "HT-1000"?

    1. In the URI, add /Supplier.

    2. Choose Enter.

      Example

      https://<server>:<port>/sap/opu/odata/sap/GW100_T_BASICGW_SRV/ProductSet('HT-1000')/Supplier

      Result

      The supplier of the product "HT-1000" is shown as XML.

Log in to track your progress & complete quizzes