You are on page 1of 23

ODATA: The Open Data Protocol (OData) is REST based service protocol for simplified

access to SAP data.


REST: REpresentational State Transfer (REST) is not a kind of protocol, but an
architectural style builds on top of HTTP defining below set of constraints –
• Client Server architecture
• Statelessness
• Cache-ability
• Layered system
• Uniform Interface
• Code on demand
HTTP: It is a protocol used for communication, usually used to communicate with
internet resources or any application with a web browser client.
Here are the different ways we create OData service in SAP –
SAP Gateway Based OData Service –
Below steps concludes with the completion of OData service creation using the T-code: SEGW, including
the generation of runtime artifacts, registration, and activation of the service. Additionally, it
emphasizes the importance of redefining methods for header and item entity sets, loading metadata,
and thoroughly testing entity sets with test data to ensure functionality. This is a classic SEGW based
OData service creation way. Content is taken from a blog written by Laxmi Jibhe.
Step 1. Go to transaction code - SEGW.

Click on Icon Create. A pop window will appear, Fill the details as per below mention in screen shot and
click on check icon or enter.
Below screen will appear where you can see below folder in project. In Folder data model, we can see
three sub folders. Entity Type - it acts as work area, Entity Sets -It act as internal table and associations.

Now we are going to define structure of work area and internal table, Right click on Data model select
import and select DDIC structure .
Here give the details of structure and structure name as per below screen below. Select radio button
Entity type and click on check box entity set. fill ABAP structure as VBAK and click on next.

Now you will get the pop-up screen with VBAK table fields name. Select fields for your structure and
click on next.
Another window will appear where we must select the key field. tick on VBELN as key field and click on
finish. You may get warning message; it can be totally ignored.

Now expand folder entity type and fill the below parameters in properties, please don't tick Null check
box for VBELN as it is key field.

Similarly Create Structure for SD Item following same steps


Click on Save button at the top of menu bar.

Now click on generate Icon, A pop up window will appear with class details, click on tick icon, and
proceed further. It will ask for package, give the details, and proceed further. These are also knowns as
runtime artifacts.
Step 2. Go to transaction code /IWFND/MAINT_SERVICE and click on push button - Add services.

Next screen will appear, give the alias name, and execute, Search for your project and double click on
your project name.
A pop-up window will appear, enter the package details, and click on tick icon. An Information message
will be shown where it will confirm about the service is created and metadata loaded successfully.
Now click back and go to main screen of transaction /IWFND/MAINT_SERVICE and find your service.
Click on SAP Gateway Client.

A new screen will come, Execute the transaction, and check the response.

Status code 200 means that the OData service we created works well and now, we will write code to
get data.

Step 3. Go to transaction code SEGW and expand folder service Implementation.

Under service implementation - Expand SDheaderSet. you will find different options. Right click on
GetEntitySet and select GO to ABAP work Bench. It will redirect to the class.
Now expand the method drop down and select method for GetEntitySet for Header data and right click
and select redefine.
Write below code in method. Save and activate all related objects for classes.

Similarly, Write the code for GetEntitySet for Item and activate.

Step -4: Go to transaction /IWFND/MAINT_SERVICE and find out you service, select your service and click
on load metadata.
Information message will pop up - Metadata has been loaded successfully. Now Click on SAP Gateway
Client.

Now Click on Entity set.

Select entity for header and enter -


Click and execute and you will get the header data in response.

Similarly, select entity set for item and execute. You will get data for item

That’s all. We have completed our Odata service creation with a Service Gateway Builder Project with
two entities and two entity-sets. We have generated the runtime artifacts and registered and activated
our OData service. We had redefined methods of header and item entity set, load metadata and test the
entity set with for test data.
SAP Gateway Based OData Service using SADL Framework –
Let’s go with the steps on how to create an OData service using the Service Adaptation Description
Language (SADL) framework and referencing SAP ABAP CDS views in SEGW Gateway Builder. Content is
taken from a blog written by Vishal Kumar.

We will use a standard CDS view - A_BILLINGDOCUMENT.

Create an empty project in Service Gateway Builder using the T-code: SEGW. To create the Data Model,
we need to refer to Data Source. (CDS View in this case).
In the next screen, we can see all the elements of CDS entity and corresponding Associations.
Select all the elements and proceed further. (We can select only the fields which are relevant for as per
Business Need and we always have the option to edit that later).

After completing this step all the Entity, Entity Sets, Association & corresponding Navigation is
automatically generated in the Gateway Project.
Now, generate the service. After generating Register and Maintain the service. Test the OData service in
Gateway Client (/IWFND/GW_CLIENT).
We can see all the Entity and corresponding Association in the Metadata Information.
We can execute the service with different Entities to see the results.
Publishing CDS View as OData Service –
This method is also referred as annotation-based approach using CDS views. Content is taken from a
blog written by Jagdish Patil.

Create a CDS View with the below code. The annotation @OData.publish: true is used to publish the
CDS as OData Service.

@AbapCatalog.sqlViewName: 'ZJP_SO_DATA_N'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Sales Order Data CDS'
@OData.publish: true
define view ZJP_SO_NEW
as select from vbak as header
association [1..*] to vbap as _item
on header.vbeln = _item.vbeln
{
key vbeln as Vbeln,
erdat as Erdat,
erzet as Erzet,
ernam as Ernam,
auart as Auart,
netwr as Netwr,
waerk as Waerk,
vkorg as Vkorg,
vtweg as Vtweg,
spart as Spart,
kunnr as Kunnr,
_item
}

Activate the CDS View created. Post Activation, a yellow warning symbol appears beside the annotation
@OData.publish: true. Hover the cursor over the line to get a pop up like below.

The Service Name appears in the pop-up and the warning says that the Service is not active. The service
name will be the CDS View name followed by _CDS.
Register the service ZJP_SO_NEW_CDS using transaction /IWFND/MAINT_SERVICE. Use the add service
button.
And the service is registered. Now, Test the Service. Use Filter to get the registered service. Start from
the transaction /IWFND/MAINT_SERVICE. Click on the Filter button.

Use the exact service name or something like ZJP* to filter the services. Click on the right service and
use SAP Gateway Client to test the service.
This service has 2 sets, ZJP_SO_NEW and VBAP. VBAP can be called directly or with Association.

URIs to test

/sap/opu/odata/sap/ZJP_SO_NEW_CDS/ZJP_SO_NEW?$format=json

/sap/opu/odata/sap/ZJP_SO_NEW_CDS/ZJP_SO_NEW(‘1’)?$format=json

/sap/opu/odata/sap/ZJP_SO_NEW_CDS/ZJP_SO_NEW(‘1’)/to_item?$format=json

/sap/opu/odata/sap/ZJP_SO_NEW_CDS/VBAP?$format=json

/sap/opu/odata/sap/ZJP_SO_NEW_CDS/ZJP_SO_NEW(‘1’)?$expand=to_item&$format=json

An example response with Read operation testing is shown as below.


SAP RAP Based OData Service –
SAP RAP, or the ABAP RESTful Application Programming Model, is a framework designed for efficient
development within the SAP ecosystem, particularly for building enterprise SAP Fiori Apps and Web
APIs. It's optimized for SAP HANA on SAP Business Technology Platform (BTP) ABAP Environment and
SAP S/4HANA, accommodating both greenfield and brownfield implementations. RAP offers a
streamlined approach to development, providing standardized architecture, native testability, and
supportability. It supports various service types, including OData-based services for UI development and
Web APIs, as well as analytical and SQL-based services.

To know how to develop an OData service based on SAP RAP, follow the series of blogs by Jagdish Patil.

SAP CAPM Based OData Service –


SAP CAP, the SAP Cloud Application Programming Model, is a framework for building powerful business
apps easily. It simplifies app development by offering tools and best practices, saving time and
improving quality. CAP handles tasks like database interactions, creating web services, handling events,
and security features like authentication. It's useful for various scenarios, from building new apps to
extending existing ones. With CAP, developers can focus more on solving business problems and less on
coding details.

To know how to develop an OData service based on SAP CAPM, check out this video from Anubhav
Oberoy.

You might also like