Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Introduction
Scenario
Procedure
Coding
1) Query Operation
2) Read Operation
3) Create Operation
4) Update Operation
5) Delete Operation
6) Function Import
Closing Remarks
Introduction
In this blog I will explain creation of simple SAP NW GW OData service which will implement Create, Read, Update,
Delete, Query and Function Import operations.
Just to make it very simple, I will just have single entity and entity set. Here I will perform these operations on Z table.
In real scenarios, you will use BAPIs. RFCs to perform these operations.
I recommend to read below documents and SAP documentation for deep understanding.
How to Develop a Gateway Service using Code based Implementation by Andre Fischer
How to Develop Query Options for an OData Service Using Code-Based Implementationby Andre
Fischer
How to Write an OData Channel Gateway Service. Part 2 - The Runtime Data Provider Class
SAP Help - SAP NetWeaver Gateway Foundation Developer Guide - SAP NetWeaver Gateway Foundation
(SAP_GWFND) - SAP Library
Code Snippet - 3.2 Data Provider Class (DPC) - SAP NetWeaver Gateway Foundation (SAP_GWFND) - SAP
Library
Scenario
We have User information table as ZUSERINFO containing below fields
Now let's create OData service which will insert, read, delete, update and query this table along with one custom
operation (UsersByCountry) as function import.
From an ABAPer perspective, This is what meant by OData operations.
OData
Operation
HTTP
Method
Create
POST
Read
GET
Update
PUT/PATCH
Delete
DELETE
Query
GET
Function
Import
GET/POST
We can correlate Entity Set as Table Type, Internal table and Entity to work area, structure!
Entity Set
Entity
Procedure
Lets go to transaction SEGW and create project as ZUSERINFO.
Now right click on Data Model and Import --> DDIC Structure option, it will display popup window. Provide DDIC
structure name. In this case table name ZUSERINFO. It will propose field and key mapping as well as object name
which will be your entity name.
We will ignore MANDT as key field and also overwrite object name. I want my field names in upper camel case
format so I will change it accordingly. Lets call entity type as User. It will look as below. Press enter.
Now lets create Entity Set as UserCollection (or UserSet or Users). You can refer Creating High-Quality
OData Services - SAP NetWeaver Gateway Foundation (SAP_GWFND) - SAP Library
I will go with UserCollection as my entity set name.
Right click folder name Entity Sets and click create. Provide entity set name as UserCollection and Entity Type name
as User. It will display as below.
Now lets generate runtime artifacts. Click on generate runtime objects button. It will display popup as below. Keep
the default class names as-is and click on enter button.
On successful generation, you will see this kind of message log and generated artifacts. 4 classes will get generated.
2 for Data provider and 2 for Model provider.
Now register your service under service Maintenance folder. Click on Register button. Keep default values as-is
and hit enter button.
On successful registration, click Maintain button. This will open service catalog window along with option to call
Gateway Client. Click on Gateway Client button to test the service. (you can also call transaction
/IWFND/GW_CLIENT to open SAP NW Gateway client)
Append $metatda to base service URL and press execute button. If everything is fine then you will HTTP
Response as below. Metadata provides information such as Entity type, key property, properties and Entity Set name.
So far we just defined single entity type and entity set. Now its time to code CRUDQ and function import methods.
Coding
There is no specific order to implement these methods but it is always good to 1st implement query and read
operation as for Create and Update, you will need request data which you will get if you already have query/read
implemented.
1) Query Operation
First we will start implementing query operation. Before that, I will add one record in my ZUSERINFO table as
Now open Runtime artifacts folder and right click on Class ZCL_ZUSERINFO_DPC_EXT and select Go to ABAP
Workbench option. Select edit mode and redefine method USERCOLLECTION_GET_ENTITYSET.
In the simplest form, this is what minimal coding will look like in GET_ENTITYSET method.
METHODusercollection_get_entityset.
DATA:lt_userinfoTYPETABLEOFzuserinfo,
ls_userinfoLIKELINEOFlt_userinfo,
ls_entityLIKELINEOFet_entityset.
*Fill ET_ENTITYSET
LOOPATlt_userinfoINTOls_userinfo.
ls_entityuserid = ls_userinfouserid.
ls_entityfirstname = ls_userinfofirstname.
ls_entitylastname = ls_userinfolastname.
ls_entityemail
= ls_userinfoemail.
ls_entityphone
= ls_userinfophone.
ls_entitycountry= ls_userinfocountry.
APPENDls_entityTOet_entityset.
ENDLOOP.
ENDMETHOD.
We are selecting all data from table ZUSERINFO and appending the result to exporting parameter ET_ENTITYSET.
Now you can go to GW client transaction and execute
URI /sap/opu/odata/sap/ZUSERINFO_SRV/UserCollectionwhich will display one record which we already added
into Z table.
Observe the method signature. put external breakpoint in method and execute query. You will find important
information in method parameters in debugging mode.
Below table will provide brief explanation of method parameters, alternative approach to get the value of those
method parameters. Last column specifies if we need to code to implement query operation.
Here IO_TECH_REQUEST_CONTEXT refers to /IWBEP/IF_MGW_REQ_ENTITYSET
OData Query
UserCollection?$filter=UserID eq
'123' and LastName eq 'Mahajan'
Method Parameter
Coding
require
d to
imple
ment
Query
Operat
ion
OData Query
UserCollection?
$select=FirstName,LastName
Method Parameter
Coding
require
d to
imple
ment
Query
Operat
ion
No method parameter
IT_ORDER
IV_SEARCH_STRING
UserCollection?
$orderby=FirstName,LastName
OR
UserCollection?
$orderby=FirstName
desc,LastName desc
UserCollection?search='test'
UserCollection?$top=1
IS_PAGING-TOP
2) Read Operation
Now lets implement GET_ENTITY method. Go to method USERCOLLECTION_GET_ENTITY and redefine it. Below
is the minimal code that we need to have in this method.
We need to read the key values passed from query URI and then fill the export parameter ER_ENTITY.
METHODusercollection_get_entity.
DATA:ls_key_tabTYPE/iwbep/s_mgw_name_value_pair,
lv_useridTYPEzuserinfouserid,
ls_userinfoTYPEzuserinfo.
lv_userid = ls_key_tabvalue.
IFsysubrc = 0.
er_entityuserid = ls_userinfouserid.
er_entityfirstname = ls_userinfofirstname.
er_entitylastname = ls_userinfolastname.
er_entityemail
= ls_userinfoemail.
er_entityphone
= ls_userinfophone.
er_entitycountry = ls_userinfocountry.
ENDIF.
ENDMETHOD.
Here IO_TECH_REQUEST_CONTEXT refers to /IWBEP/IF_MGW_REQ_ENTITY
Method
Parameter
OData Query
Coding
require
d to
imple
ment
Query
Operat
ion
UserCollection(UserID='Test') OR
UserCollection('Test'
IT_KEY_TAB
UserCollection('Test')?
$select=FirstName,LastName
No
UserCollection('Test')?$format=json
No
No
No
Also note that you cannot use system query options else you will get an error as System query options
'$orderby,$skip,$top,$skiptoken,$inlinecount,' are not allowed in the requested URI
3) Create Operation
Now we will focus on create operation by redefining method USERCOLLECTION_CREATE_ENTITY. Below is the
code that will perform POST operation.
Here we are reading the request data and then filling the exporting parameter ER_ENTITY.
METHODusercollection_create_entity.
DATA:ls_request_input_dataTYPEzcl_zuserinfo_mpc=>ts_user,
ls_userinfoTYPEzuserinfo.
ls_userinfoemail= ls_request_input_dataemail.
ls_userinfophone= ls_request_input_dataphone.
ls_userinfocountry= ls_request_input_datacountry.
OData Query
Method Parameter
Coding
required
to
implem
ent
Query
Operati
on
DATA: ls_request_input_dataTYPEzcl_zuserinfo_mpc=>ts_
/
user.
sap/opu/odata/sap/ZUSERINFO_SRV/UserColl io_data_provider->read_entry_data(IMPORTING es_data
Yes
ection
=ls_request_input_data ).
In case of Create operation, you cannot use system query options else you will get an error as
The Data Services Request contains SystemQueryOptions that are not allowed for this
Request Type
This error message comes from method PROCESS_ENTITY_SET (/IWCOR/CL_DS_PROC_DISPATCHER). Just in
case if you are curious and want to know
4) Update Operation
Now we will implement update operation by redefining method USERCOLLECTION_UPDATE_ENTITY. We need to
put below code in this method.
METHODusercollection_update_entity.
DATA:ls_request_input_dataTYPEzcl_zuserinfo_mpc=>ts_user,
ls_key_tabTYPE/iwbep/s_mgw_name_value_pair,
lv_useridTYPEzuserinfouserid,
ls_userinfoTYPEzuserinfo.
OData Query
Method Parameter
UserCollection('Test1
')
IT_KEY_TAB
Coding
required
to
impleme
nt Query
Operatio
n
Yes
OData Query
Method Parameter
Coding
required
to
impleme
nt Query
Operatio
n
Yes
5) Delete Operation
To implement Delete operation, you need to execute DELETE HTTP method for particular key. Below is the code for
method USERCOLLECTION_DELETE_ENTITY.
Here we are reading key value of the record to be deleted and executing Delete statement.
METHODusercollection_delete_entity.
DATA:ls_key_tabTYPE/iwbep/s_mgw_name_value_pair,
lv_useridTYPEzuserinfouserid.
* Read key values
READTABLEit_key_tabINTOls_key_tabWITHKEYname = 'UserID'.
lv_userid = ls_key_tabvalue.
IFlv_useridISNOTINITIAL.
* Delete record from table ZUSERINFO
DELETEFROMzuserinfoWHEREuserid = lv_userid.
ENDIF.
ENDMETHOD.
On successful record deletion, you will see HTTP response code as 204.
OData Query
Method
Parameter
UserCollection('Test1') IT_KEY_TAB
6) Function Import
Coding required to
implement Query
Operation
As per SAP documentation, Function Imports - SAP NetWeaver Gateway Foundation (SAP_GWFND) - SAP
Library
The Open Data Protocol (OData) includes standard CRUD (Create, Retrieve, Update, and Delete) operations that
map to the HTTP methods POST, GET, PUT/MERGE, and DELETE.
In addition, OData supports further service operations (function imports) that can be invoked by the HTTP methods
GET or POST for anything that cannot be mapped to the standard CRUD operations. You can implement such
additional service operations in the Service Builder by creating function imports within your data model.
For example, you could create function imports for the following custom operations:
And we want to get users by specific country then we can implement function import. Lets call our function import as
UsersByCountry!
Right click on Data model and create function import. Provide proper name. Again refer Creating High-
Quality OData Services - SAP NetWeaver Gateway Foundation (SAP_GWFND) SAP Library
Provide required details such as mentioned below. Remember here we want to return collection of users and hence
we selected Return cardinality as 0..n with return entity set and HTTP method type as GET.
Now click on Function Import parameters and create import parameters as shown below. In this case we just want to
pass value of country and hence we will have one parameter as Country.
Finally save project, check project consistency and generate runtime objects. To check if everything is fine, in GW
client execute service metadata URL as /sap/opu/odata/sap/ZUSERINFO_SRV/$metadata which should show you
function import definition in metadata as below,
Now we will implement function import operation. Go to DPC_EXT class and redefine method
/IWBEP/IF_MGW_APPL_SRV_RUNTIME~EXECUTE_ACTION.
Put below code to implement function import.
METHOD/iwbep/if_mgw_appl_srv_runtime~execute_action.
DATA:ls_parameterTYPE/iwbep/s_mgw_name_value_pair,
lv_countryTYPEstring,
lt_userinfoTYPETABLEOFzuserinfo,
ls_userinfoTYPEzuserinfo,
ls_entityTYPEzcl_zuserinfo_mpc=>ts_user,
lt_entitysetTYPEzcl_zuserinfo_mpc=>tt_user.
IFlv_countryISNOTINITIAL.
SELECT*FROMzuserinfoINTOTABLElt_userinfoWHEREcountry =lv_country.
LOOPATlt_userinfoINTOls_userinfo.
ls_entityuserid= ls_userinfouserid.
ls_entityfirstname = ls_userinfofirstname.
ls_entitylastname= ls_userinfolastname.
ls_entityemail= ls_userinfoemail.
ls_entityphone= ls_userinfophone.
ls_entitycountry= ls_userinfocountry.
APPENDls_entityTOlt_entityset.
ENDLOOP.
* Call methos copy_data_to_ref and export entity set data
copy_data_to_ref(EXPORTINGis_data = lt_entityset
CHANGINGcr_data = er_data).
ENDIF.
ENDIF.
ENDIF.
ENDMETHOD.
OData Query
Method
Parameter
/
sap/opu/odata/sap/ZUSERINFO_SRV/UsersByCo IT_PARAME
untry?Country='US'
TER
Coding
requir
ed to
imple
ment
Query
Operat
ion
Closing Remarks
This is very simple example of an OData service. In real case scenarios, you will have multiple entities,
relationship, association and navigation between them but when it comes to coding in DPC_EXT class
methods, you will find above explanation of each method, parameters and alternative way to get the
parameter values useful.
I hope you enjoyed reading this blog and now ready to develop your OData service! I request you to put
comments/suggestions. Please feel free if you have any different thought to improve any part of this blog
as well.
Happy Learning & Coding