Projections, Service Definitions and OData V4 Bindings

This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production.

A business object with a data model and a behavior does nothing for a browser yet. Nobody can reach it, and it does not say which of its fields a particular app may see. Three more objects close that gap. A projection cuts out the part of the business object that one app needs. A service definition lists what the service offers. A service binding chooses the web protocol, here OData V4, and publishes the service so that a browser, a Fiori app or a script can call it. This page builds these three layers for the expense claim, one object at a time, and ends with a first request against the published service. It is written against ABAP for Cloud Development release 9.20 (2608) and applies to RAP in ABAP Cloud (the SAP BTP ABAP environment and SAP S/4HANA Cloud Public Edition) and in SAP S/4HANA on premise, where the service binding editor and the preview behave as described but where transporting a published service differs, as the section on publishing explains. Every listing needs an SAP system with ABAP development tools (ADT), so each one is labelled as requiring an SAP system and was not run here.

Before you read

Key terms

  • Projection view: a CDS view entity that selects, and may rename, the elements of one entity of a business object for one service.

  • Service definition: the list of CDS entities that a business service exposes, independent of any protocol.

  • Service binding: the object that ties a service definition to a protocol and to a kind of service, and publishes it.

  • OData: a web protocol for reading and changing data with ordinary HTTP requests and URLs.

  • SAP Fiori elements: SAP’s way of generating the screens of an app from annotations instead of hand-written screen code.

  • Value help: the list a user can open next to an input field to pick a valid value.

  • Web API: a service meant for programs, which carries no user interface information in its metadata.

The path from business object to service

The base business object is built without any service in mind and carries every feature a service might want. SAP calls the projection layer the first layer that is specific to a service. From there on every object belongs to one service: the projection views and the projection behavior definition cut the part the service needs, the service definition names it, and the service binding publishes it. Annotations for the screen, the metadata, are added to the projection layer.

flowchart TD BO["Base business object: ZI_ExpClaim, ZI_ExpItem and their behavior definition"] --> PV["Projection views: ZC_ExpClaim, ZC_ExpItem"] BO --> PB["Projection behavior definition: use create, use update, use action"] MDE["UI annotations and value helps"] -.-> PV PV --> SD["Service definition: expose ZC_ExpClaim as Claim"] PB --> SD SD --> UI["Service binding: OData V4 - UI"] SD --> API["Service binding: OData V4 - Web API"] UI --> FE["Fiori elements preview, Fiori app"] UI --> CL1["Service URL: metadata, entity sets"] API --> CL2["Any HTTP client, for example curl"]

The RAP guide describes the same split in its own words: the service definition is a projection of the data model and behavior to be exposed, and the service binding defines a communication protocol and the kind of service offered to a consumer. Because of the split, the same data and service definitions can be bound to other protocols without being implemented again.

The projection layer

A projection is a service-specific view of a business object. SAP gives two reasons to have one. The base object may grow without changing a service that already exists, because the projection exposes only the subset the service needs, and the same base object can be exposed for a Fiori UI and for a stable Web API, each with its own projection. The guide’s example is a business partner object that is projected as customer, vendor or supplier, each projection using only the part that fits the role.

A projection has two kinds of objects.

  • A CDS projection view for each node of the business object. It uses as projection on to point at the entity it projects, and, unlike the consumption views of the older model, it creates no extra database view and needs no ABAP Dictionary representation. It selects a subset of the elements of the projected entity and may rename them; it cannot add new persistent fields, but it can add virtual elements that ABAP code calculates and new read-only associations. When one entity is projected, its root and parent entities must be projected too, the root stays a root (define root view entity), and compositions are redirected to the projection view of the child.

  • A projection behavior definition, a behavior definition of type projection, which reuses behavior of the base definition with the keyword use.

The keyword documentation adds the rules for the view: the projected entity must be a CDS view entity (or a transactional interface view), the view names provider contract transactional_query, and, if it is part of a business object, it must be the root or reach the root through a composition. For the expense claim the root projection looks like this.

@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Expense claim'
@Metadata.allowExtensions: true
define root view entity ZC_ExpClaim
  provider contract transactional_query
  as projection on ZI_ExpClaim
{
  key ClaimId,
      @Consumption.valueHelpDefinition: [{ entity: { name: 'ZI_ExpEmployeeVH', element: 'EmployeeId' } }]
      EmployeeId,
      Status,
      CurrencyCode,
      @Semantics.amount.currencyCode: 'CurrencyCode'
      TotalAmount,
      _Items : redirected to composition child ZC_ExpItem
}
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Expense claim item'
@Metadata.allowExtensions: true
define view entity ZC_ExpItem
  as projection on ZI_ExpItem
{
  key ClaimId,
  key ItemNo,
      Category,
      @Semantics.amount.currencyCode: 'CurrencyCode'
      Amount,
      CurrencyCode,
      _Claim : redirected to parent ZC_ExpClaim
}

Both listings require an SAP system; not run here. The names ZC_ExpClaim and ZC_ExpItem are the running example’s; the elements come from ZI_ExpClaim and ZI_ExpItem as defined on CDS Associations, Compositions and Path Expressions, and the employee association _Employee is left out of the projection because the app does not need it. Three details deserve a closer look.

  • redirected to composition child ZC_ExpItem says that the composition _Items of the claim now leads to the projection of the item, not to the base item. The keyword documentation lists three forms: redirected to, redirected to composition child and redirected to parent. The child’s _Claim is redirected to the parent projection in the same way. Without the redirection the projection would still point into the base layer, and the data model would not be projected completely. The child view does not name a provider contract and does not repeat root, because only the root of a composition tree specifies the provider contract and the children inherit it.

  • Annotations on elements of the projected entity are propagated to the projection view. When an element is renamed in the projection, an annotation that refers to another element by name no longer finds it, and you have to write it again with the new name. The guide’s example is @Semantics.amount.currencyCode. The listing keeps the names of the base view, so the annotation in ZC_ExpClaim simply repeats the one in ZI_ExpClaim; if the projection renamed CurrencyCode to Currency, the annotation value would have to change to 'Currency'.

  • @Metadata.allowExtensions: true lets a metadata extension attach UI annotations, as the page on CDS Annotations and Metadata Extensions shows. The CDS pages of this section used ZC_ExpClaim for a plain consumption view that reads from ZI_ExpClaim with as select from. In a RAP business object the same role is played by this projection view, which is why it keeps the name and why its annotations and extension work the same way.

The access control must also follow. The keyword documentation says that the CDS access control of a projection view must be defined symmetrically to the access control of the projected entity, so that all the restrictions of the base are inherited; the page on CDS Access Control shows the define role that does it for ZC_ExpClaim.

The projection behavior definition

The projection view decides what the service can read. The projection behavior definition decides which operations it can perform. It does not contain code: the guide says that projection behavior definitions have no behavior implementation, because the complete behavior is realized by mapping to the underlying behavior. Everything the service should offer has to be named with use, since no operation or characteristic is inherited automatically.

projection;
strict ( 2 );

define behavior for ZC_ExpClaim alias Claim
{
  use create;
  use update;
  use delete;
  use action submit;
  use association _Items { create; }
}

define behavior for ZC_ExpItem alias Item
{
  use update;
  use delete;
  use association _Claim;
}

The listing requires an SAP system; not run here. It reuses the operations of the base behavior definition of Behavior Definitions and Implementations: the standard operations, the action submit and the creation of items through the claim. A use is valid only when the base definition has the element. strict ( 2 ) must match the strict mode of the projected business object, and the keyword documentation requires the same version in both. Locking, authorization and feature control are applied from the base and cannot be overwritten, so they do not appear. A service that should not offer deletion simply leaves out use delete. The additions precheck and augment of use need an implementation in a behavior pool and are not used here.

The service definition

The service definition is a repository object whose source text lists the entities a service exposes. The RAP guide calls it consumer-specific but protocol-agnostic: it knows nothing about OData or HTTP. Its name is in the same namespace as the CDS entities, and its transport type is SRVD.

@EndUserText.label: 'Expense claims service'
@ObjectModel.leadingEntity.name: 'ZC_ExpClaim'
define service ZSD_ExpClaim
{
  expose ZC_ExpClaim as Claim;
  expose ZC_ExpItem  as Item;
}

The listing requires an SAP system; not run here. It follows the shape of the guide’s own example, which exposes the travel root and its related entities. Reading it:

  • @EndUserText.label is a short text of at most 60 characters in the original language. Further annotations are optional.

  • @ObjectModel.leadingEntity.name names the root entity of the service. The guide says it marks that entity in the entity tree of the service binding editor and lists it first.

  • define service ZSD_ExpClaim starts the definition; the name ZSD_... follows a common convention (service definition), not a rule. The prefix ZUI_... is more usual for a UI service binding, so the running example uses ZSD_ExpClaim for the definition and ZUI_ExpClaim_O4 for the OData V4 binding.

  • Each expose line names one CDS entity and may give it an alias with as. The alias is what a client sees: the keyword documentation says the entity is exposed under that name and the alias must be used when the business service is accessed. Here the service has an entity set Claim and an entity set Item, not ZC_ExpClaim.

  • Each statement ends with a semicolon.

Two rules prevent surprises. The keyword documentation recommends that, together with the root of a RAP business object, all entities of the composition tree are exposed too, and that the targets of exposed associations are exposed as well; it says there is no syntax check for this, so a forgotten child shows up only when a client cannot reach it. Value help provider views are the exception: the guide says that when a source view is exposed in an OData service its value help provider view is exposed with it, so the employee value help of this page does not have to be listed. Abstract entities cannot be bound to an OData service for Web APIs or UI services; the guide allows them in a service definition only for the service consumption case.

A service definition on its own is not reachable: the guide says it is used only together with at least one service binding. A service definition extension exists for extension nodes that were added to a business object, so that the new node can be exposed without changing the original definition.

The service binding and OData V4

The service binding is the object that makes the service reachable. The guide lists its parameters.

Parameter What it means

Service name

The unique name of the service in the system. It is identical to the name of the service binding.

Binding type

Two choices in one: the protocol and the way the service is offered. The protocols named in the guide are OData V2, OData V4, InA for analytical models and SQL for published database API objects.

Service type

UI, a service that can carry an SAP Fiori elements UI or another UI client, or Web API, a service for every other use. UI services are supported for OData and InA, Web APIs for OData and SQL.

Service version

A version number of the binding. Adding another service definition to the binding creates the next version; removing one lowers it.

Because the binding type and the category cannot be changed once the binding exists, the choice is made in the creation wizard. For the expense claim the choices are OData V4 - UI for the app and, if other programs also need the data, a second binding of the type OData V4 - Web API on the same service definition. The guide advises using OData V4 wherever possible for transactional services, and adds that full support for Fiori elements UIs on OData V4 is granted only for draft-enabled scenarios, a topic the page on drafts takes up.

The two service types differ in what the metadata contains. A UI service carries UI information such as the annotations and the value help parameters; a Web API carries none. The comparison table of the guide has one column for each combination of version and type, and for most rows it gives one entry for both versions of a type. The next table shows those rows, where the entry holds for OData V2 and V4 alike.

Feature Web API UI service

UI annotations in the metadata

none

contained

Value help

not available

available

Side effects

not available

available

Feature control

not exposed as part of the service

exposed as part of the service

PDF export

not available

available via entity sets

Release contract in ADT

C2 can be set from the binding

C1 can be set from the binding

Fiori elements preview

not available

available in the service binding

The guide’s table has further rows, for example the code lists for units and currencies and the ISO conversion of units, where the entries differ between V2 and V4; read the original table for them. Two restrictions in the same topic matter for planning. First, change tracking with the $deltatoken query option of OData V4 delta queries is not supported out of the box in RAP OData V4 services; if a consumer needs it, the guide says the whole service has to be implemented by hand. Second, the guide describes messages for V4: bound messages travel in a collection-valued message container of a successful response, unbound messages in the sap-messages response header, and an error response has the structure of an OData V4 error with details, which the page on messages in RAP covers.

Creating, activating and publishing the binding

In ADT the sequence is the same for every binding. The guide’s tutorials describe it for a service named in the Flight reference scenario; the steps are the same for the expense claim.

  1. Open the context menu of the service definition and choose the entry that creates a new service binding.

  2. Enter the project, package, name and description, choose the binding type (OData V4 - UI or OData V4 - Web API), check that the right service definition is preset, then choose Next, assign a transport request and choose Finish.

  3. The binding opens in a form editor. Choose the activation button; the guide states that the binding must be active to be published.

  4. Choose Publish (the button that publishes the local service endpoint). The guide says this triggers several task lists that enable the service, and that it enables the service for the current system only.

  5. The editor now shows the service URL and the list of entity sets, and lets you open the metadata and the preview.

Publishing is local. In an on-premise system, the guide says, the binding can only enable the service locally for tests in the development system; the binding and the artifacts generated locally cannot be transported. To enable the service in a quality or production system, the local service is unpublished with the Unpublish button and the SAP Gateway tools are used to activate and maintain it, with a separate path for OData V4 services. What applies in the SAP BTP ABAP environment or in SAP S/4HANA Cloud Public Edition is not stated in that topic; follow the documentation of your environment for the transport of the service.

The steps need an SAP system and ADT; they were not run here. The topic about creating a binding in the guide shows the wizard for an OData V2 binding; the Web API topic shows the choice of the V4 binding type, and the OData UI service generator of the guide produces a service binding of the type OData V4 UI.

The service URL and a first request

The binding editor shows the service URL as a link. Do not construct it by hand if you can avoid it. The RAP guide documents the form of an OData V2 URL, /sap/opu/odata/<service binding name>, and shows $metadata appended to the service URL to get the metadata document. Examples published by SAP show that the V4 URL has a different shape: it starts with /sap/opu/odata4/sap/, then the name of the service binding, then a segment srvd (some examples show srvd_a2x) with the namespace and name of the service definition and a version number, 0001. SAP’s own example repositories show URLs such as the ones below; the names are theirs, not the expense claim’s.

Table 1. OData V4 service URLs as they appear in SAP example repositories
Example Shape of the path after the host

A UI service of a partner example

/sap/opu/odata4/sap/zpra_mf_ui_visitor_o4/srvd/sap/zpra_mf_visitor/0001/

A service of a forms example

/sap/opu/odata4/sap/zcine_app_ui_sb/srvd_a2x/sap/zcine_ticket_srvd/0001/

The two examples do not agree on the segment that follows the binding name, and the official guide does not describe the V4 form, so treat the shape as a pattern to compare with the link in the binding editor, not as a rule. For the running example the URL of a binding named ZUI_ExpClaim_O4 for the service definition ZSD_ExpClaim would follow the same pattern. The request below reads the first two claims from such a service and requests JSON. It uses the entity set Claim, the alias from the service definition. <host> and <service URL> stand for the values of your system, and the option -u "$SAP_USER" makes curl ask for the password so that it does not appear in the command line or in the shell history.

# Metadata document: lists the entity sets, their properties, and for a UI service the annotations.
curl -s -u "$SAP_USER" -H "Accept: application/xml" "https://<host><service URL>/$metadata"

# The first two claims, as JSON.
curl -s -u "$SAP_USER" -H "Accept: application/json" "https://<host><service URL>/Claim?\$top=2"

The listing requires an SAP system; not run here. The first request returns the metadata document in the Common Schema Definition Language, the format in which the RAP guide describes the metadata of the published service; the guide states that the document lists the entity types named after the CDS entities or their aliases, with their keys and properties. The second request uses the OData system query option $top to limit the number of entries; the backslash keeps the shell from reading $top as a variable. The system may also need a client number (the query parameter sap-client) or a different authentication method than basic authentication, depending on how it is set up; a request without the right logon fails with an HTTP error before any claim is read, and the business object is never reached.

Fiori elements preview

The guide describes a preview in the service binding that lets you see a list report for a published UI service without writing an app. In the service details section of the binding editor you select an entity set, such as Claim, and choose Preview; your browser opens a new tab with the SAP Fiori elements app. The features of the underlying business object are reflected there: the search, filter and sort capabilities defined in the CDS views, and, when the object is enabled for them, transactional and draft behavior. In the older V2 topic, the list shows no data until you choose columns with the settings button and press Go; this depends on the annotations: when the metadata extension of the claim, as shown on CDS Annotations and Metadata Extensions, carries @UI.lineItem, the columns appear on their own.

The guide is explicit that the preview is not a deployable app. It is a first entry point showing how a ready application could look; an app for production is built separately. The preview is available only for UI services: the Web API binding has no preview.

Value helps in the projection layer

A value help is the list that opens next to an input field, with the button in the field or the F4 key, so that a user can pick a valid value, for example an employee for a new claim. The guide names the projection layer as one place for it, together with UI annotations, calculations and defaulting, since it is service-specific. A value help is defined in CDS with the annotation @Consumption.valueHelpDefinition on the element, which names a CDS entity as the provider and the element of the provider whose value fills the field. The provider needs no association to the source view.

The projection listing above already contains the annotation on EmployeeId, pointing at ZI_ExpEmployeeVH with the element EmployeeId. That provider view is the employee value help of Views and Search Helps, which makes an employee findable by name and not only by number. The same guide topic says the provider view can carry @Consumption.valueHelpDefault.fetchValues: #AUTOMATICALLY_WHEN_DISPLAYED to load the full list when the dialog opens, @ObjectModel.resultSet.sizeCategory: #XS to show a dropdown list instead of a search dialog, and @Search.searchable: true on the provider to let the user type ahead. The guide recommends writing the value help annotation in the data definition, not in the metadata extension, although it is a UI feature.

Value helps work only in a UI service. The metadata of a UI service lists every field of the provider view as a value list parameter in its annotations; the guide’s Web API topic says the Web API of the same views does not contain that UI-specific list, even though the annotation is defined in the CDS views.

This agrees with the comparison table of the service binding topic: value help is listed as not available for a Web API and available for a UI service. A Web API metadata document does not describe how to display the value help.

Generated services

Writing all these objects by hand is rarely necessary. ADT has generators that produce the whole stack from a database table, from a CDS view entity or from nothing. The guide describes three: OData UI Service, which generates a managed business object with draft capabilities and exposes it as an OData V4 UI service with a metadata extension, OData Web API Service, which does the same without the UI parts, and OData UI Service from Scratch, which starts from an empty package. The generated objects are normal repository objects that you can edit afterwards, to add a value help, an action or a validation.

The wizard from scratch has limits that the guide lists: one business object, UUID key fields, managed with internal early numbering, no actions, determinations or validations, no Web APIs, no associations, and only a base layer and a projection layer. The generators produce layers for the whole stack in one run; the sections above are what you read to understand and change the result. The page on drafts covers what the draft part of a generated object means.

What older books show, and what has changed

  • Books on the ABAP Programming Model for SAP Fiori (the model of the AS ABAP 7.5x era) describe consumption views and BOPF business objects and publish the service in other ways. This page describes the RAP way: projection views, a service definition and a service binding. The older ways still exist in old systems but are not how new development is done.

  • Older consumption views were CDS views on top of a DDIC SQL view. A CDS projection view creates no extra SQL view and needs no ABAP Dictionary representation.

  • Many tutorials show OData V2 bindings and /sap/opu/odata/ URLs. The guide advises OData V4 for transactional services where possible; V2 remains available, and its URL shape differs from V4.

Summary

  • A projection is the first layer specific to one service: CDS projection views cut and rename the elements, redirect the composition to the projected child and carry UI annotations and value helps; a projection behavior definition lists with use the operations the service offers.

  • A service definition, written with define service and expose, names the entities of the service under aliases that clients see; it is independent of the protocol and has to be bound.

  • A service binding chooses protocol and type, here OData V4 with the UI or the Web API type, and is activated and published in ADT; publishing enables the service in the current system only.

  • The UI service carries UI annotations and has a Fiori elements preview; the Web API does not. The service URL is shown in the binding editor, and the V4 URL differs in shape from the V2 URL.

  • A first request is a GET with curl on $metadata or on an entity set such as Claim, which needs the right logon.

  • Value helps are defined in the projection layer with @Consumption.valueHelpDefinition, and the generators of ADT can produce the whole stack from a table.