Skip to Content

Consume an OData Service with Create Option

0 %
Consume an OData Service with Create Option

Consume an OData Service with Create Option

January 5, 2021
Created by
September 6, 2016
Consume an OData Service with Create Option

You will learn

  • How to consume an OData service with create option
QR code


Step 1: Copy previous code

You will begin by making a copy of the previous exercise.


Right-click on the resources folder. Choose Paste.


In the copy folder dialog, give the new Name as odataCRUD.

copy folder
Log on to answer question
Step 2: Edit `manifest,json`

Go into the manifest.json file and switch the service to the existing user.xsodata service. Also adjust the name of the datasource in the model configuration:

Adjust manifest
Log on to answer question
Step 3: Adjust the XML view

Use the following code to replace the existing code for your XML view:

<mvc:View xmlns="sap.m" xmlns:mvc="sap.ui.core.mvc" xmlns:core="sap.ui.core"
	xmlns:smartTable="sap.ui.comp.smarttable" controllerName="opensap.odataBasic.controller.App" height="100%">
	<Page id="mPage" title="{i18n>appTitle}" titleLevel="H1">
			<ScrollContainer id="sc1" height="100%" width="100%" horizontal="true" vertical="true">
				<Panel id="panel" headerText="New User Record Details" expandable="true" expanded="true">
					<List id="list" width="400px">
						<InputListItem id="itmFName" label="{i18n>FName}">
							<Input id="fName" value="{/FirstName}"/>
						<InputListItem id="itmLName" label="{i18n>LName}">
							<Input id="lName" value="{/LastName}"/>
						<InputListItem id="itmEmail" label="{i18n>EMail}">
							<Input id="email" value="{/Email}"/>
					<Button id="btnCall" text="{i18n>B3}" press="callUserService"/>
					<Button id="btnUpdate" text="{i18n>B4}" press="callUserUpdate"/>
				<VBox fitContainer="true">
					<smartTable:SmartTable id="userTable" header="{i18n>UList}" editable="false" entitySet="Users" showRowCount="true" enableAutoBinding="true"
						showFullScreenButton="true" tableType="Table">
								<Column id="UserId">
										<core:CustomData key="p13nData" value='\{"columnKey": "UserId", "leadingProperty": "UserId", "sortProperty": "UserId", "columnIndex":"1"}'/>
									<Text text="{i18n>UserId}"/>
								<Column id="FirstName">
										<core:CustomData key="p13nData" value='\{"columnKey": "FirstName", "leadingProperty": "FirstName", "maxLength": "40","columnIndex":"2"}'/>
									<Text text="{i18n>FName}"/>
								<Column id="LastName">
										<core:CustomData key="p13nData" value='\{"columnKey": "LastName", "leadingProperty": "LastName", "maxLength": "40","columnIndex":"3"}'/>
									<Text text="{i18n>LName}"/>
								<Column id="Email">
										<core:CustomData key="p13nData" value='\{"columnKey": "Email", "leadingProperty": "Email", "maxLength": "40","columnIndex":"4"}'/>
									<Text text="{i18n>EMail}"/>
										<Input value="{UserId}" name="PERS_NO"/>
										<Input value="{FirstName}" name="FIRSTNAME"/>
										<Input value="{LastName}" name="LASTNAME"/>
										<Input value="{Email}" name="E_MAIL"/>
Log on to answer question
Step 4: Add controller logic

Event handlers are within the controller file for your view. You can delete the implementation of the functions onInit and onErrorCall, as you will implement new logic:

delete handler implementation

Add the following code to the onInit function:

this.getView().addStyleClass("sapUiSizeCompact"); // make everything inside this View appear in Compact mode
var oConfig = this.getOwnerComponent().getModel("config");
var userName = oConfig.getProperty("/UserName");
var userModel = this.getOwnerComponent().getModel("userModel");
var oTable = this.getView().byId("userTable");

Add a function called callUserService:

callUserService: function () {
  var oModel = this.getOwnerComponent().getModel("userModel");
  var result = this.getView().getModel().getData();
  var oEntry = {};
  oEntry.UserId = "0000000000";
  oEntry.FirstName = result.FirstName;
  oEntry.LastName = result.LastName;
  oEntry.Email = result.Email;

    "content-type": "application/json;charset=utf-8"
  var mParams = {};
  mParams.success = function () {
    sap.ui.require(["sap/m/MessageToast"], function (MessageToast) {"Create successful");
  mParams.error = onODataError;
  oModel.create("/Users", oEntry, mParams);

Take a look at the code you have just added. You can see it is creating a JSON object with the service fields.

Add a new function called callUserUpdate

callUserUpdate: function () {
  var oModel = this.getOwnerComponent().getModel("userModel");
    "content-type": "application/json;charset=utf-8"

  var mParams = {};
  mParams.error = onODataError;
  mParams.success = function () {
    sap.ui.require(["sap/m/MessageToast"], function (MessageToast) {"Update successful");


Finally, delete the error handling function as we will use the reusable function from common/error.js which was created earlier. But we also need add this error.js reference to our index.html

	<script type="text/javascript" src="/common/error.js" ></script>
Add error.js
Log on to answer question
Step 5: Save and test

Run the web module. When the new tab opens, adjust the URL to include the /odataCRUD path:


Try creating a new record:

Try both creating a new record and editing existing records. Also try creating a record with an invalid email address.

Log on to answer question

Next Steps

Back to top