Publishing CDS Views as SAP Gateway Services

Objectives

After completing this lesson, you will be able to:
  • Define SAP Gateway Services via CDS View Annotations
  • Define a Navigation Using CDS View Associations

CDS View Publishing

Development flow: CDS view publishing

A CDS view is published in the ABAP Development Tools (ADT). The data model and service implementation are generated together by implementing the OData.publish: true annotation in a CDS view.

Development steps: CDS view publishing

The starting point is a CDS view in the ADT. First the name of the entity type and entity set are defined. Adding the OData.publish: true annotation in the CDS view generates SAP Gateway runtime artifacts when activating the CDS view. These runtime artifacts are tagged as system code and are not visible or even changeable in any ABAP editor. All read operations are generated based on SADL. After registering the service, these read operations can be tested.

Write operations can only be implemented by integrating a managed runtime. But this is then separated from publishing a CDS view as SAP Gateway service.

Source code implementing the publish OData annotation in a CDS view

Open the CDS data definition in the ADT. Above the line with define view, add the OData.publish: true annotation and activate the CDS data definition. This generates the runtime artifacts tagged as system code. You can only see in the status line that there is a generation of ABAP classes ongoing during activation.

Screenshot flow about how to register a published CDS view in /IWFND/MAINT_SERVICE

After activation, a warning is shown in the line with the OData annotation. The ADT informs you that the OData service is not yet registered.

Open the SAP Gateway Service Maintenance (/IWFND/MAINT_SERVICE) and add the service to the service catalog. Search for the name of the data definition ending with "_CDS" and register the service.

After a refresh of the ADT, the warning is replaced by the information "G", which means Generated Objects. The message also provides a link to call the OData service document in a Web browser.

Note

The warning will only disappear, if the OData service and CDS data definition are in the same system – an embedded deployment. In a hub deployment, when the OData service is in another system, the warning will stay.

OData Metadata Annotations in CDS Views

Source code implementing OData metadata annotations in a CDS view

The name of the entity type and entity set can be defined by additional OData annotations:

  • @OData.entityType.name: '<entity>Type'
  • @OData.entitySet.name: '<entity>'

Both must be placed above the line with define view. The sequence is not important. The runtime artifacts are updated when the CDS data definition is activated.

Hint

Following the SAP naming rules, the entity set should be a name in singular and the entity type should be the entity set name with "Type" appended.
Source code and result of defining OData metadata in a CDS view

Not defining names for entity type and entity set leads to names derived from the CDS data definition. This may lead to complex terms not easily understandable by the OData service consumer. In addition, the technical basis of a service implementation is visible outside the ABAP system. This also applies if the OData service is consumed in the Internet.

Implement OData.Publish in a CDS View

Business Example

You are a developer or solution architect in your company. You need to implement an SAP Gateway service based on a CDS view.

Template:
None
Solution:
GW100_C_SupplierPublish (CDS Data Definition)

Note

This exercise requires an SAP Learning system. Login information is provided by your system setup guide.

Note

Whenever the values or object names in this exercise include ##, replace ## with the number of your user.

Prerequisites

The packages were created in exercise Define a Data Model Based on a Data Dictionary Structure .

Task 1: Create an ABAP Project in Eclipse

Steps

  1. In Eclipse, create an ABAP project connecting to your SAP S/4HANA (S4H) system. Add package ZGW100_##_BES as favorite.

    1. In the Windows start menu, choose Eclipse.

    2. Choose Hide in the upper right corner of the Welcome page.

    3. Choose Open Perspective in the upper right corner.

    4. Select ABAP and choose OK.

    5. Choose NewABAP Project.

    6. In the Connection Settings popup, in the System ID field, enter the system ID of your S4H.

    7. Select the Active Secure Network Communication (SNC) checkbox.

    8. Choose Next >.

    9. Enter your client and user and choose Finish.

    10. In the Project Explorer, expand your system.

    11. Right-click Favorite Packages and choose Add Package….

    12. Enter ZGW100_##_BES and choose OK.

    13. Expand Favorite Packages.

Task 2: Create a CDS Data Definition

Steps

  1. Create the CDS view entity Z##_C_Supplier in your ZGW100_##_BES package with SEPM_I_BusinessPartner as data source. Set @Metadata.ignorePropagatedAnnotations to false and delete the @ObjectModel annotation.

    1. Right-click the ZGW100_##_BES node and choose NewOther ABAP Repository Object.

    2. In the New ABAP Repository Object popup, select Core Data ServicesData Definition.

    3. Choose Next.

    4. In the New Data Definition popup, in the Name field, enter Z##_C_Supplier.

    5. In the Description field, enter a value of your choice.

    6. Choose Next.

    7. Select your transport request and choose Next.

    8. Select the template Define View Entity.

    9. Choose Finish.

    10. For the data source name, enter SEPM_I_BusinessPartner and choose Enter.

    11. Set @Metadata.ignorePropagatedAnnotations to false.

    12. Delete the @ObjectModel annotation.

    13. Your code should look like this:

      Code Snippet
      123456789
      @AbapCatalog.viewEnhancementCategory: [#NONE] @AccessControl.authorizationCheck: #NOT_REQUIRED @EndUserText.label: 'Supplier ##' @Metadata.ignorePropagatedAnnotations: false define view entity Z##_C_Supplier as select from SEPM_I_BusinessPartner { }
    14. Choose Save.

      Note

      There is still the error Unexpected word "}". Fields will be added in the next exercise step.
  2. Include the following columns in Z##_C_Supplier. You may use the Insert all elements template of the code completion. Activate and run the data definition as ABAP application.

    • BusinessPartnerUUID

    • BusinessPartner

    • BusinessPartnerRole

    • Currency

    • CompanyName

    • LegalForm

    • EmailAddress

    • FaxNumber

    • PhoneNumber

    • URL

    1. Place the cursor between the curly brackets.

    2. Choose Ctrl+Space.

    3. Select Insert all elements (template) and choose Enter.

    4. Delete all associations.

    5. Delete the following columns:

      • AddressUUID

      • CreatedByUser

      • CreationDateTime

      • LastChangedByUser

      • LastChangedDateTime

    6. Your code should look like this:

      Code Snippet
      12345678910111213141516171819
      @AbapCatalog.viewEnhancementCategory: [#NONE] @AccessControl.authorizationCheck: #NOT_REQUIRED @EndUserText.label: 'Supplier ##' @Metadata.ignorePropagatedAnnotations: false define view entity Z##_C_Supplier as select from SEPM_I_BusinessPartner { key BusinessPartnerUUID, BusinessPartner, BusinessPartnerRole, Currency, CompanyName, LegalForm, EmailAddress, FaxNumber, PhoneNumber, URL }
    7. Choose Activate.

    8. Choose RunRun AsABAP Application or F8.

      Result

      The Data Preview displays a list of suppliers.

Task 3: Publish a CDS Data Definition as OData Service

Steps

  1. In Eclipse, connected to your S4H, publish the Z##_C_SUPPLIER data definition as OData service. Test the service from the Outline of the data definition.

    1. In Eclipse, connected to your S4H, add @OData.publish: true above the view definition.

    2. Your code should look like this:

      Code Snippet
      12345678910
      @AbapCatalog.viewEnhancementCategory: [#NONE] @AccessControl.authorizationCheck: #NOT_REQUIRED @EndUserText.label: 'Supplier ##' @Metadata.ignorePropagatedAnnotations: false @OData.publish: true define view entity Z##_C_Supplier as select from SEPM_I_BusinessPartner ... }
    3. Choose Activate.

    4. In the Outline, expand Z##_C_SUPPLIERsecondary objects.

    5. Right click OData Exposure and choose Open.

    6. Log on using your user and password of your S4H.

      Result

      The Web browser displays the error No service found....
  2. In the SAP Gateway Service Maintenance of your S4H, register the Z##_C_SUPPLIER_CDS service for system alias LOCAL using your package ZGW100_##_FES.

    1. In the SAP Easy Access of your S4H, search for SAP Gateway Service Maintenance or start transaction /IWFND/MAINT_SERVICE.

    2. In the SAP Gateway Service Maintenance, choose Add Service.

    3. In the System Alias field, enter LOCAL.

    4. In the Technical Service Name field, enter Z##*.

    5. Choose Get Services.

    6. Choose Z##_C_SUPPLIER_CDS.

    7. In the Add Service popup, in the Package Assignment field, enter ZGW100_##_FES.

    8. Choose Continue.

    9. For every popup, choose the transport request provided to you.

    10. In the Information popup, choose Continue.

    11. Choose Back.

Task 4: Test the Metadata, Query, and Read Operations of the SAP Gateway Service

Steps

  1. In the SAP Gateway Client of your S4H, test the metadata of your service. What are the names of the entity type and entity set?

    1. In the SAP Gateway Service Maintenance of your S4H, choose SAP Gateway Client.

    2. In the SAP Gateway Client, select HTTPS as Protocol.

    3. Choose Add URI Option.

    4. In the Add URI Option popup, double-click $metadata.

      Example

      /sap/opu/odata/SAP/Z##_C_SUPPLIER_CDS/$metadata
    5. Choose Execute.

      Result

      The HTTP Response displays the metadata of Z##_C_SupplierType and Z##_C_Supplier.
  2. Test the paging operation for suppliers in your service. Query the first two suppliers.

    1. Choose Entity Set.

    2. In the Entity Sets popup, double-click Z##_C_Supplier.

    3. Choose Add URI Option.

    4. In the Add URI Option popup, double-click $top=2.

      Example

      /sap/opu/odata/SAP/Z##_C_SUPPLIER_CDS/Z##_C_Supplier?$top=2
    5. Choose Execute.

      Result

      The HTTP Response displays the first two suppliers.
  3. Test the sorting operation for suppliers in your service. Query suppliers are sorted by their company name ascending.

    1. In the Request URI field, add &$orderby=CompanyName asc to the URI.

      Example

      /sap/opu/odata/SAP/Z##_C_SUPPLIER_CDS/Z##_C_Supplier?$top=2&$orderby=CompanyName asc
    2. Choose Execute.

      Result

      The HTTP Response displays the first two suppliers ordered by company name ascending.
  4. Test the filter operation for suppliers in your service. Query the suppliers whose e-mail address starts with "supplier".

    1. In the Request URI field, add $filter=startswith(EmailAddress, 'supplier') to the URI.

      Example

      /sap/opu/odata/SAP/Z##_C_SUPPLIER_CDS/Z##_C_Supplier?$top=2&$orderby=CompanyName asc&$filter=startswith(EmailAddress, 'supplier')
    2. Choose Execute.

      Result

      The HTTP Response displays the first two suppliers ordered by company name ascending whose e-mail address starts with "supplier".
  5. Test the read operation for suppliers in your service. Read a single supplier.

    1. In the Request URI field, replace all query options by the key of a supplier with guid in front.

      Example

      /sap/opu/odata/SAP/Z##_C_SUPPLIER_CDS/Z##_C_Supplier(guid'00505604-4e85-1edd-b9b4-a81d353342c0')

      Hint

      Copy the self link of a supplier in the HTTP Response.
    2. Choose Execute.

      Result

      The HTTP Response displays the selected supplier.

Task 5: Change the OData Service Data Model in a CDS Data Definition

Steps

  1. In Eclipse connected to your S4H, in the Z##_C_SUPPLIER data definition, set the entity set name to Supplier and the entity type name to SupplierType.

    1. In Eclipse connected to your S4H, add @OData.entitySet.name: 'Supplier' one line beneath @OData.publish: true.

    2. Add @OData.entityType.name: 'SupplierType' one line beneath @OData.entitySet.name: 'Supplier'.

    3. Your code should look like this:

      Code Snippet
      123456789101112
      @AbapCatalog.viewEnhancementCategory: [#NONE] @AccessControl.authorizationCheck: #NOT_REQUIRED @EndUserText.label: 'Supplier ##' @Metadata.ignorePropagatedAnnotations: false @OData.publish: true @OData.entitySet.name: 'Supplier' @OData.entityType.name: 'SupplierType' define view entity Z##_C_Supplier as select from SEPM_I_BusinessPartner ... }
    4. Activate your changes.

  2. In the SAP Gateway Client of your S4H, test the metadata of your service. What are the names of the entity type and entity set?

    1. In the SAP Gateway Client, select HTTPS as Protocol.

    2. Choose Add URI Option.

    3. In the Add URI Option popup, double-click $metadata.

      Example

      /sap/opu/odata/SAP/Z##_C_SUPPLIER_CDS/$metadata
    4. Choose Execute.

      Result

      The HTTP Response displays the metadata of SupplierType and Supplier.
  3. Test the query operation for suppliers in your service. Query all suppliers.

    1. Choose Entity Set.

    2. In the Entity Sets popup, double-click Supplier.

      Example

      /sap/opu/odata/SAP/Z##_C_SUPPLIER_CDS/Supplier
    3. Choose Execute.

      Result

      The HTTP Response displays all suppliers.

Navigation Definition in CDS Views

Source code implementing a navigation in a CDS view

Navigation in an OData service based on a published CDS view is defined by implementing associations in the CDS data definitions. Associations define relationships between data definitions that can easily be read and are translated into joins on the database level. In addition, exposed associations are only evaluated when needed – like navigation properties in OData.

An association must be implemented right before the open curly bracket that contains the view fields. The implementation defines the association name, target CDS view, multiplicity, and key mapping. To enable a navigation via the association, the association must be exposed by adding the name of the association to the list of fields inside the curly brackets.

The runtime artifacts are updated when the CDS data definition is activated.

Note

Following the SAP naming rules, associations should start with an underscore and should be added at the end of the list of fields.
Source code and result of defining a navigation in a CDS view

Defining the association in the data definition results in an association and association set in the OData service metadata. Exposing the association in the data definition results in a navigation property in the entity type. The name of the navigation property is the name of the association in the data definition with "to" in front of it.

Implement a Navigation Using Associations in CDS Views

Business Example

You are a developer or solution architect in your company. You need to add an entity for navigation to an SAP Gateway service based on CDS views.

Template:
GW100_C_SupplierPublish (CDS Data Definition)
Solution:
GW100_C_ArticleNavigation (CDS Data Definition)
GW100_C_SupplierNavigation (CDS Data Definition)

Note

This exercise requires an SAP Learning system. Login information are provided by your system setup guide.

Note

You may continue with your solution of the previous exercise or copy the template to Z##_C_SupplierNavigation. Whenever the values or object names in this exercise include ##, replace ## with the number of your user.

Prerequisites

The OData service data model was implemented and the service was registered in exercise Implement OData.Publish in a CDS View .

Task 1: Create a CDS Data Definition with Association

Steps

  1. In Eclipse connected to your SAP S/4HANA (S4H) system, create the CDS view entity Z##_C_Article in your ZGW100_##_BES package with SEPM_I_Product as data source. Set @Metadata.ignorePropagatedAnnotations to false and delete the @ObjectModel annotation.

    1. In Eclipse connected to your S4H, expand <your system>Favorite PackagesZGW100_##_BESCore Data ServicesData Definitions.

    2. Right-click the Data Definitions node and choose New Data Definition.

    3. In the New Data Definition popup, in the Name field, enter Z##_C_Article.

    4. In the Description field, enter a value of your choice.

    5. Choose Next.

    6. Select your transport request and choose Next.

    7. Select the template Define View Entity.

    8. Choose Finish.

    9. For the data source name, enter SEPM_I_Product and choose Enter.

    10. Set @Metadata.ignorePropagatedAnnotations to false.

    11. Delete the @ObjectModel annotation.

    12. Your code should look like this:

      Code Snippet
      123456789
      @AbapCatalog.viewEnhancementCategory: [#NONE] @AccessControl.authorizationCheck: #NOT_REQUIRED @EndUserText.label: 'Article ##' @Metadata.ignorePropagatedAnnotations: false define view entity Z##_C_Article as select from SEPM_I_Product { }
    13. Choose Save.

      Note

      There is still the error Unexpected word "}". Fields will be added in the next exercise step.
  2. Include the following columns in Z##_C_Article. You may use the Insert all elements template of the code completion. Activate and run the data definition as ABAP application testing the association.

    • ProductUUID
    • Product
    • ProductType
    • Price
    • Currency
    • Height
    • Width
    • Depth
    • DimensionUnit
    • ProductPictureURL
    • ProductValueAddedTax
    • SupplierUUID
    • Weight
    • WeightUnit
    1. Place the cursor between the curly brackets.

    2. Choose Ctrl+Space.

    3. Select Insert all elements (template) and choose ENTER.

    4. Delete the following columns:

      • ProductCategory
      • CreatedByUser
      • CreationDateTime
      • LastChangedByUser
      • LastChangedDateTime
      • ProductNameGroupUUID
      • ProductDescriptionGroupUUID
      • ProductBaseUnit
    5. Delete all associations except _Supplier.

    6. Your code should look like this:

      Code Snippet
      1234567891011121314151617181920212223
      @AbapCatalog.viewEnhancementCategory: [#NONE] @AccessControl.authorizationCheck: #NOT_REQUIRED @EndUserText.label: 'Article ##' @Metadata.ignorePropagatedAnnotations: false define view entity Z##_C_Article as select from SEPM_I_Product { key ProductUUID, Product, ProductType, Price, Currency, Height, Width, Depth, DimensionUnit, ProductPictureURL, ProductValueAddedTax, SupplierUUID, Weight, WeightUnit }
    7. Choose Activate.

    8. Choose RunRun AsABAP Application or F8.

      Result

      The Data Preview displays a list of articles.
  3. In the Z##_C_Article data definition, define an association to Z##_C_Supplier with cardinality 1 and the name _Supplier. Test the association in the data preview.

    1. In the Z##_C_Article data definition, add association [1] to Z##_C_Supplier as _Supplier on $projection.SupplierUUID = _Supplier.BusinessPartnerUUID before the curly bracket open of the columns.

    2. Add _Supplieras a new column.

    3. Your code should look like this:

      Code Snippet
      1234567891011121314151617181920212223242526
      @AbapCatalog.viewEnhancementCategory: [#NONE] @AccessControl.authorizationCheck: #NOT_REQUIRED @EndUserText.label: 'Article ##' @Metadata.ignorePropagatedAnnotations: false define view entity Z##_C_Article as select from SEPM_I_Product association [1] to Z##_C_Supplier as _Supplier on $projection.SupplierUUID = _Supplier.BusinessPartnerUUID { key ProductUUID, Product, ProductType, Price, Currency, Height, Width, Depth, DimensionUnit, ProductPictureURL, ProductValueAddedTax, SupplierUUID, Weight, WeightUnit, _Supplier }
    4. Choose Activate.

    5. Choose RunRun AsABAP Application or F8.

    6. In the Data Preview, right click an article in the list and choose Follow Association.

    7. In the List of Associations popup, choose _ArticlesZ##_C_Supplier [0 .. 1]

      Result

      The Data Preview displays the supplier of the article.

Task 2: Define a Bi-Directional Navigation in a CDS Data Definition

Steps

  1. In Eclipse connected to your S4H, in the Z##_C_ARTICLE data definition, set the entity set name to Article and the entity type name to ArticleType.

    1. In Eclipse connected to your S4H, add @OData.entitySet.name: 'Article' one line beneath @EndUserText.label: 'Article ##'.

    2. Add @OData.entityType.name: 'ArticleType' one line beneath @OData.entitySet.name: 'Supplier'.

    3. Your code should look like this:

      Code Snippet
      1234567891011
      @AbapCatalog.viewEnhancementCategory: [#NONE] @AccessControl.authorizationCheck: #NOT_REQUIRED @EndUserText.label: 'Article ##' @Metadata.ignorePropagatedAnnotations: false @OData.entitySet.name: 'Article' @OData.entityType.name: 'ArticleType' define view entity Z##_C_Article as select from SEPM_I_Product ... }
    4. Choose Activate.

  2. In the Z##_C_Supplier data definition, define an association to Z##_C_Article with cardinality 0..* and the name _Article. Test the association in the data preview.

    1. In the Z##_C_Supplier data definition, add association [0..*] to Z##_C_Article as _Article on $projection.BusinessPartnerUUID = _Article.SupplierUUID before the curly bracket open of the columns.

    2. Add _Article as a new column.

    3. Your code should look like this:

      Code Snippet
      12345678910111213141516171819202122232425
      @AbapCatalog.viewEnhancementCategory: [#NONE] @AccessControl.authorizationCheck: #NOT_REQUIRED @EndUserText.label: 'Supplier ##' @Metadata.ignorePropagatedAnnotations: false @OData.publish: true @OData.entitySet.name: 'Supplier' @OData.entityType.name: 'SupplierType' define view entity Z##_C_Supplier as select from SEPM_I_BusinessPartner association [0..*] to Z##_C_Article as _Article on $projection.BusinessPartnerUUID = _Article.SupplierUUID { key BusinessPartnerUUID, BusinessPartner, BusinessPartnerRole, Currency, CompanyName, LegalForm, EmailAddress, FaxNumber, PhoneNumber, URL, _Article }
    4. Choose Activate.

    5. Choose RunRun AsABAP Application or F8.

    6. In the Data Preview, right click a supplier with an e-mail address starting with "supplier" in the list and choose Follow Association.

    7. In the List of Associations popup, choose _ArticlesZ##_C_Article [ 0 .. * ]

      Result

      The Data Preview displays the articles of the supplier.

Task 3: Test the Navigation and Expand Operation of the SAP Gateway Service

Steps

  1. In the SAP Gateway Client of your S4H, test the query operation for suppliers in your service. Query all suppliers.

    Note

    From the last step of the previous exercise, if your SAP Gateway Client still displays the URI /sap/opu/odata/SAP/Z##_C_SUPPLIER_CDS/Supplier, you can skip this step.
    1. In the SAP Easy Access of your S4H, search for SAP Gateway Service Maintenance or start transaction /IWFND/MAINT_SERVICE.

    2. In the SAP Gateway Service Maintenance, choose Filter.

    3. In the Filter for Service Catalog popup, in the Technical Service Name field, enter Z##_C_SUPPLIER_CDS.

    4. Choose Continue.

    5. Select Z##_C_SUPPLIER_CDS.

    6. Choose SAP Gateway Client.

    7. In the SAP Gateway Client, select HTTPS as Protocol.

    8. Choose Entity Set.

    9. In the Entity Sets popup, double-click Supplier.

      Example

      /sap/opu/odata/SAP/Z##_C_SUPPLIER_CDS/Supplier
    10. Choose Execute.

      Result

      The HTTP Response displays all suppliers.
  2. Test the navigation between suppliers and articles in your service. Query the articles of a supplier.

    Hint

    If no articles are found for a supplier, make sure to select a supplier with an e-mail address starting with "supplier".
    1. In the Request URI field, add one of the links to the articles of a supplier.

      Example

      /sap/opu/odata/SAP/Z##_C_SUPPLIER_CDS/Supplier(guid'00505604-4e85-1edd-b9b4-a81d353342c0')/to_Article

      Hint

      Copy a link from the HTTP Response.
    2. Choose Execute.

      Result

      The HTTP Response displays the articles of the supplier.
  3. Test the expand operation for suppliers and articles in your service. Read a supplier including its articles.

    1. In the Request URI field, replace the / in front of to_Article with ?$expand=

      Example

      /sap/opu/odata/SAP/Z##_C_SUPPLIER_CDS/Supplier(guid'00505604-4e85-1edd-b9b4-a81d353342c0')?$expand=to_Article
    2. Choose Execute.

      Result

      The HTTP Response displays the supplier including its articles.

Log in to track your progress & complete quizzes