Show TOC

OData Write SupportLocate this document in the navigation structure

For data binding with the OData model, SAPUI5 supports several features.

Note As these features are experimental, changes in future versions of SAPUI5 may occur. Currently, all write operations have to be performed by the application and are triggered synchronously.

SAPUI5 supports the following operations and features for the OData model:

  • write
  • create
  • remove
  • update
  • read
  • get metadata service document
  • Cross-site request forgery (XSRF) token support for the REST library
  • refresh

The default binding mode is sap.ui.model.BindingMode.OneWay. You can also set the binding mode to sap.ui.model.BindingMode.TwoWay.

Create Request

The create function triggers a POST request to an OData service which was specified during creation of the OData model.

The application has to specify the collection where to create the entry and the entry data payload.

To get the result of the request, the application can hand over callback handler functions. To recreate and update the bindings, a refresh is triggered automatically after the successful creation.

Example

The following code triggers a new entry in the Products collection:

var oEntry = {};
oEntry.Name = "IPad";
oEntry.Price = "499$";
oModel.create('/Products', oEntry, null, function(){
 		alert("Create successful");
 	},function(){
		alert("Create failed");});
Delete Request

The remove function triggers a DELETE request to an OData service which was specified during creation of the OData model. The application has to specify the path to the entry which should be deleted. To update the bindings in the model, a refresh is triggered automatically after successful deletion.

A single parameter oParameters can be passed into the function and can carry all optional attributes, such as attributes for success and error handling functions as well as ETag attributes. ETags can be used for concurrency control if the OData service is configured to provide them. For more information, see the section about concurrency control and ETags in the OData documentation.

If an ETag is specified in oParameters, it will be used in the If-Match-Header instead of any ETag found in the metadata of an entry. If not, the ETag is retrieved from the entry's metadata. If no ETag is found, no If-Match-Header will be used.

Example

The following code deletes the product entry with ID=1 from the Products collection of the data service:

oModel.remove('/Products(1)', null, function(){
 		alert("Delete successful");
 	},function(){
		alert("Delete failed");});
Update Request

The update function triggers a PUT request to an OData service which was specified during creation of the OData model. See the ODataModel API for information about attribute use.

The application has to specify the path to the entry which should be updated with the specified updated entry.

After a successful request to update the bindings in the model, a refresh is triggered automatically.

Example

The following code updates the price:

var oEntry = {};
oEntry.Price = "599$";
oModel.update('/Products(1)', oEntry, null, function(){
 		alert("Update successful");
 	},function(){
		alert("Update failed");});

The flag bMerge has been introduced to also allow a MERGE request to perform a differential update.

Read Request

The read function triggers a GET request to a specified path which should be retrieved from the OData service which was specified during creation of the OData model.

The retrieved data is returned in the success callback handler function.

Example
oModel.read('/Products(1)', null, null, true, function(oData, oResponse){
 		alert("Read successful: " + JSON.stringify(oData));
 	},function(){
		alert("Read failed");});
Refresh

The Refresh function triggers a refresh for each binding to check if a value has been updated, or not. For a list binding a new request is triggered to reload the data from the server.

Additionally, if the XSRF token is not valid any more, a read request is triggered to fetch a new XSRF token from the server.

XSRF Token

To address the challenge of Cross Site Request Forgery an OData service might require XSRF tokens for change requests by the client application for security reasons. In this case the client has to fetch a token from the server and send it with each change request to the server.

The OData model fetches the XSRF token when reading the metadata and then automatically sends it in each write request header. If the token is not valid any more a new token can be fetched by calling the refresh function on the OData model.

Metadata

The getServiceMetadata function returns the parsed metadata document as a JavaScript object.