No history yet

OData V4 Annotations

The Language of Fiori

So, you've built a Core Data Services (CDS) view and exposed it as an OData service. How does your backend ABAP system tell the frontend SAP Fiori application how to display that data? The answer is annotations. Think of annotations as a set of instructions, or metadata, that you attach to your data model. Instead of writing complex UI code, you simply 'annotate' your data elements to describe how they should look and behave.

SAP Fiori Elements takes this a step further. It's a framework that reads these annotations and automatically generates the user interface for you. No custom JavaScript is required for most standard applications. This approach, known as declarative UI, means you define what the UI should do, not how it should do it. The framework handles the rest, leading to faster development and a consistent user experience across applications.

Annotation

noun

Metadata added to a data model that provides semantic details or hints to the consumer of the data, such as a user interface.

The real power comes from using predefined sets of terms and rules called 'vocabularies'. SAP provides several, but the most important one for Fiori Elements is the UI vocabulary. It contains all the terms you need to describe how data should be presented, from simple labels to complex chart configurations.

From OData V2 to V4: A Big Leap

The way annotations are handled changed significantly between OData V2 and OData V4. Understanding this difference is key to working with modern S/4HANA systems.

In the OData V2 world, annotations were often added in a separate, clunky metadata file within the SAP Gateway. This approach worked, but it separated the data model from its presentation layer, making maintenance a hassle. It also led to larger, more 'chatty' network requests.

With the introduction of the ABAP RESTful Application Programming Model (RAP), SAP fully embraced OData V4. In this model, annotations are defined directly within the CDS Views themselves. This is a huge improvement. Your UI definitions now live right next to your data definitions, making your code more cohesive and easier to understand.

FeatureOData V2 AnnotationsOData V4 Annotations
LocationSeparate MPC/DPC classes in GatewayDirectly in CDS View files
FormatXML-based, often in a metadata fileSimple, clear syntax inside CDS
PayloadLarger, more verbose (XML)More compact (JSON)
ApproachImperative (more coding)Declarative (less coding)

This shift to OData V4 means less boilerplate code, better performance due to smaller data payloads, and a much cleaner development process. You define the UI's structure once in the backend, and it's automatically reflected in the front end.

Building a UI with Annotations

Let's see how this works in practice. The core of a Fiori Elements application is built using a few key annotations. These are typically defined in a special type of CDS view called a Metadata Extension file, which keeps your UI-specific code separate from your core business logic.

A best practice is to define your core data model in a CDS Interface View (DEFINE VIEW) and add your UI annotations in a separate Metadata Extension (ANNOTATE VIEW).

The most common UI annotation is UI.lineItem. This annotation specifies which fields should appear as columns in a list report or table. Each item in the list represents a DataField record.

@UI.lineItem: [ { position: 10, label: 'Product ID' } ]
ProductID;

@UI.lineItem: [ { position: 20, label: 'Product Name' } ]
ProductName;

@UI.lineItem: [ { position: 30, label: 'Price' } ]
Price;

The position property determines the order of the columns, and label sets the column header text. Simple, right?

Next up are UI.selectionField annotations. These define the filter fields that appear at the top of a list report, allowing users to search and narrow down the data.

@UI.selectionField: [ { position: 10 } ]
ProductID;

@UI.selectionField: [ { position: 20 } ]
SupplierID;

When you navigate from a list to a specific item, you land on the object page. The content here is organized into sections using UI.facet annotations. A common type of facet is a FieldGroup, which groups related fields together under a common heading.

First, you define the FieldGroup and give it a qualifier (a unique name). Then, you reference this FieldGroup in your UI.facet definition.

// Step 1: Define the FieldGroup for general data
@UI.fieldGroup: [ { 
    qualifier: 'GeneralInfo',
    position: 10, 
    label: 'Product ID' 
} ]
ProductID;

@UI.fieldGroup: [ { 
    qualifier: 'GeneralInfo', 
    position: 20, 
    label: 'Description' 
} ]
Description;

// Step 2: Create a Facet on the object page to display this group
@UI.facet: [ {
    id: 'General',
    purpose: #STANDARD,
    type: #FIELDGROUP_REFERENCE,
    label: 'General Information',
    targetQualifier: 'GeneralInfo'
} ]

This creates a section on the object page titled "General Information" containing the Product ID and Description fields. You can create multiple field groups and facets to organize your UI logically.

Finally, the HeaderInfo annotation controls what appears in the header of the object page, giving the user immediate context.

@UI.headerInfo: {
    typeName: 'Product',
    typeNamePlural: 'Products',
    title: {
        type: #STANDARD,
        value: 'ProductName'
    },
    description: {
        type: #STANDARD,
        value: 'ProductCategory'
    }
}
annotate view ZC_PRODUCT_CDS with
...

This tells the UI to use the ProductName field as the main title and ProductCategory as the subtitle in the header. The typeName and typeNamePlural are used in various places, like window titles.

One last point to remember is annotation priority. If an annotation is defined in multiple places (for example, in the base CDS view and also in a metadata extension), the one closest to the final service definition wins. This means a metadata extension will always override an annotation in the base view, which is a powerful way to customize UIs without changing the core business logic.

Ready to test your knowledge? Let's see what you've learned about Fiori annotations.

Quiz Questions 1/5

What is the primary role of annotations when building a SAP Fiori Elements application with CDS views?

Quiz Questions 2/5

Compared to the older OData V2 approach, what is a major benefit of defining annotations directly within CDS views as part of the ABAP RESTful Application Programming Model (RAP)?