Explaining Metadata Extension

Objectives
After completing this lesson, you will be able to:

After completing this lesson, you will be able to:

  • List the steps for implementing a metadata extension

Metadata Extension

The syntax of CDS will grow if complex annotations are needed. That may reduce the readability of CDS.

A metadata extension separates annotation from business logic.

Perform the following steps to implement a metadata extension:

  1. Add an annotation of @metadata.allExtensions:true to the CDS view.
  2. Create a Metadata extension and write annotations for the view and its fields.
  3. Use ";" to separate fields.
Note
Metadata extensions only allow annotation of JSON style.

Some annotations relevant to the activation of CDS, like @odata.publish, @AbapCatalog.sqlViewName are not supported in a metadata extension.

Note
If there is more than one metadata extension annotation for the same CDS, refer to https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/abencds_meta_data_extension_eval.htm to know how to determine which metadata extension is used.

Create a List Report using a Metadata Extension for a CDS View

Business Example

In this exercise, you will learn a new method of writing CDS annotations by creating a metadata extension for a CDS view where you can separate the annotation from the business logic parts. The purpose of this exercise is to make you familiar with the process, so you don’t need to dive deep into the details about all annotations we use in this exercise.

In this exercise, replace every instance of ## with the group number your trainer assigned to you.

Task 1: Log in to the S4D SAP System using ABAP Development Tools for Eclipse (ADT)

Steps

  1. Open Eclipse.

  2. Navigate to the package ZTRAIN_## from the favorite packages of your ABAP project.

    1. Choose the small triangle on the left of your ABAP project, enter your password to the SAP system when prompted, and then choose OK.

    2. Expand the Favorite Packages folder by selecting the small triangle. Find your package ZTRAIN_##.

Task 2: Create and Activate a CDS View

Steps

  1. Create a blank CDS view using following information:

    CDS View Information

    FieldValue
    NameZCDS_UX403_META_##
    DescriptionUX403 Writing CDS annotations using metadata extension
    1. Right-click your package and from the context menu choose NewOther ABAP Repository Object.

    2. In the ABAP Repository Object step, enter data into the search field, and choose Data Definition under the Core Data Services folder, and then choose Next.

    3. In the Data Definition step, complete the Name and Description fields according to the task description, and choose Next.

    4. In the Selection of Transport Request step, select the Choose from requests in which I am involved button. Select an available transport request from the table and choose Next.

    5. In the Templates step, deselect Use the selected template, and choose Finish.

    6. A blank CDS is already created.

  2. Copy the source code from "N:\UX403\Templates\UX403_Exercise06_Template01.txt" into the code editor of the CDS view.

    You must replace all the instances of ## in the source code before you activate it. There are two ## in each source code. The first is after @AbapCatalog.sqlViewName. The second is after define view.

    1. Open the File Explorer.

    2. Double-click Application (N:).

    3. Double-click UX403.

    4. Double-click Templates.

    5. To open the template file, double-click the file mentioned in task description.

    6. Select all the content in the text file and, on your keyboard, press Ctrl + C to copy contents to the clipboard.

    7. Switch back to ADT and in your blank CDS view and, on your keyboard, press Ctrl + V to paste the content.

    8. Replace all ## in the CDS view to your group number.

    9. Your implementation should look like the following figure:

    10. Save your changes.

  3. Activate your CDS view.

    1. To activate your CDS view, choose the Activate button in the toolbar.

Task 3: Create a Metadata Extension for your CDS View

Steps

  1. Add an annotation to allow a metadata extension for your CDS view.

    1. In the source code of your CDS view, add the annotation @Metadata.allowExtensions: true after the line of @AbapCatalog….

    2. Your implementation should look like the following figure:

    3. Save your changes.

    4. To activate your CDS view, choose the Activate icon in the toolbar.

  2. Create a blank CDS metadata extension using following information:

    CDS Metadata Extension Information

    FieldValue
    NameZCDS_UX403_META_##
    DescriptionUX403 Writing CDS annotation using metadata extension
    1. Right-click your package and, from the context menu, choose NewOther ABAP Repository Object.

    2. In the ABAP Repository Object step, enter meta in the search field, and choose Metadata Extension under the Core Data Services folder. Then, choose Next.

    3. In the Metadata Extension step, complete the Name and Description fields according to the task description, and choose Next.

    4. In the Selection of Transport Request step, select the Choose from requests in which I am involved button. Select an available transport request from the table with the owner TRAIN-## and choose Next.

    5. In the Templates step, deselect the Use the selected template option, and choose Finish.

    6. A blank CDS metadata extension has been created.

  3. Copy the source code of the text file "N:\UX403\Templates\UX403_Exercise06_Template02.txt" into the code editor of the CDS metadata extension.

    Note
    You must replace ## after the annotate view in the source code before you activate it.
    1. Open the File Explorer.

    2. Double-click Application (N:).

    3. Double-click UX403.

    4. Double-click Templates.

    5. To open the template file, double-click the file mentioned in task description.

    6. Select all the content in the text file and, on your keyboard, press Ctrl + C to copy contents to the clipboard.

    7. Switch back to ADT in your blank CDS metadata extension, and, on your keyboard, press Ctrl + V to paste the content.

    8. Replace the one ## in the CDS view with your group number.

    9. Your implementation should look like the following figure:

    10. Save your changes.

  4. Activate your metadata extension.

    1. To activate your metadata extension, choose the Activate icon in the toolbar.

  5. Return to your CDS view ZCDS_UX403_META_##, and find the information that shows the relationship between the CDS view and the metadata extension.

    1. To return to your CDS view, choose the tab which has the title ZCDS_UX403_META_## and the D icon in front of the title.

    2. Activate your CDS view again by choosing the Activate icon in the toolbar.

    3. Now there are two icons on the left side of the line of source code define view…. The warning icon overlaps the icon which shows the link to the metadata extension.

    4. Hide the warning icon by selecting the warning icon on the left side of the line of source code define view…, and then double-clicking on Hide warning.

    5. Notice that there is a new icon on the left side of the line of source code define view…. Hover your mouse over the icon and the relationship to the metadata extension displays.

    6. Now you can delete the line /*+[hideWarning] { "IDS" : [ "KEY_CHECK" ] } */.

    7. Save your changes.

    8. Reactivate the CDS view by choosing the Activate button in the toolbar.

Task 4: Expose your CDS View as an OData Service

Steps

  1. Add an @OData.publish: true annotation to your CDS view ZCDS_UX403_META_## and reactivate it.

    1. To switch to the tab page of your CDS view, choose the tab with the title of your CDS view name and the D icon in front of the title.

    2. Add the annotation @OData.publish: true after the annotation @Metadata.allowExtensions: true.

    3. Your implementation should look like the following:

    4. Save your changes.

    5. To activate your CDS view again, choose the Activate button in the toolbar.

    6. A warning appears on the left of @OData.publish: true to tell you that the OData service is generated but not activated. You can see it by hovering your mouse over the warning icon.

  2. Publish your new OData service on the S4D SAP system.

    1. Open the SAP Logon.

    2. Log on to the SAP system S4D with your user credentials.

    3. Enter /N/IWFND/MAINT_SERVICE in the input field on the top left of the window, then press Enter.

    4. Choose the Add Service icon in the application toolbar.

    5. In the Add Selected Service window, select LOCAL as the System Alias, enter your CDS view name ZCDS_UX403_META_##_CDS at Technical Service Name, and choose Get Services.

    6. Choose the line of service in the search result table, and chose the Add Selected Services icon.

    7. In the Add Service popup window, enter your package ZTRAIN_## in the Package Assignment field, and choose Continue (green checkmark).

    8. In the popup windows that follow, make sure that your workbench request is selected and choose Continue (green checkmark).

    9. A popup window with a successful information appears, choose Continue (green checkmark) to finish the publish process.

Task 5: Create an SAP Fiori elements application from the OData Service generated by the CDS View

Steps

  1. Open the SAP Business Application Studio.

  2. Create an SAP Fiori elements project using the following information:

    Note
    Make sure that you do not create the new project in the project folder from the previous exercises.  In the Project Attributes in the Business Application Studio, there is a field Project folder path and there it should look like this: /home/user/projects/.

    SAP Fiori elements Project Information

    NameValue
    TemplateList Report Page
    SAP SystemS4D_100
    OData serviceZCDS_UX403_META_##_CDS
    Main entityZCDS_UX403_META_##
    Module namecdsmetaext.student##
    Application titleList Report from CDS view and Metadata Extension
    DescriptionList Report from CDS view and Metadata Extension.
    1. Choose FileNew Project from Template in the SAP Business Application Studio.

    2. Choose SAP Fiori application and choose Start.

    3. Choose SAP Fiori application as Application Typeand choose List Report Page as floorplan. Then choose Next.

    4. Choose Connect to an SAP System at Data source.

    5. Choose S4D_100 for System.

    6. Enter META_##_CDS into the input field of Service and select your service ZCDS_UX403_META_##_CDS. Choose Next.

    7. In the Entity Selection screen, select ZCDS_UX403_META_## for Main entity. Choose Next.

    8. On the Project Attributes screen, use cdsmetaext.student## as the Module name, use List Report from CDS view and Metadata Extension as Application title and use List Report from CDS view and Metadata Extension. as Description. Make sure that your previous folder is not selected in the Project folder path. Select the Project folder. It should look like this /home/user/projects/. Choose Finish.

    9. The dependencies will be installed. Wait until you can see the notification The project has been generated.

    10. Open the Workspace with your new Project. Choose File and select Open Folder and choose your new Project.

  3. Test the application.

    1. Start the application as in the previous exercises using the application preview function. 

    2. If you have your popups allowed, a new window with the running application will open.

    3. In the opened application, choose the Go button to display the Sales Orders in the List Report.

Log in to track your progress & complete quizzes