CDS Annotations and Metadata Extensions
|
This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production. |
A CDS view says which data you can read, but it does not say how a screen should show that data: which columns come first,
what the column headings are, which amount belongs to which currency, or which field is a filter. That extra meaning is
written as annotations, short lines that start with @ and sit in front of a CDS entity or one of its fields. A newcomer
meets annotations on the first CDS view, because the standard template already contains some, and meets them again on
every SAP Fiori app built with the ABAP RESTful Application Programming Model. This page explains what an annotation is,
where you may write one, the two families SAP delivers, the annotations you will use most, how the list report and the
object page of an SAP Fiori app are described with @UI annotations, and how a metadata extension moves those annotations
into a file of their own and lets partners and customers override them. It is written against ABAP for Cloud Development
release 9.20 (2608) and Standard ABAP release 8.16. Annotations and metadata extensions belong to ABAP CDS, so they apply in
ABAP Cloud, in classic Standard ABAP, on premise and in the Public Edition alike; which annotations are released for ABAP
Cloud is shown in the documentation with the entry API of each annotation.
Before you read
-
CDS view entities, the objects that carry the annotations, have a page of their own, CDS View Entities; this page uses
define view entityin the examples and explains what each line does. -
ABAP Dictionary: Tables and Structures: the table
zexp_claimthat the examples read from. -
Currencies, Units and Conversions: why an amount needs a currency key, which one of the annotations below declares.
-
ABAP Language Versions and Released APIs: what ABAP Cloud allows, which decides the annotations you may use.
Key terms
-
CDS annotation: a line that starts with
@and adds metadata to a CDS entity or to one of its elements. -
CDS entity: the common name for an object defined in CDS, such as a view entity, that you can read with ABAP SQL.
-
CDS view entity: the current kind of CDS view, written with
define view entity. -
ABAP annotation: an annotation that the ABAP runtime itself evaluates, for example when the entity is activated.
-
Framework-specific annotation: an annotation that another SAP component reads, such as the OData service or the SAP Fiori user interface.
-
Metadata extension: a separate piece of CDS source code that adds annotations to an existing CDS entity without changing its definition.
-
Annotation layer: the level of a metadata extension (core, localization, industry, partner or customer) that decides which extension wins.
-
SAP Fiori elements: the SAP UI technology that builds list and detail pages from the annotations of a service, with no screen code.
What an annotation is
Think of the sticky notes a colleague puts on a printed report: "this column is money, the currency is in the next
column", "show this column first", "call this column Claim number". The report itself does not change, but whoever reads it
now knows how to treat it. A CDS annotation does the same for a CDS entity. It is stored with the entity when the entity is
activated, and it goes beyond what the SQL part of the definition can say. Programs and frameworks read it back; the
system class CL_DD_DDL_ANNOTATION_SERVICE is the API that evaluates the annotations of CDS entities.
Two rules keep annotations orderly. First, only the annotations that SAP delivers can be used: every annotation has an
annotation definition, and partners and customers cannot define their own at present. Second, a value must follow its
definition: the right type, and for a fixed list of choices one of the listed enumeration symbols, which are written with a
leading # such as #NOT_REQUIRED. An annotation with no definition is not evaluated, and an annotation that no framework
evaluates is simply ignored.
Where an annotation can be written
An annotation is allowed only in the places its definition names, called its scopes. The most common scopes are the whole
entity (written above define view entity), an element (written directly above one field of the select list), and a
parameter of the entity. Other scopes cover the other CDS objects, for example #ROLE for access control, #SERVICE for a
service definition and #ANNOTATE for a metadata extension. The documentation lists every scope with the place in the source
where it applies. If you write an annotation in a place that its scope does not cover, the syntax check reports an error.
There is one placement detail worth knowing. For most CDS entities an element annotation can stand before the element
(@name) or after it (@<name). For CDS view entities, which is what you write today, element annotations stand only
before the element they refer to.
Two families: ABAP annotations and framework-specific annotations
SAP delivers its annotations in two families. They look the same in the source; they differ in who reads them.
| ABAP annotations | Framework-specific annotations | |
|---|---|---|
Evaluated by |
The ABAP runtime, at activation of the entity or when the entity is used in the ABAP runtime environment |
The framework of another software component, through suitable APIs |
What they describe |
Technical and semantic attributes of the CDS object; they can change how ABAP SQL treats the entity |
Properties needed by OData, the user interface or analytics |
Groups, for example |
|
|
Relevant to activation |
Yes |
No |
Some groups, such as Semantics, appear in both families because the individual annotations differ. The documentation marks
each annotation with its kind, its scopes, its type, its enumeration symbols, its default, whether it is translatable text
(Text), whether it may appear in a metadata extension (MDE) and whether it is released as an API (API). The default is
the value applied when the annotation is written without a value, which can differ from what applies when the annotation is
left out altogether.
Annotation syntax
An annotation is a name that starts with @, a colon and a value. Names with sub-parts are written either as a chain or as
a group in braces; the two lines below say the same. A list of values, called an annotation array, is always written with
square brackets, even when it has one element.
@UI.headerInfo.typeName: 'Expense claim'
@UI.headerInfo.typeNamePlural: 'Expense claims'
@UI.headerInfo: { typeName: 'Expense claim', typeNamePlural: 'Expense claims' }
@UI.lineItem: [ { position: 10, label: 'Claim number' } ]
Strings go in single quotes, numbers stand as they are, and a value from a fixed list is an enumeration symbol such as
#HIGH. In an annotation array each element is a group in braces. This listing is only about notation, it does not belong
to an object, and it requires an SAP system; not run here.
An annotated interface view for the running example
The running Expense claims example keeps its data in the table zexp_claim (see the Dictionary page). The first CDS entity
over it is an interface view that describes the claim in business terms and carries the annotations that belong to the data
itself: a label for the entity, the access control setting, the link between an amount and its currency, and the link
between a status code and its text. It requires an SAP system; not run here, because it needs the CDS compiler of an ABAP
system.
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Expense claim'
@Metadata.ignorePropagatedAnnotations: true
define view entity ZI_ExpClaim
as select from zexp_claim
association [0..1] to zexp_claim_status_t as _StatusText
on $projection.Status = _StatusText.status
{
key claim_id as ClaimId,
employee_id as EmployeeId,
@ObjectModel.text.element: [ 'StatusText' ]
status as Status,
_StatusText.text as StatusText,
@Semantics.currencyCode: true
currency_code as CurrencyCode,
@Semantics.amount.currencyCode: 'CurrencyCode'
total_amount as TotalAmount
}
Source: ABAP Keyword Documentation: Semantics.amount.currencyCode, ObjectModel.text.element and AccessControl.authorizationCheck
Each annotation in the listing does one job:
-
@AccessControl.authorizationCheck: #NOT_REQUIREDsays that the entity needs no access control for now. With this value, an access control that exists is evaluated, and one that does not exist means every user may read the entity; it is the value SAP recommends for entities that may get roles later. The other values are#CHECK,#MANDATORY,#NOT_ALLOWEDand#PRIVILEGED_ONLY. A later page covers access control in detail. -
@EndUserText.labelgives the entity a readable name for tools and screens. -
@Semantics.currencyCode: truemarksCurrencyCodeas a currency key, and@Semantics.amount.currencyCodenames it as the currency ofTotalAmount. In a CDS view entity the second annotation is mandatory for every element of the Dictionary typeabap.curr, and it must point to an element of the same view. The framework that serves the data translates it into the matching OData annotation, so a screen shows the amount with the right number of decimals. -
@ObjectModel.text.elementlinks the code inStatusto the elementStatusTextthat holds its description. In OData services the first element of the array is treated as the descriptive text of the annotated field, so the user interface can show "Approved" next to or instead of the code. -
@Metadata.ignorePropagatedAnnotations: trueswitches off annotation propagation for the entity. Without it, annotations of the elements of the data source (for example from the table definition) flow into the view automatically; with it you start from a clean list.
The text table zexp_claim_status_t in the listing is a Dictionary table of the example that holds the status code and its
description; it is not created on this page.
The @UI annotations of an SAP Fiori app
Of all framework-specific annotations, the UI group matters most to a newcomer, because it describes a screen without
writing screen code. SAP Fiori elements builds a list report (the table page you see first) and an object page (the detail
page of one row) from the annotations of the OData service. The annotations you meet first are:
| Annotation | What it does |
|---|---|
|
Gives the singular and plural name of the thing ( |
|
Puts the element in the list report as a column. |
|
Shows a filter field for the element above the list. Its |
|
Defines the sections of the object page. A facet of type |
|
Puts the element on the object page, in the order of its |
The position value is a relative number, not a measured one: elements are arranged in ascending order of it, which is why
documentation and samples count in steps of ten so that a later extension can slip an element in between. The label of a
column or filter comes from the data element in the ABAP Dictionary unless you set one with @UI.lineItem.label or
@EndUserText.label.
Source: ABAP Keyword Documentation: UI.lineItem.position, UI.lineItem.importance, UI.headerInfo.typeName, UI.facet.type, RAP: Adding Selection Fields and RAP: Defining UI Annotations
You can write these annotations directly in the data definition, element by element. That works, but a view that has a dozen elements and four annotations on each one becomes hard to read, and a change of a column heading would mean changing the data model. SAP therefore recommends keeping all UI annotations in a metadata extension, which is the next topic.
Metadata extensions
A metadata extension (MDE for short) adds annotations to a CDS entity from a separate file. The file is a CDS object of its
own, written in DDLX source code and transported on its own. Alongside the DDL statements that define data, CDS DDL has the
statement ANNOTATE ENTITY that defines an extension; ANNOTATE VIEW does the same for a classic CDS view and is not used
for view entities. Metadata extensions can be created for any CDS entity except a CDS table function, and the DDLX source
code can be edited only in the ABAP development tools for Eclipse (ADT), where the entity’s context menu offers
New Metadata Extension.
Three advantages follow from keeping annotations out of the definition:
-
The definition stays readable because it does not list every piece of metadata.
-
The metadata can be written and transported independently of the data definition.
-
Activating a metadata extension does not force a mass activation of the repository objects that depend on the entity, which activating a changed data definition can.
Two rules make this safe. The entity has to allow it: its source must carry @Metadata.allowExtensions: true. When
the annotation is left out, the value is false, so a new view entity cannot be extended until you add it. And an extension
may contain only annotations whose definition allows it, which is the MDE column of the annotation tables; anything else
is a syntax error. Because the activation of the entity does not depend on its extensions, an extension can add only
annotations that are not already evaluated while the entity is activated, so ABAP annotations such as
@AccessControl.authorizationCheck stay in the data definition, and metadata for the user interface goes in the extension.
The example splits the claim into two entities. ZI_ExpClaimList is a list view over ZI_ExpClaim that opens the door
for extensions, and the metadata extension of the same name holds everything the list report and object page need. Both
listings require an SAP system; not run here.
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Expense claim list'
@Metadata.allowExtensions: true
define view entity ZI_ExpClaimList
as select from ZI_ExpClaim
{
key ClaimId,
EmployeeId,
Status,
StatusText,
CurrencyCode,
TotalAmount
}
@Metadata.layer: #CUSTOMER
@UI: {
headerInfo: {
typeName: 'Expense claim',
typeNamePlural: 'Expense claims',
title: { type: #STANDARD, value: 'ClaimId' }
}
}
annotate entity ZI_ExpClaimList with
{
@UI.facet: [ { type: #IDENTIFICATION_REFERENCE, label: 'General', position: 10 } ]
@UI.lineItem: [ { position: 10, label: 'Claim number', importance: #HIGH } ]
@UI.selectionField: [ { position: 10 } ]
@UI.identification: [ { position: 10 } ]
ClaimId;
@UI.lineItem: [ { position: 20, importance: #MEDIUM } ]
@UI.selectionField: [ { position: 20 } ]
@UI.identification: [ { position: 20 } ]
EmployeeId;
@UI.lineItem: [ { position: 30, importance: #HIGH } ]
@UI.selectionField: [ { position: 30 } ]
@UI.identification: [ { position: 30 } ]
Status;
@UI.lineItem: [ { position: 40, label: 'Total', importance: #HIGH } ]
@UI.identification: [ { position: 40 } ]
TotalAmount;
}
Source: RAP: Adding Metadata Extensions, CDS DDL - ANNOTATE ENTITY, VIEW and Metadata.allowExtensions
The extension has the same name as the entity it extends, which is the naming convention for metadata extensions. Reading it from the top:
-
@Metadata.layer: #CUSTOMERassigns the extension to a layer (next section). SAP’s own demo objects use#CORE; an extension in your ownZnamespace belongs to the customer layer. -
The annotations before
annotate entityare entity annotations, here@UI.headerInfo, and the facet of the object page sits at the top of the element list. -
Between the braces each element of the entity is named and annotated. An element that has no annotation does not need to appear.
-
@UI.lineItem: [ { ... } ]is an array, so the square brackets are required even for one column.
When this entity is exposed through an OData UI service and opened in SAP Fiori elements, the list report shows the columns
Claim number, Employee, Status and Total in that order, offers three filter fields, and shows the plural heading
Expense claims. If the screen is narrow, the columns marked #MEDIUM give way first. How the entity becomes a service,
and how the service is previewed, is covered on Projections, Service Definitions and OData V4 Bindings.
The same annotations drive the SAP Fiori elements preview that ADT offers on a service binding. Without any @UI
annotation the preview still shows a list with sorting and filtering, and the user picks the columns through the settings
icon.
Layers: which value wins
More than one metadata extension can exist for one entity. An extension for an application component can come from SAP,
another from an industry solution, another from a partner, and another from the customer. Each extension names its layer
with @Metadata.layer, and the layer sets the priority. In descending order of priority, the layers are:
| Layer | Relates to |
|---|---|
|
A customer extension of a product (highest priority). |
|
A partner extension of a product. |
|
An industry-specific extension of a product. |
|
The localization of a product. |
|
The core product, the ABAP Platform (lowest priority). |
When the system evaluates the annotations of an entity, it works in this order: the annotations of the metadata extensions come first, layer by layer from the highest; an annotation found in a higher layer is no longer searched in a lower one, so it overrides the lower value; finally the annotations of the entity itself that no extension supplied are added, which includes the ones written in the source and the inherited ones. Inside one layer, if two extensions give the same annotation, the first one found is used and the syntax check warns about the duplicate; the order is undefined but stable, so avoid the situation. Because of this design, a partner or customer can change a column heading or hide a field in the user interface without modifying SAP’s objects.
Finding the right annotation
The list of annotations is long, and nobody learns it by heart. Four places answer almost every question:
-
In ADT, put the cursor on an annotation and press the F2 key: the tooltip shows technical details and the annotation documentation.
-
The ABAP Keyword Documentation lists all ABAP annotations and all framework-specific annotations in tables with their scopes, values and defaults, and each annotation has a topic of its own.
-
The program
ABAP_DOCU_CDS_ANNOS_OVERVIEWshows all SAP annotations and their properties, and the programABAP_DOCU_MDE_ANNOSshows those that may appear in metadata extensions. Both run in an ABAP system. -
The ABAP Data Models guide explains recommended annotations for view entities, and the RAP guide explains the ones for business objects and Fiori apps.
Old material you will still meet
Older books and tutorials show CDS views defined as DDIC-based views with define view and an SQL view name, and the
documentation now marks those as obsolete: new code uses view entities. Such books also put every annotation into the data
definition, because they were written before metadata extensions became the usual place for UI annotations, and you will also
see the older ANNOTATE VIEW form, which is meant for classic CDS views only. The keyword documentation also warns that the
programs shown in it are syntax examples and are not meant for direct use in production. Use the current topics listed under
References, and check an annotation’s API entry before using it in ABAP Cloud.
Summary
-
A CDS annotation is metadata on a CDS entity or element, written with
@. Only annotations that SAP delivers can be used, and each is allowed only in the scopes of its definition. -
ABAP annotations are evaluated by the ABAP runtime; framework-specific annotations such as
UI,ObjectModelandConsumptionare evaluated by other frameworks, for example the OData service behind an SAP Fiori app. -
@Semantics.amount.currencyCode,@ObjectModel.text.elementand@EndUserText.labeltie amounts to currencies, codes to texts and fields to readable names. -
@UI.headerInfo,@UI.lineItem,@UI.selectionField,@UI.facetand@UI.identificationdescribe the list report and the object page of an SAP Fiori elements app. -
A metadata extension (
ANNOTATE ENTITY) keeps annotations in a separate, separately transported file, but only if the entity has@Metadata.allowExtensions: trueand only for annotations markedMDE. -
The layer of an extension (
#CORElowest,#CUSTOMERhighest) decides which value wins when several extensions give the same annotation.
References
-
ABAP Keyword Documentation: ABAP CDS - Framework-Specific Annotations
-
ABAP Keyword Documentation: ABAP CDS - Annotation Properties
-
ABAP Keyword Documentation: ABAP CDS - Scopes for Annotations
-
ABAP Keyword Documentation: ABAP CDS - SAP Annotation Documentation
-
ABAP Keyword Documentation: ABAP CDS - DDL for Metadata Extensions
-
ABAP Keyword Documentation: ABAP CDS - Evaluation of Metadata Extensions
-
ABAP Keyword Documentation: AccessControl.authorizationCheck
-
ABAP RESTful Application Programming Model: Defining CDS Annotations for Metadata-Driven UIs
-
ABAP RESTful Application Programming Model: Defining UI Annotations
-
ABAP RESTful Application Programming Model: Adding Metadata Extensions
-
ABAP RESTful Application Programming Model: Adding Selection Fields