Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
Xiao-fei_Song
Advisor
Advisor
*** Please be aware that the Scope of ODM was expanded and therefore the ODM-related content of this blog post has been updated.

Refer to the following blog post: “Harnessing Half a Century of Knowledge: SAP’s Journey of Enriching APIs with Business Metadata.”***

In this blog post, we will demonstrate how to extend SAP ODM model using the SAP CDS Graphical Modeler and SAP Business Application Studio, and also show you how to generate the OData services based on the extended model through CDS projection.

In order to use the CDS Graphical Modeler in SAP Business Application Studio, we would need to create a dev space in "Full Stack Cloud Application" category. For the introduction of CDS Graphical Modeler and how to setup Business Application Studio dev space as well as creating the CAP project, please refer to https://blogs.sap.com/2021/04/23/an-introduction-to-cds-graphical-modeler-for-sap-business-applicati....

Prepare Your CAP Project


So assuming that your CAP project has been created successfully already in the "Full Stack Cloud Application" dev space, we need to include SAP ODM model by adding ODM dependencies to the package.json file in the project.


 

Save the file and let's build this project. Open a terminal through "Terminal" -> "New Terminal" menu of the application studio, and type "npm install" in the command line console. This will setup the project dependencies including the ODM module we want to extend.

After the build is successful, open db/data-model.cds using the CDS Graphical Modeler:




Create an Extension for ODM WorkforcePerson Entity


In order to create the extension of ODM model, we need to first import the ODM cds file to our data-model.cds in the project. Click the "Import" toolbar button and select the WorkforcePerson.cds under "node_modules/@sap/odm/dist/workforce" folder:


 

Click the "Select CDS File" dialog button to dismiss the dialog and you will see the import CDS dialog:


Accept the default and click "OK" button to finish the import. You can verify the db/data-model.cds using the code editor:
namespace my.bookshop;

using sap.odm.workforce from '@sap/odm/dist/workforce/WorkforcePerson';

entity Books
{
key ID : Integer;
title : String;
stock : Integer;
}

Now you see the using statement for the ODM WorkforcePerson has been successfully generated by the CDS Graphical Modeler.

Then we can create an extension for ODM WorkforcePerson entity by clicking the "Add extension" toolbar menu item:


And in the "create extension" dialog select the WorkforcePerson as the base type:



Click the "Create" button and close the dialog, now you will see the empty extension for WorkforcePerson entity.



Let's add a few properties through this extension through the "Add Property" floating toolbar button.



and in the "New Property" dialog, add 3 extended properties to this extension.

  • ext__newstring_property

  • ext__newint_property

  • ext__newdouble_property



Close the dialog and you will see the extension now contains the extended properties you just created.


Now you can check if the extension has taken effective on the original WorkforcePerson entity in the ODM namespace. You can navigate to the "sap.odm.workforce" namespace:


Then you can see the 3 newly added properties have been included in the ODM WorkforcePerson entity.


We can now create a projection for the ODM WorkforcePerson entity in the srv/cat-service.cds. Open the srv/cat-service.cds file:


You can see that the Books projection already exists in the CatalogService.


Now let's create a new service called "AdminService" where we want to create the projection. Click "New Service" toolbar button:


And then enter "AdminService" in the "New Service" dialog and click "Create" button to dismiss the dailog. CDS Graphical Modeler will load the newly created "AdminService":


Import ODM WorkforcePerson.cds file for srv/cat-service.cds as well, the same way as what we do for db/data-model.cds:


Click "Select CDS File" and finish the import. Now let's create a projection for the extended WorkforcePerson entity:


You will see the "create projection" dialog:



In the "New Projection" dialog, select "sap.odm.workforce.WorkforcePerson" as the projection base type. Please ensure to exclude "personalDetail" and "profileDetail" while creating the projection to avoid an odata service generation error for this version of ODM.


Click "Create" and finish creating the projection. Now you should be able to see the newly created projection in AdminService.


Please also note that the 3 newly added properties have shown up in the projection. This is expected because cat-service.cds includes db/data-model.cds where we have already extended the ODM WorkforcePerson entity through a CDS extension.

Run a Local OData Service


Now let's generate a local odata service and check its odata metadata to ensure everything is working properly. Open a terminal again and type "cds watch" and press enter.


After the service is generated you will see below popup dialog to indicate the exposed port information in the SAP Business Application Studio.


Click "Expose and Open" button to dismiss this dialog. Now you should be able to see a new browser tab is opened that allows you to check the odata metadata.


Click "/admin/$metadata" URL and check the OData metadata. Now locate the "WorkforcePerson" entity type in the OData metadata document, you should be able to see the 3 newly added properties in the "WorkforcePerson" entity type:


With this odata services, we have extended the original ODM WorkforcePerson entity successfully by adding 3 additional new properties, and the user can build the frontend application to consume this extended odata model now.

References


An introduction to CDS Graphical Modeler for SAP Business Application Studio

Creating CDS projections using the CDS Graphical Modeler

Importing CDS file using the CDS Graphical Modeler

Conclusion


As a summary, in this blog post we first show you how to import the ODM model into the CAP project and how to import the ODM WorkforcePerson entity into the data model for this project. And then we demonstrate how to create an extension for the ODM WorkforcePerson, and then create a projection on this extended ODM model in the newly created CDS service. Finally, we have demonstrated how to run a local OData service based on the CAP project through "cds watch" and show you the extended OData model in the OData metadata document.
8 Comments