Tuesday, 15 January 2019

Expose CDS Views as OData Service

There are 3 different ways you can expose CDS views as OData service:

1. Import DDIC structure using SEGW Netweaver Gateway service builder transaction.
2. Reference Data Source using SEGW Netweaver Gateway service builder transaction.
3. Using Generic Annotations. (@Odata.publish:true)

First way is supported from SAP ABAP NW 7.40 SP5, Second and third ways are supported from SAP ABAP NW 7.50 and above. For first two ways SAP NW Gateway is used and for third type without use of SAP NW Gateway odata services can be created using annotations. Gateway is used only to add the service created using annotations.

In this blog I am going to show type one i.e how to generate OData service using import DDIC structure using SEGW service builder transaction.

How to Create CDS View:


Firstly as a prerequisite ABAP Development tools(ADT) needs to be installed in Eclipse to create CDS Views.

On installation of ADT in eclipse open ABAP perspective as shown below:

SAP ABAP Tutorial and Material, SAP Certification, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Study Materials

After opening ABAP perspective, In project explorer right click in the context menu select New->ABAP Project. A popup appears asks for ABAP system details which you need to add to create CDS views. Details like System-ID, Username, Password has to be given and click on OK to add the system to eclipse. Note that ABAP system which you add should support SAP ABAP NW 7.40 package. find the attachment for how to add ABAP system to eclipse.

Once after adding the ABAP system project explorer looks like below:

SAP ABAP Tutorial and Material, SAP Certification, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Study Materials

Expand $TMP package, Select and right click on Dictionary folder in the context menu to create DDL source ABAP Repository Object

SAP ABAP Tutorial and Material, SAP Certification, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Study Materials

Select DDL source, click on next and give below details to create sample CDS view:

SAP ABAP Tutorial and Material, SAP Certification, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Study Materials

Without clicking on finish, on click on next CDS view ask for transport:

SAP ABAP Tutorial and Material, SAP Certification, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Study Materials

since CDS view is create in local object no transport is required, click on next then sample templates are populated to select:

SAP ABAP Tutorial and Material, SAP Certification, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Study Materials

You can use any of the template as shown above to create CDS view. Template helps you to create different types of CDS views with syntax auto populated on selecting the type of view. Here I am creating simple CDS view so I select define view template and click on finish.

SAP ABAP Tutorial and Material, SAP Certification, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Study Materials

In the define view template core annotations are populated with basic syntax to create view, define the view as below:

SAP ABAP Tutorial and Material, SAP Certification, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Study Materials

Here I defined sql view name as ‘CDS_DDL_SAMPLE1’ and selected data from “snwd_so” sales order data source with some columns. After defining the view, Activate the view as shown above. On Activation two objects are created:

1. CDS Database View (CDS_DDL_SAMPLE1)
2. CDS entity. (ZCDS_SAMPLE1)

CDS Database view can be accessed using SE11 ABAP Dictionary as shown below:

SAP ABAP Tutorial and Material, SAP Certification, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Study Materials

Give CDS Database View name as shown above and click on display.

SAP ABAP Tutorial and Material, SAP Certification, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Study Materials

DDL SQL view is created in ABAP Dictionary. Here you can identify MANDT field in the view definition though we didnt mention it explicitly while defining the view. CDS views are default client dependent no need to mention client field while defining the view.

Right click on CDS View created to preview the data as shown below:

SAP ABAP Tutorial and Material, SAP Certification, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Study Materials

Generate OData service using CDS View:


Go to SEGW transaction. Create project as shown below:

SAP ABAP Tutorial and Material, SAP Certification, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Study Materials

Give project details as below:

SAP ABAP Tutorial and Material, SAP Certification, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Study Materials

Save project in local object. Expand project node, right click on data model->import->DDIC structure:

SAP ABAP Tutorial and Material, SAP Certification, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Study Materials

Create entity type by importing ABAP structure. Here give  CDS Dabase view ‘CDS_DDL_SAMPLE1’, select create default entity set flag and click on next:

SAP ABAP Tutorial and Material, SAP Certification, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Study Materials

follow the wizard where you select parameters and key fields respectively:

SAP ABAP Tutorial and Material, SAP Certification, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Study Materials

SAP ABAP Tutorial and Material, SAP Certification, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Study Materials

click on finish, then entity, entity set and corresponding properties are created. Expand node service implementation right click on SalesOrderSet->Map to Data Source

SAP ABAP Tutorial and Material, SAP Certification, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Study Materials

Here you map your CDS Database view(CDS_DDL_SAMPLE1) and CDS enity(ZCDS_SAMPLE1). on click on map to data source popup populates to select CDS business entity:

SAP ABAP Tutorial and Material, SAP Certification, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Study Materials

select type as business entity and give CDS entity ZCDS_SAMPLE1 as Name:

SAP ABAP Tutorial and Material, SAP Certification, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Study Materials

click on OK.

SAP ABAP Tutorial and Material, SAP Certification, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Study Materials

click on OK. Drag and drop properties to map fields or click on generate mapping to automatically map the fields.

SAP ABAP Tutorial and Material, SAP Certification, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Study Materials

After mapping done save the changes and click on generate icon as shown below to generate run time artifacts.

SAP ABAP Tutorial and Material, SAP Certification, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Study Materials

In model and service definition you can find DPC, MPC classes and technical service name of odata service:

SAP ABAP Tutorial and Material, SAP Certification, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Study Materials

click on ok and save the objects in local object;

SAP ABAP Tutorial and Material, SAP Certification, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Study Materials

after saving run time artifacts are generated. Expand node Service maintenance select gateway, click on register button to register the service.

SAP ABAP Tutorial and Material, SAP Certification, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Study Materials

add the service to local object:

SAP ABAP Tutorial and Material, SAP Certification, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Study Materials

click on OK. Now service is registered you can find status as green as shown below and click on Gateway client button to access the service:

SAP ABAP Tutorial and Material, SAP Certification, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Study Materials

Odata service in gateway client:

SAP ABAP Tutorial and Material, SAP Certification, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Study Materials

Use OData parameter $metadata to view the metadata of service:

SAP ABAP Tutorial and Material, SAP Certification, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Study Materials

on using odata parameter $metadata you can find the properties and entities of CDS view exposed as odata service.

This is how you create CDS view and expose the same as odata service using import DDIC structure by mapping CDS Database View and CDS entity using SEGW service builder transaction.

1 comment: