Create an oData service from CDS

This blog is about how to create an oData service from a CDS View. The code and example follow closely SAP Help documentation and the included example on this topic:

I only cut the documentation overhead and make the information available in a single blog. As you can see in the above two links, the task consists of 2 steps:

  1. Create CDS View
  2. Expose OData service

For the example, I used NW ABAP 7.52 Developer Edition and ABAP in Eclipse (ADT) tools. If you have a “real” SAP NW ABAP System available, you may also implement the sample service there.

Create CDS Data Source

In ADT, create a new CDS Data Definition.

Name: ZDEMO_CDS_SalesOrderItem
Description: List Reporting for Sales Order Item

Click on next to go throught the wizard.

Paste the following code in the new created file: https://github.com/tobiashofmann/cds_sample_service/blob/master/ZDEMO_CDS_SalesOrderItem

@AbapCatalog.sqlViewName: 'ZDEMO_SOI_001'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'List Reporting for Sales Order Item'
@OData.publish: true

define view ZDEMO_CDS_SalesOrderItem as select from SEPM_I_SalesOrderItem_E as Item {
  key Item.SalesOrder as SalesOrderID,
  key Item.SalesOrderItem as ItemPosition,
  Item._SalesOrder._Customer.CompanyName as CompanyName,
  Item.Product as Product,
  @Semantics.currencyCode: true
  Item.TransactionCurrency as CurrencyCode,
  @Semantics.amount.currencyCode: 'CurrencyCode'
  Item.GrossAmountInTransacCurrency as GrossAmount,
  @Semantics.amount.currencyCode: 'CurrencyCode'
  Item.NetAmountInTransactionCurrency as NetAmount,
  @Semantics.amount.currencyCode: 'CurrencyCode'
  Item.TaxAmountInTransactionCurrency as TaxAmount,
  Item.ProductAvailabilityStatus as ProductAvailabilityStatus
}

Save and activate the CDS View.

Activate OData Service

The above created a CDS Data Definition and when activating, some magic happened. What is missing is to activate the OData service. ADT won’t do this for you, this needs to be done manually in the Gateway System.

Tx: /n/IWFND/MAINT_SERVICE

Click on Add Service

Search for services in the local system.

System Alias: LOCAL
Technical Service Name: ZDEMO_CDS_SALESORDERITEM_CDS

Click on Get Services

The CDS Service is shown.

Select the service and click on Add selected Services

Add service dialog.

Package Assignment: $TMP (click on Local Object)

Test service

After performing the above steps, the CDS View is implemented and the OData service exposing the data is activate and can be used. You may now test the service to see if everything is working as expected.

Tx: /n/IWFND/MAINT_SERVICE
Select the service: ZDEMO_CDS_SALESORDERITEM_CDS

Click on SAP Gateway Client. To test the service, use the URL:

/sap/opu/odata/sap/ZDEMO_CDS_SALESORDERITEM_CDS/$metadata

Available entity sets can be seen by clicking on EntitySets.

Available options by clicking on Add URI Option

The see the top 2 results in json, the URL is:

/sap/opu/odata/sap/ZDEMO_CDS_SALESORDERITEM_CDS/ZDEMO_CDS_SalesOrderItem?$top=2&$format=json

Let the world know

Gateway: Set Profile Parameters

Profile parameter to be set in the Gateway and BEP (backend) system. SAP Help. These parameters are set in the DEFAULT profile SAP Help

  • login/accept_sso2_ticket = 1
  • login/create_sso2_ticket = 1

Transaction: RZ10

If the transaction is called for the 1st time, a profile must be generated first. We want to adjust the default profile, therefore a default profile must be cerated. Enter the profile meta data

  • Profile: DEFAULT
  • Version: 1

Select Create

Select Copy

Back on RZ10 main screen, select Import

Select the base profile to be imported.

Profile: DEFAULT.2.PFL

Select Copy

Click OK. New profile is now saved and activated.

Select Extended Maintenance and then Change.

A list of parameters is shown.

Create a new parameter: . Search for

  • Parameter name: login/accept_sso2_ticket
  • Parameter value: 1

Select copy If it worked, status message indicates:

Do the same for parameter login/create_sso2_ticket

  • Parameter name: login/create_sso2_ticket
  • Parameter value: 2

Result

Two new parameters are added to the profile:

Let the world know

Activate RMTSAMPLEFLIGHT service

After installing a SAP NetWeaver Gateway system, you’ll want to play around with a OData service. One option is to create everything from scratch; another option is to use a sample service that serves as a basis for learning. SAP NetWeaver ABAP comes with the flight sample, and to no surprise, SAP Gateway comes with a sample OData service based on the flight sample. To maintain a service, transaction /IWFND/MAINT_SERVICE is used. It shows a list of available services:

Adding the service

After a fresh SAP Gateway installation, the flight sample service won’t be listed. The service is already installed, but is not visible in the service catalog. To add it, select the Add Service option: This brings up the Add Selected Services screen.

The get a list of services, first inform the system alias. In my case, I created before in SPRO an alias named GWD for my Gateway system.

Then, click on Get Services: . The brings up the list of services available in the system

The OData service for the flight data is RMTSAMPLEFLIGHT or RMTSAMPLEFLIGHT_2. Select the services you want to add and click on Add Selected Services.

Confirm the following dialog

This will load the service. After everything was done successfully, you’ll hopefully see the following information:

Back at the Activate and Maintain Services transaction shows that the RMTSAMPLEFLIGHT service is added.

Test the service

Click on the service name. This will change the lower section of the screen.

To test the service, click on Gateway Client: . This will open the SAP NetWeaver Gateway Client.

Note that in the Request URI field the service URI is already inserted: /sap/opu/odata/IWFND/RMTSAMPLEFLIGHT/?$format=xml

To see of the service works, just execute a GET request. This should bring back the service description.

Service document

Metadata document

Collection

To retrieve data from a specific collection, click on Entity Sets: . Select an Entity Set (Collection)

This will set the URI parameter accordingly.

Specific flight

The information returned by the entity set can be used to retrieve the information of a specific flight. Part of the returned XML is an entity, that contains an URL like this:

/sap/opu/odata/IWFND/RMTSAMPLEFLIGHT/FlightCollection(carrid=’AA’,connid=’0017′,fldate=datetime’2014-10-29T00%3A00%3A00′)

Copy & paste this URL in the URI field.

Let the world know