CData SAP Driver Deep Dive: Use SAP Gateway Service Builder for SAP S/4HANA and Set Up OData Service



In a previous article, we explained how to connect to SAP S/4 HANA using the RFC protocol. Due to the nature of SAP, RFC and OData services do not exist for all functions by default. It is common to customize and develop interfaces as needed to implement integration.

In contrast, platforms like Salesforce and Dynamics 365 have generic APIs implemented by default, highlighting an interesting cultural difference. In this article will setup an SAP OData service from scratch. Finally, we will connect to the implemented OData service using the CData Driver.

Note: We used SAP S/4 HANA Private Cloud Edition for this article, but the principals apply to the Public Cloud Edition.

Creating the OData Service

In this article we will setup an an OData service to retrieve a list of BNKA (bank master) data using GetEntitySet.

OData supports CRUD operations through interfaces like Create, Delete, GetEntity (single record retrieval), GetEntitySet (multiple record retrieval), and Update. For now, we will implement GetEntitySet with minimal scope and publish it as an OData service.

In the future, we might try other implementations, but for now, the focus is on understanding how OData can be implemented.

Log into the SAP Environment

Let's start by accessing the configured SAP environment using SAP Logon.

We used the pre-prepared BPINST user account. Adjust this according to your environment.

Create a Project using SAP Gateway Service Builder

After logging in, enter the transaction code "SEGW" to open the SAP Gateway Service Builder screen. We will implement the OData service here.

First, create a project for the OData service implementation.

The confusing part might be "Project Type". Annotations refer to SAP-specific metadata features that cannot be expressed in the OData metadata specification. Since we do not plan to use custom annotations, we select the default "Service with SAP Annotations" and proceed. SAP Resources below:

SAP Annotation Documentation

SAP Documentation for Creating a Service Builder Project

Add a Data Model

After creating the project, create the data model to be used in the OData service. This defines the items and structure for data exchange.

We will use the existing BNKA table, so we create it from the "DDIC Structure" available in Import.

Specify a name and select BNKA as the ABAP Structure.

Select all items from the BNKA list.

Since a Key is required for OData services, specify the necessary items as Keys. Note that this defines the Key in the OData interface and does not guarantee the uniqueness of the stored data.

This imports the data model.

It also generates templates for Create, Delete, GetEntity (Read), GetEntitySet (Query), and Update operations for the BNKA data model. However, this only creates the interface, and no implementation exists yet.

Create a Runtime Object

Next, generate the ABAP classes and runtime objects related to the interface implementation. Save the project and click the generate runtime objects button.

Specify the class names to be generated. The default suggestions are displayed, so proceed with them.

Once generated, the Runtime Artifacts are added. These define the implementation classes and the classes for registering the OData service.

In the Service Implementation, you can see the Implementation Class Name and Method Name linked to each interface. We will write ABAP code in these classes and methods to implement data processing for the interface.

Register as an OData Service

Although the internal implementation of the interface is not yet complete, let's register the service to make it requestable as an OData service. Use "/IWFND/MAINT_SERVICE" to register and manage OData services.

Move to "/IWFND/MAINT_SERVICE" and click Add Service.

Select a System Alias and click Get Services. The service registered as Runtime Artifacts in SAP Gateway Service Builder is displayed.

Select it and click Add Selected Services.

Proceed with the default registration information.

This registers the service, making it callable as an OData API. Use the "SAP Gateway Client" at the bottom left to verify.

An API Client like Postman is displayed. Click the Execute button. This confirms that the BNKA EntitySet can be called from the generated service.

You can call "/CDATA_SAMPLE_SRV/BNKASet", but since the internal implementation of GetEntitySet is not complete, you cannot retrieve actual data yet. Next, we will implement the internal logic of GetEntitySet.

Detail Setting of GetEntitySet

Return to SAP Gateway Service Builder to implement the internal logic. Right-click "GetEntitySet (Query)" in the Service Implementation of BNKA and click "Go to ABAP Workbench".

Proceed with the internal implementation from this screen. Each OData service implementation has linked classes and methods.

For GetEntitySet, the target is "BNKASET_GET_ENTITYSET" under "Methods" → "Inherited Methods". Right-click "BNKASET_GET_ENTITYSET" and select "Redefine".

This opens the internal implementation code screen for "BNKASET_GET_ENTITYSET", where you can proceed with the ABAP implementation.

Here, we will implement the logic to handle input parameters when GetEntitySet is called and pass the response according to the BNKA data model to "et_entityset".

For simplicity, we implemented the logic to select the top 10 rows from the BNKA table and pass them to "et_entityset". Click the "Insert" button and add the following line:

SELECT * UP TO 10 ROWS FROM bnka INTO CORRESPONDING FIELDS OF TABLE et_entityset.

Execute "Activate" to reflect the implementation results.

Return to SAP Gateway Client and call GetEntitySet again! If the implementation is successful, you should see the BNKA data retrieved.

You can also verify it via the browser.

Although pagination and filter conditions are not implemented, this gives an idea of how to proceed with the implementation.

Use CData SAP Netwaver Gateway Driver to access the OData Service

Finally, let's connect to the created OData service using the CData SAP NetWeaver Gateway Driver. We will use the ODBC Driver for this, but the same applies to JDBC, ADO.NET, or CData Sync.

CData SAP NetWeaver Gateway Drivers

Launch the ODBC DSN configuration screen for the CData SAP NetWeaver Gateway Driver and enter the necessary information. In the SAP S/4 HANA Private Cloud trial environment, it is published on port 50000, so specify the URL and user information. Set the Namespace to sap and the Service to "CDATA_SAMPLE_SRV".

Click "Test connection", and if the message "The connection test was successful" is shown, it has a successful connection. Move to the data model tab to see the BNKASet object. In the preview tab, you can see that 10 records are returned, confirming that GetEntitySet is called successfully!

In the Query tab, you can see that 10 records are returned, confirming that GetEntitySet is called successfully!

About the IP Address

When connecting externally, the public IP address used is for the "SAP S/4HANA 2023 FPS00 & SAP HANA DB 2.0" machine, with port number 50000.

The SAP Gateway Client connects using the DNS specified in the hosts file.

You can see the private IP address.

Setting Multiple Resources to an OData Service

OData services can include multiple resources and EntitySets in one service. For example, by adding the ADKO data model, multiple tables can be called from the CData driver.