How to Create SAP OData

by | Aug 14, 2019 | ODATA

Preface – This post is part of the SAP ABAP OData Tutorial series.

Introduction

In our previous article, we have already discussed what an OData is. In this article we will go through step by step process to create a SAP OData service. Later on, we will register our service and also test if it is working fine. In this article, we will not write any code related to CRUD (create, write, update and delete). To learn CRUD in OData, click here.

Step by Step Process to create SAP OData service

In this section we will follow multiple predefined steps to create an OData, these are:

  1. We will use SAP Service Gateway Builder (SEGW) to create a new project
  2. We will Import DDIC/CDS or other Structure to create an OData Model
  3. We will Generate our OData
  4. We will Register and test our Service

Create a new Service Builder project

To create a new project, follow the given steps:

  1. Go to transaction code SEGW, it will start the Gateway Service Builder.
  2. Click on the Create Project button, as shown below:
  3. Enter the given information
FieldDescriptionExample
ProjectIt is the name of your SAP OData Service. It will start with Z or YZPROJECT_NAME
DescriptionEnter a description for your ODataDemo OData Project
Project TypeSAP Provides given four Project Options:

1.       Service with SAP Annotations

2.       Service with Vocabulary-Based Annotations

3.       Annotation Model for Referenced Service

4.       OData 4.0 Service

We will learn about them in details, later.

Service with SAP Annotations

 

Generation StrategyIt will be preloaded as Standard which means, it has all standard configurations.Standard
PackageEnter your project package or temporary package i.e. $TMP$TMP
Person ResponsibleIt will be preloaded with your User ID/ User NamebarryAllen

OData creation

  1. Press Continue and Save.

Import a DDIC structure

To use an OData Service, it should be designed in such way that it will read/update one or more table. Therefore, we need to import any table, structure or view. These are part of DDIC structure. We can also import Data model from file, RFC/ BOR Interface or a search help. But in this section, we will learn how to import a DDIC structure:

  1. Click on the Data Model under the project we have recently created to expand it.
  2. Right Click on Data Model and click Import and then click DDIC Structure
    OData DDIC Import
  3. Now, a Wizard will open. Enter the following details, there:
FieldDescriptionExample
NameEnter a name of Model. It should be explanatory.CustomerData
TypeWe have two options here:

1.       Entity Type: This means a simple DDIC structure/table/view will be imported

2.       Complex Type: This means a complex DDIC structure/table/view will be imported

Choose Entity Type
ABAP StructureChoose your required table or structure name. Just copy and paste the name of your DDIC table/structure/viewzcustomer_data
Create Default Entity SetIt will create an Entity Type with the same fields as of Entity. Also, the name will remain same. We recommend you to leave it check marked.Leave it check marked

 

The above details are shown below:
OData Entity Set Creation

  1. In the next step, the wizard will show all the fields of the imported table. Just select them all and click next as shown below:
    OData Entity Set Creation2
  2. In the next step, the wizard will show all the checked fields. Here you need to check the primary keys and finish, as shown below:
    OData Entity Set Creation3

 

Generate OData Service

Now, we have created an OData object with a model. Now to use this Object we need to generate all the required classes. To generate, press the Generate Runtime Objects  OData Generate Icon button, as shown below:

OData Generate Runtime Objects

Note: The Generate Runtime Objects button automatically saves the project before generating classes.

Pressing the above button, will open a dialog box as shown below:

OData Class Generation

Just press Continue, as shown above. The above process will generate following classes:

Class NameDescription
MPC ClassMPC stands for Model Provider Class. This is generated and refreshed every time, we generate a class. It is not recommended to extend this class for Model Annotations, because all the methods are deleted whenever the OData is generated.
MPC EXT ClassThis is the Extended MPC class, and all the methods are conserved even the OData is generated.
DPC ClassDPC stands for Data Provider Class. This is generated and refreshed every time, we generate a class. It is not recommended to extend this class for CRUD operation, because all the methods are deleted whenever the OData is generated.
DPC EXT ClassThis is the Extended DPC class, and all the methods are conserved even the OData is generated.
MDL ClassThe is the Model Class.
SRV ClassThis is the Service Class.

 

Just click on Lock Object to generate the classes, as shown below:

OData Service Generation

Register SAP OData using the Gateway client

Once the OData is generated, now it is the time to register the service. Once the service is registered, then only it can be used anywhere.

To generate an OData, follow the given steps:

  1. Double click the node Service Maintenance
  2. Select any System and click Register as shown below:
    OData Service Register
  3. Enter an Alias, if asked as shown below:
    OData System Alias
  4. Now, in the next screen you get following options:
FieldDescription
Technical Service NameThis is auto generated service name
Service VersionIt is auto generated service version
DescriptionIt is the Description of the OData you have mention earlier
External Service NameIt is same to the auto generated service name
NamespaceIt is by default bank
External Mapping IDIt is by default Empty
External Data Source TypeIt is by Default set to source type C
Technical Model NameIt is auto generated Model Name
Model VersionIt is auto generated Model Version
Package AssignmentAssign your package name here, or just choose Local Object from the button below. It will be then $TMP
ICF NodeIt is by default “Standard Mode”
oAuth EnablementIt provides option to add extra authentication, in case you need oAuth2 enablement choose it, else leave it blank

 

In our case, we just need to choose package and click confirm, as shown below:

OData Service Registration

Once the service is registered, the Registration will turn green, as shown below:

OData Service Status

Test the OData service

  1. To test the OData service, click the SAP Gateway Client button, as shown below:OData Test Service
  2. It will open a new screen, here click Add URI Option as shown below:
    OData Add URI Option
  3. Choose $metadata, here:
    OData $metadata
  4. Click Execute as shown below:
    OData Service Status 200

If the status is 200, as shown above, then it means your OData is working fine.

In upcoming articles, we will learn how to perform CRUD operation using OData.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.