Module 10: OData Model Basics
OData is the primary integration mechanism between SAPUI5 applications and the SAP backend.
Understanding OData models is mandatory for building real-world Fiori applications.
This module explains what OData is, how UI5 consumes it, and how to handle errors properly.
1. What is OData?
Definition
OData (Open Data Protocol) is a REST-based protocol used to:
- Expose business data
- Perform CRUD operations
- Enable standard, URL-based access
Almost all SAPUI5 applications consume data via OData services.
2. Key Characteristics
OData is:
- RESTful (HTTP-based)
- Stateless
- Supports CRUD
- Uses standard HTTP verbs
- Returns JSON or XML
3. Example OData URL
/sap/opu/odata/sap/Z_SALES_SRV/SalesOrderSet
This URL retrieves all sales orders from the Z_SALES_SRV service.
Service URLs follow a consistent naming pattern in SAP systems.
4. OData Versions
| Version | Status | Usage |
|---|---|---|
| V2 | Legacy | Existing apps |
| V4 | Modern | New S/4HANA apps |
Prefer OData V4 for new developments.
5. Creating ODataModel in UI5
Declarative (manifest.json)
"models": {
"": {
"type": "sap.ui.model.odata.v2.ODataModel",
"settings": {
"serviceUrl": "/sap/opu/odata/sap/Z_SALES_SRV/"
}
}
}
Programmatic
var oModel = new sap.ui.model.odata.v2.ODataModel(
"/sap/opu/odata/sap/Z_SALES_SRV/",
{
json: true
}
);
this.getView().setModel(oModel);
Use manifest.json for configuration.
6. Reading Data from OData
Using ODataModel with Binding
<List items="{/SalesOrderSet}">
<StandardListItem
title="{SalesOrderID}"
description="{CustomerName}" />
</List>
UI5 automatically requests data from OData.
Reading Specific Entity
var oModel = this.getView().getModel();
oModel.read("/SalesOrderSet('123')", {
success: function (oData) {
console.log(oData);
},
error: function (oError) {
console.log(oError);
}
});
Use binding for lists, read() for details.
7. Creating Data (POST)
var oNewOrder = {
SalesOrderID: "999",
CustomerName: "Acme Corp"
};
oModel.create("/SalesOrderSet", oNewOrder, {
success: function (oData) {
sap.m.MessageToast.show("Order created");
},
error: function (oError) {
sap.m.MessageBox.error("Creation failed");
}
});
Always provide error callbacks.
8. Updating Data (PUT/PATCH)
oModel.update("/SalesOrderSet('123')", {
CustomerName: "New Name"
}, {
success: function () {
sap.m.MessageToast.show("Updated");
},
error: function (oError) {
sap.m.MessageBox.error("Update failed");
}
});
9. Deleting Data (DELETE)
oModel.remove("/SalesOrderSet('123')", {
success: function () {
sap.m.MessageToast.show("Deleted");
},
error: function (oError) {
sap.m.MessageBox.error("Delete failed");
}
});
Always confirm before deleting.
10. OData vs JSONModel
| Aspect | ODataModel | JSONModel |
|---|---|---|
| Data Source | Backend | Client-side |
| CRUD | Full support | Manual |
| Performance | Optimized | Simple |
| Use Case | Production | Testing |
Production apps use ODataModel.
11. Interview-Grade Explanation
Q: What is OData and why is it important in SAPUI5?
Answer:
OData is a REST-based protocol that enables standard data access from SAP backends. SAPUI5 uses ODataModel to consume OData services declaratively through binding or programmatically through read/create/update/delete methods, making it the primary integration mechanism for Fiori applications.
12. Summary
- OData is REST-based protocol for data access
- ODataModel is the UI5 adapter
- Data binding integrates seamlessly
- CRUD operations are straightforward
- Error handling is mandatory
- V2 is legacy, V4 is modern
13. What's Next?
➡️ Module 11: Lists & Tables
Mastering OData Model makes you a productive SAPUI5 developer.