The name of the EntitySet. It can start with a "/" (slash)
modelName?
string
No
undefined
The name of the OData V2 model which can be found on the manifest.json file. Leave this parameter undefined if the name of the OData model = "" (empty string)
Tip for TypeScript
The ODataReadCL<EntityT, EntityKeysT> is a generic class and can be initialized with 2 types.
The EntityT type contains all properties of the EntitySet that is specified on the class constructor.
The EntityKeysT type contains the key properties of the EntitySet that is specified on the class constructor.
The EntityT type is used as the returning type of the read(): Promise<EntityT[]> and readByKey(keys: EntityKeysT): Promise<EntityT> methods.
The EntityKeysT type is used as the type for the keys parameter of the readByKey(keys: EntityKeysT): Promise<EntityT> method.
importControllerfrom"sap/ui/core/mvc/Controller";importODataReadCLfrom"ui5/antares/odata/v2/ODataReadCL";// Import the class/** * @namespace your.apps.namespace */exportdefaultclassYourControllerextendsController{publiconInit(){}publicasynconReadProduct(){// Initialize without a typeconstodata=newODataReadCL(this,"Products");}publicasynconReadCategory(){// Initialize with a typeconstodata=newODataReadCL<ICategory,ICategoryKeys>(this,"Categories");}publicasynconReadCustomer(){// Initialize with a model nameconstodata=newODataReadCL(this,"Customers","myODataModelName");}}interfaceICategory{ID:string;name:string;}interfaceICategoryKeys{ID:string;}
sap.ui.define(["sap/ui/core/mvc/Controller","ui5/antares/odata/v2/ODataReadCL"// Import the class],/** * @param {typeof sap.ui.core.mvc.Controller} Controller */function(Controller,ODataReadCL){"use strict";returnController.extend("your.apps.namespace.YourController",{onInit:function(){},onReadProduct:asyncfunction(){// Initializeconstodata=newODataReadCL(this,"Products");},onReadCategory:asyncfunction(){// Initialize with a model nameconstodata=newODataReadCL(this,"Categories","myODataModelName");}});});
Read Request (GET EntitySet)
To send a GET (READ) request through the OData V2 model, you can use the read(): Promise<EntityT[]> method.
Info
The read() method runs asynchronously and can be awaited.
If the request is successful, the read() method will return the data (multiple) of the target entity.
Tip
By default, the read() method of the sap.ui.model.odata.v2.ODataModel class returns an object that contains a single property named results. However, the OData Read class destructs the original object and returns the value of the results property which is an array containing the data of the EntitySet. The return type is derived from the generic EntityT type.
Warning
In the event of a failed GET request, the OData Read class will generate an error message. To ensure the error is identified and addressed, it is recommended to call the read() method within a try-catch block.
Error Type
In the event of a failed GET request, the object generated by the class can contain the properties outlined below.
importControllerfrom"sap/ui/core/mvc/Controller";importODataReadCLfrom"ui5/antares/odata/v2/ODataReadCL";// Import the classimport{IError}from"ui5/antares/types/common";// Import the error typeimportMessageBoxfrom"sap/m/MessageBox";/** * @namespace your.apps.namespace */exportdefaultclassYourControllerextendsController{publiconInit(){}publicasynconReadProducts(){// Initialize with a typeconstodata=newODataReadCL<IProducts,IProductKeys>(this,"Products");try{// send the http request and get the result.constresult=awaitodata.read();result.forEach((row:IProducts)=>{// write your logic});}catch(error){// catch the errorMessageBox.error((errorasIError).message||"Request failed");}}}interfaceIProducts{ID:string;name:string;description:string;brand:string|null;price:number;currency:string;quantityInStock:number;categoryID:string|null;supplierID:string|null;}interfaceIProductKeys{ID:string;}
sap.ui.define(["sap/ui/core/mvc/Controller","ui5/antares/odata/v2/ODataReadCL",// Import the class"sap/m/MessageBox"],/** * @param {typeof sap.ui.core.mvc.Controller} Controller */function(Controller,ODataReadCL,MessageBox){"use strict";returnController.extend("your.apps.namespace.YourController",{onInit:function(){},onReadProducts:asyncfunction(){// Initializeconstodata=newODataReadCL(this,"Products");try{// send the http request and get the result.constresult=awaitodata.read();result.forEach((row)=>{// write your logic});}catch(error){// catch the errorMessageBox.error(error.message||"Request failed");}}});});
Read By Key Request (GET Entity)
To send a GET (READ) request to retrieve a single data through the OData V2 model, you can use the readByKey(keys: EntityKeysT): Promise<EntityT> method.
Info
The readByKey() method runs asynchronously and can be awaited.
If the request is successful, the readByKey() method will return the data (single) of the target entity.
Warning
In the event of a failed GET request, the OData Read class will generate an error message. To ensure the error is identified and addressed, it is recommended to call the readByKey() method within a try-catch block.
Error Type
In the event of a failed GET request, the object generated by the class can contain the properties outlined below.
importControllerfrom"sap/ui/core/mvc/Controller";importODataReadCLfrom"ui5/antares/odata/v2/ODataReadCL";// Import the classimport{IError}from"ui5/antares/types/common";// Import the error typeimportMessageBoxfrom"sap/m/MessageBox";/** * @namespace your.apps.namespace */exportdefaultclassYourControllerextendsController{publiconInit(){}publicasynconReadSingleProduct(){// Initialize with a typeconstodata=newODataReadCL<IProducts,IProductKeys>(this,"Products");try{// send the http request and get the result. Note: you need to specify the key values of the entity to read a single dataconstresult=awaitodata.readByKey({ID:"f60481d8-3426-4a91-a6a3-5e445a7deb46"});MessageBox.information("Product with ID: "+result.ID);}catch(error){// catch the errorMessageBox.error((errorasIError).message||"Request failed");}}}interfaceIProducts{ID:string;name:string;description:string;brand:string|null;price:number;currency:string;quantityInStock:number;categoryID:string|null;supplierID:string|null;}interfaceIProductKeys{ID:string;}
sap.ui.define(["sap/ui/core/mvc/Controller","ui5/antares/odata/v2/ODataReadCL",// Import the class"sap/m/MessageBox"],/** * @param {typeof sap.ui.core.mvc.Controller} Controller */function(Controller,ODataReadCL,MessageBox){"use strict";returnController.extend("your.apps.namespace.YourController",{onInit:function(){},onReadProduct:asyncfunction(){// Initializeconstodata=newODataReadCL(this,"Products");try{// send the http request and get the result. Note: you need to specify the key values of the entity to read a single dataconstresult=awaitodata.readByKey({ID:"f60481d8-3426-4a91-a6a3-5e445a7deb46"});MessageBox.information("Product with ID: "+result.ID);}catch(error){// catch the errorMessageBox.error(error.message||"Request failed");}}});});
URL Parameters
Prior to sending the GET request with the read() or readByKey() method, it is possible to set the URL parameters using the setUrlParameters() method.
Parameter
Type
Mandatory
Description
urlParameters
Record<string, string>
Yes
The URL parameters of the GET request
Returns
Description
Record<string, string> | undefined
Returns the value that was set using setUrlParameters() method. Default value is undefined
importControllerfrom"sap/ui/core/mvc/Controller";importODataReadCLfrom"ui5/antares/odata/v2/ODataReadCL";// Import the class/** * @namespace your.apps.namespace */exportdefaultclassYourControllerextendsController{publiconInit(){}publicasynconReadProduct(){// Initialize with a typeconstodata=newODataReadCL<IProducts,IProductKeys>(this,"Products");// set the url parametersodata.setUrlParameters({"$expand":"toProductLocations"});}}interfaceIProducts{ID:string;name:string;description:string;brand:string;price:number;currency:string;quantityInStock:number;categoryID:string;supplierID:string;}interfaceIProductKeys{ID:string;}
importControllerfrom"sap/ui/core/mvc/Controller";importODataReadCLfrom"ui5/antares/odata/v2/ODataReadCL";// Import the classimportFilterfrom"sap/ui/model/Filter";// Import Filter classimportFilterOperatorfrom"sap/ui/model/FilterOperator";// Import FilterOperator enum/** * @namespace your.apps.namespace */exportdefaultclassYourControllerextendsController{publiconInit(){}publicasynconReadProduct(){// Initialize with a typeconstodata=newODataReadCL<IProducts,IProductKeys>(this,"Products");constnameFilter=newFilter("name",FilterOperator.Contains,"smartphone");constpriceFilter=newFilter("price",FilterOperator.GT,1500);// add the filters one by oneodata.addFilter(nameFilter);odata.addFilter(priceFilter);}publicasynconReadCategory(){// Initializeconstodata=newODataReadCL(this,"Categories");constnameFilter=newFilter("name",FilterOperator.Contains,"smartphone");constpriceFilter=newFilter("price",FilterOperator.GT,1500);// set the filters at onceodata.setFilters([nameFilter,priceFilter]);}}interfaceIProducts{ID:string;name:string;description:string;brand:string;price:number;currency:string;quantityInStock:number;categoryID:string;supplierID:string;}interfaceIProductKeys{ID:string;}
sap.ui.define(["sap/ui/core/mvc/Controller","ui5/antares/odata/v2/ODataReadCL",// Import the class"sap/ui/model/Filter",// Import Filter class"sap/ui/model/FilterOperator"// Import FilterOperator enum],/** * @param {typeof sap.ui.core.mvc.Controller} Controller */function(Controller,ODataReadCL,Filter,FilterOperator){"use strict";returnController.extend("your.apps.namespace.YourController",{onInit:function(){},onReadProduct:asyncfunction(){// Initializeconstodata=newODataReadCL(this,"Products");constnameFilter=newFilter("name",FilterOperator.Contains,"smartphone");constpriceFilter=newFilter("price",FilterOperator.GT,1500);// add the filters one by oneodata.addFilter(nameFilter);odata.addFilter(priceFilter);},onReadCategory:asyncfunction(){// Initializeconstodata=newODataReadCL(this,"Categories");constnameFilter=newFilter("name",FilterOperator.Contains,"smartphone");constpriceFilter=newFilter("price",FilterOperator.GT,1500);// set the filters at onceodata.setFilters([nameFilter,priceFilter]);}});});
Sorters
To execute the read() method with sorters, two different methods are available.
This method enables the user to add sorters individually.
importControllerfrom"sap/ui/core/mvc/Controller";importODataReadCLfrom"ui5/antares/odata/v2/ODataReadCL";// Import the classimportSorterfrom"sap/ui/model/Sorter";// Import the Sorter class/** * @namespace your.apps.namespace */exportdefaultclassYourControllerextendsController{publiconInit(){}publicasynconReadProduct(){// Initialize with a typeconstodata=newODataReadCL<IProducts,IProductKeys>(this,"Products");constnameSorter=newSorter("name");constpriceSorter=newSorter("price",true);// descending// add the sorters one by oneodata.addSorter(nameSorter);odata.addSorter(priceSorter);}publicasynconReadCategory(){// Initializeconstodata=newODataReadCL(this,"Categories");constnameSorter=newSorter("name");constpriceSorter=newSorter("price",true);// descending// set the sorters at onceodata.setSorters([nameSorter,priceSorter]);}}interfaceIProducts{ID:string;name:string;description:string;brand:string;price:number;currency:string;quantityInStock:number;categoryID:string;supplierID:string;}interfaceIProductKeys{ID:string;}
sap.ui.define(["sap/ui/core/mvc/Controller","ui5/antares/odata/v2/ODataReadCL",// Import the class"sap/ui/model/Sorter"// Import the Sorter class],/** * @param {typeof sap.ui.core.mvc.Controller} Controller */function(Controller,ODataReadCL,Sorter){"use strict";returnController.extend("your.apps.namespace.YourController",{onInit:function(){},onReadProduct:asyncfunction(){// Initializeconstodata=newODataReadCL(this,"Products");constnameSorter=newSorter("name");constpriceSorter=newSorter("price",true);// descending// add the sorters one by oneodata.addSorter(nameSorter);odata.addSorter(priceSorter);},onReadCategory:asyncfunction(){// Initializeconstodata=newODataReadCL(this,"Categories");constnameSorter=newSorter("name");constpriceSorter=newSorter("price",true);// descending// set the sorters at onceodata.setSorters([nameSorter,priceSorter]);}});});
Additional Response Info
The read() and the readByKey() methods return the data of the target entity. However, you may require further information such as the status code and headers.
Once the read() or the readByKey() function has been completed, the getResponse() method can be utilized to obtain further details.
importControllerfrom"sap/ui/core/mvc/Controller";importODataReadCLfrom"ui5/antares/odata/v2/ODataReadCL";// Import the classimport{IError}from"ui5/antares/types/common";// Import the error typeimportMessageBoxfrom"sap/m/MessageBox";/** * @namespace your.apps.namespace */exportdefaultclassYourControllerextendsController{publiconInit(){}publicasynconReadProduct(){// Initialize with a typeconstodata=newODataReadCL<IProducts,IProductKeys>(this,"Products");try{// send the http request and get the resultconstresult=awaitodata.readByKey({ID:"3ccb5dd2-cc12-483a-b569-a6ec844f8f0b"});MessageBox.information(result.ID+" was fetched.");// get the additional response infoconstresponse=odata.getResponse();if(response){console.log("Status Code: "+response.statusCode);}}catch(error){// catch the errorMessageBox.error((errorasIError).message||"Request failed");}}}interfaceIProducts{ID:string;name:string;description:string;brand:string;price:number;currency:string;quantityInStock:number;categoryID:string;supplierID:string;}interfaceIProductKeys{ID:string;}
sap.ui.define(["sap/ui/core/mvc/Controller","ui5/antares/odata/v2/ODataReadCL",// Import the class"sap/m/MessageBox"],/** * @param {typeof sap.ui.core.mvc.Controller} Controller */function(Controller,ODataReadCL,MessageBox){"use strict";returnController.extend("your.apps.namespace.YourController",{onInit:function(){},onReadProduct:asyncfunction(){// Initializeconstodata=newODataReadCL(this,"Products");try{// send the http request and get the resultconstresult=awaitodata.readByKey({ID:"3ccb5dd2-cc12-483a-b569-a6ec844f8f0b"});MessageBox.information(result.ID+" was fetched.");// get the additional response infoconstresponse=odata.getResponse();if(response){console.log("Status Code: "+response.statusCode);}}catch(error){// catch the errorMessageBox.error(error.message||"Request failed");}}});});