Exploring Diverse Approaches of OData Services creation in SAP (2024)

Introduction

OData (Open Data Protocol) is a standardized protocol for building and consuming RESTful APIs (Application Programming Interfaces) that expose data resources over the web. It was developed by Microsoft and has gained widespread adoption as an open industry standard for data exchange and integration.

Creating OData services in SAP offers various approaches to cater to different development scenarios and requirements. These methods provide flexibility and allow developers to leverage existing data models, applications, and frameworks. Let's explore some of the key ways to create OData services in SAP.

In this article , we would be talking about below approaches.

  1. SEGW : Manual Approach
  2. SEGW: Import
  3. SADL
  4. SEGW: RFC
  5. Annotation based

1. SEGW : Manual Approach

Go to T-code SEGW and create new project.

Exploring Diverse Approaches of OData Services creation in SAP (1)
Exploring Diverse Approaches of OData Services creation in SAP (2)

Create Entity

Exploring Diverse Approaches of OData Services creation in SAP (3)

Enter Entity and Entity set name.

Exploring Diverse Approaches of OData Services creation in SAP (4)
Exploring Diverse Approaches of OData Services creation in SAP (5)

Expand Entity and double click on property to add field .

Exploring Diverse Approaches of OData Services creation in SAP (6)

project should look like below.

Exploring Diverse Approaches of OData Services creation in SAP (7)

Save project and generate service.

Exploring Diverse Approaches of OData Services creation in SAP (8)
Exploring Diverse Approaches of OData Services creation in SAP (9)

Runtime objects would get generated.

Exploring Diverse Approaches of OData Services creation in SAP (10)

Add service through T-code /n/iwfnd/maint_service

Exploring Diverse Approaches of OData Services creation in SAP (11)
Exploring Diverse Approaches of OData Services creation in SAP (12)

Go to T-code /n/iwfnd/maint_service , select service and click on SAP Gateway client.

Exploring Diverse Approaches of OData Services creation in SAP (13)

We can go to below screen via /n/iwfnd/gw_client as well.

Exploring Diverse Approaches of OData Services creation in SAP (14)

2.SEGW : Import

We will create Odata project through DDIC table.

DDIC table .

Exploring Diverse Approaches of OData Services creation in SAP (15)

Go to SEGW project , right click on data model > import > DDIC Structure

Exploring Diverse Approaches of OData Services creation in SAP (16)

Give Entity name and DDIC structure.

Exploring Diverse Approaches of OData Services creation in SAP (17)

select desired field and finish.

Exploring Diverse Approaches of OData Services creation in SAP (18)

After save , project would look like below.

Exploring Diverse Approaches of OData Services creation in SAP (19)

Generate runtime object and Go to T-code /n/iwfnd/GW_CLIENT to check the service .

We can see there is another entity has been added (imported from DDIC ).

Exploring Diverse Approaches of OData Services creation in SAP (20)

3. SEGW : SADL Approach

We will use below CDS to create OData service. We can leverage benefits of CDS annotations in service.

Exploring Diverse Approaches of OData Services creation in SAP (21)

Go to SEGW project , Right Click on data model >> Reference >> Data source.

Exploring Diverse Approaches of OData Services creation in SAP (22)

Provide CDS name and proceed further.

Exploring Diverse Approaches of OData Services creation in SAP (23)

Project would look like below.

Exploring Diverse Approaches of OData Services creation in SAP (24)

Generate service and check service in /N/IWFND/GW_CLIENT. There is another entity set has been generated from CDS view.

Exploring Diverse Approaches of OData Services creation in SAP (25)

4. SEGW : RFC

We can create OData service on RFC enabled Function module and make use of the FM functionality through service.

We have created a custom RFC FM which has importing parameter VBELN and returns VBELN,AUART and LIFSK .

Exploring Diverse Approaches of OData Services creation in SAP (26)
Exploring Diverse Approaches of OData Services creation in SAP (27)

Create SEGW project.

Exploring Diverse Approaches of OData Services creation in SAP (28)

Right click on data model ,Import >>RFC/BOR Interface.

Exploring Diverse Approaches of OData Services creation in SAP (29)

Provide entity name and RFC enabled FM to proceed.

Exploring Diverse Approaches of OData Services creation in SAP (30)

Select required parameters.

Exploring Diverse Approaches of OData Services creation in SAP (31)

Mark suitable field as key and finish.

Exploring Diverse Approaches of OData Services creation in SAP (32)

Project would look like below.

Exploring Diverse Approaches of OData Services creation in SAP (33)

Now we need to map the service to the FM to get the data. Expand Service implementation and right click on getEntity(Since we are reading data from FM) and click map data source .

Exploring Diverse Approaches of OData Services creation in SAP (34)

Provide FM details.

Exploring Diverse Approaches of OData Services creation in SAP (35)

Propose mapping ,framework will auto map the importing and exporting fields.

Exploring Diverse Approaches of OData Services creation in SAP (36)
Exploring Diverse Approaches of OData Services creation in SAP (37)

As of now we have generated design time objects , lets Generate runtime objects.

Exploring Diverse Approaches of OData Services creation in SAP (38)
Exploring Diverse Approaches of OData Services creation in SAP (39)

Since we have mapped the data source to GET_ENTITY method , framework has generated code accordingly. Lets check DPC_EXT class.

Exploring Diverse Approaches of OData Services creation in SAP (40)
Exploring Diverse Approaches of OData Services creation in SAP (41)

Next step is to register the service and test .

Go to T-code /N/IWFND/MAINT_SERVICE

Exploring Diverse Approaches of OData Services creation in SAP (42)
Exploring Diverse Approaches of OData Services creation in SAP (43)

Service has been generated . OData is a complete framework , we can create and test OData service within SAP system itself .

We can go to SAP gateway system to test the service (/N/IWFND/GW_CLIENT).

Exploring Diverse Approaches of OData Services creation in SAP (44)

We have pass VBELN through URI and service has executed FM and return the data.

Exploring Diverse Approaches of OData Services creation in SAP (45)

5. Annotation Based

We can create Odata service directly from CDS view with the help of annotation 'Odata.publish:true' .

Exploring Diverse Approaches of OData Services creation in SAP (46)

If we notice ADT is showing warning that service is not active.

Exploring Diverse Approaches of OData Services creation in SAP (47)

Go to T-code /N/IWFND/MAINT_SERVICE and add the service .

Exploring Diverse Approaches of OData Services creation in SAP (48)
Exploring Diverse Approaches of OData Services creation in SAP (49)

Odata service is activated now.

Exploring Diverse Approaches of OData Services creation in SAP (50)
Exploring Diverse Approaches of OData Services creation in SAP (51)

Important URIs

A URL used by an OData service has at most three significant parts: theservice root URL,resource pathandquery options. Additional URL constructs (such as a fragment) MAY be present in a URL used by an OData service; however, this specification applies no further meaning to such additional constructs.

Exploring Diverse Approaches of OData Services creation in SAP (52)

  • To get the Metadata information

/sap/opu/odata/sap/ZGW_PO_SRV/$metadata 

  • URI to READ

/sap/opu/odata/sap/ZGW_PO_SRV/POHeaderSet(‘4500002012’). 

  • How to pass multiple fields of the Composite Primary Key?

/sap/opu/odata/sap/ZWM_ODATA_SRV/ZZCALIBRATE_JSON_Set(CMSASSET=’CMS456788879′,LAST_CAL_DATE=’20160923′,CAL_DUE_DATE=’20230923′) 

  • Filter Option (look for IV_FILTER_STRING in the GET_ENTITYSET method).

/sap/opu/odata/sap/ZWM_ODATA_SRV/ZZCALIBRATE_JSON_Set?&$filter=CMSASSET eq ‘CMS45678887 

  • READ one particular field or property of the Entity Type (/Field1) (Reading plant of specific material)

/sap/opu/odata/sap/ZGW_PO_SRV/POHeaderSet(‘4500002013’)/Lifnr. 

  • Count the number of entries in the system ($count)

/sap/opu/odata/sap/ZGW_PO_SRV/POHeaderSet/$count /sap/opu/odata/sap/ZGW_PO_SRV/POHeaderSet(‘4500002012’)/$count 

  • Extract only some fields of the Entity Type ($select=Field1,Field2,Field3) (Projection)

/sap/opu/odata/sap/ZGW_PO_SRV/POHeaderSet?$select=Ernam,Bukrs,Ebeln. 
/sap/opu/odata/sap/ZGW_PO_SRV/POItemSet?$select=Ebeln,Ebelp,Werks&$format=json 

  • URI to send non-Key Filters in the OData Service – ?&$filter=Xblnr eq ‘RON MILLS 2’

/sap/opu/odata/sap/ZGW_VIM_APP_SRV/DocDetailSet?&$filter=Xblnr eq ‘RON MILLS 2’ 

  • URI to send multiple Filters in the OData Service

?$filter=Erdat gt datetime’2015-01-01T00:00:00’and Ekgrp eq ’17’and Werks eq ‘4030’ 
/sap/opu/odata/sap/ZGW_PREQ_SRV/PReqSet?$filter=Erdat gt datetime’2015-01-01T00:00:00’and Ekgrp eq ’17’and Werks eq ‘4030’ 
Association specifies the cardinality between Entity Types. When an Association is created,a Navigation Property of the Entity Type is also generated.

if we execute below query to get one data set , system will show us (link href ) that we can make use of navigation property as well.

it will show the code as well.

/sap/opu/odata/sap/ZGW_PO_SRV/POHeaderSet(‘4500002012’<link href=POHeaderSet(‘4500002012’)/HeadtoItemNav) 

  • $Expand : will fetch both item and header details using navigation property

/sap/opu/odata/sap/ZGW_PO_SRV/POHeaderSet(‘4500001729’)?$expand=HeadToItemNav 

whereas below URL will fetch only item details

/sap/opu/odata/sap/ZGW_PO_SRV/POHeaderSet(‘4500001729’)/HeadToItemNav 

Conclusion and further resources

This guide has given an overview of OData services and walked you through the step-by-step process of creating a service using SAP.

If you're looking for more information, check out the official SAP documentation on OData service creation, the OData official website, and the OData community resources. Thanks for reading!

Exploring Diverse Approaches of OData Services creation in SAP (2024)
Top Articles
Latest Posts
Article information

Author: Horacio Brakus JD

Last Updated:

Views: 6660

Rating: 4 / 5 (71 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Horacio Brakus JD

Birthday: 1999-08-21

Address: Apt. 524 43384 Minnie Prairie, South Edda, MA 62804

Phone: +5931039998219

Job: Sales Strategist

Hobby: Sculling, Kitesurfing, Orienteering, Painting, Computer programming, Creative writing, Scuba diving

Introduction: My name is Horacio Brakus JD, I am a lively, splendid, jolly, vivacious, vast, cheerful, agreeable person who loves writing and wants to share my knowledge and understanding with you.