End To End OData Service SAPUI5 Application
End To End OData Service SAPUI5 Application
com
Beginners SAP
Page 2
Quick Introductions:
OData service: In SAP, OData service can be considered as a kind of webservice which can be used directly in any end user web application to access
the module specific data. What data can be accessed through the service is
decided during the development of such a service.
SAP Netweaver Gateway Service Builder: In SAP we use SAP
Netweaver Gateway Service Builder (Tcode SEGW) to develop such OData
services. This gateway is delivered by SAP in SAP Netweaver 7.2 version
onwards. However it does not come automatically in built. To get it
installed in your SAP version you need to install some add on packages.
I have developed this tutorial on the SAP Netweaver 7.4 version so when
you try this tutorial on your system some of your screens may look a bit
different.
ESPM Demo Model Enterprise Sales and Procurement Model:
SAP Netweaver delivers a sample data model which we can use to develop
demo applications. This is known as the ESPM or EPM Model in short.
Beginners SAP
Page 3
Beginners SAP
Page 4
Beginners SAP
Page 5
In this case I can see 115 Product entries returned so we are all set to move
with our actual OData service creation.
Beginners SAP
Page 6
Beginners SAP
Page 7
Now if you are interested to go ahead after looking at this lovely output,
then lets continue.
Beginners SAP
Page 8
Define or Import
DATA MODEL
Implement
SERVICE
Register SERVICE
and
Activate HUB
Beginners SAP
Page 9
Beginners SAP
Page 10
The values for the products (Product names, Category etc) were returned in
the HEADERDATA Table and this is of type
BAPI_EPM_PRODUCT_HEADER.
Hence we will need to refer this structure when we build our service.
Execute transaction SEGW and create a new project.
Beginners SAP
Page 11
Beginners SAP
Page 12
Page 13
Step 2 Deselect some fields you would not need in your service.
Beginners SAP
Page 14
Step 3 Mark the PRODUCT_ID field as the Key for this service.
After you follow the wizard to add the DDIC structure, a new Entity Set EmpProducSet will be added to your Project - Z_EPM_PRODUCTS Data
Model.
Beginners SAP
Page 15
Page 16
Beginners SAP
Page 17
Beginners SAP
Page 18
Beginners SAP
Page 19
Beginners SAP
Page 20
From here you can click the Call in Browser button to display the service
document in the browser.
Beginners SAP
Page 21
Beginners SAP
Page 22
Beginners SAP
Page 23
Beginners SAP
Page 24
The code is simple to understand. All we are doing here is calling the BAPI
'BAPI_EPM_PRODUCT_GET_LIST' which we had executed in the PreCheck step to fetch the values for PRODUCTS from the SAP system. Then
we added code to be able to SORT order to our put if it is requested by the
service caller in the io_tech_request_context and if Paging is requested
then we add paging value as read from the is_paging Parameter.
Beginners SAP
Page 25
If you check the feed in Internet Explorer you will notice that there are exactly 115
records returned.
Beginners SAP
Page 26
Tip: In internet explorer, to switch from the feed view to the raw xml, go to
Internet Options -> Content -> Feeds and Web Slices Settings and uncheck the
"Turn on feed reading view". Then revisit the URL, you should see the raw XML
from the service.
The above implementation will enable the service to return the entire set of
records. However we would also want out service to return specific product
data when the product id is passed to the service. This can be done by
redefining the EPMPRODUCTSET_GET_ENTITY method of the
ZCL_Z_EPM_PRODUCTS_DPC_EXT class.
So again in the class builder (Transaction SE24) open the class
ZCL_Z_EPM_PRODUCTS_DPC_EXT in change mode, select the method
EPMPRODUCTSET_GET_ENTITY and click the redefine method.
Beginners SAP
Page 27
Beginners SAP
Page 28
In the above code we are simply passing the Product id from the import
parameter it_key_tab of the method to
'BAPI_EPM_PRODUCT_GET_DETAIL' and returning back the product
details into the export parameter ER_ENTITY.
Thru this code you have now made your service able to fetch and return a
product values for a specific product based on the product id to your service
caller.
To ensure that the service will return you the values properly test the
service by executing the service again in the browser.
This means now we have a nicely working backend Odata service created
and is responding as required.
Beginners SAP
Page 29
END OF PART 1
Beginners SAP
Page 30
PART 2
Developing an application using SAPUI5 to
consume an OData Service.
Beginners SAP
Page 31
Beginners SAP
Page 32
1. Start Eclipse
2. Navigate to File->New->Other
Beginners SAP
Page 33
3. In the screen that appears, type SAP and under SAPUI5 Application Development
folder select Application Project and hit the Next Button.
Beginners SAP
Page 34
4. On the next Screen you need to provide a suitable project name for you APP
5. Here we are going to display the Products to the end user using the OData service
we created in part 1, So enter a suitable name EPMProductsApp
Beginners SAP
Page 35
6. Keeping all other settings the same (or you can change the file location your
wish) click the next button. Notice in the above screenshot in Options, Create an
Initial View is checked, hence in the next step we will need to provide details for
the applications Initial view.
7. I have named it ProductList and kept Development Paradigm as Javascript and
hit the next button.
Beginners SAP
Page 36
8. Finally you get a list of objects that will be automatically created for you. Click
Finish.
Beginners SAP
Page 37
Beginners SAP
Page 38
On close observation you will notice that the following parts have been created for you
1. ProductList.view.js: A view file of type JavaScript
2. ProductList.controller.js: A controller file of type JS
3. index.html - The main file that contains the libaray references, the
theme references, etc.
Beginners SAP
Page 39
12. We now need to tie these parts together to create a smoothly running Product
APP
Beginners SAP
Page 40
2. On the next screen select a repository plugin which in our case is the SAPUI5
ABAP Repository and hit next button
Beginners SAP
Page 41
3. In the screen that follows you need to select your SAP server instance that you
want to connect to.
Beginners SAP
Page 42
4. And then click ok. The system automatically populates all the data related to this
connection. Click the Next button.
5. On the next screen Provide the Client, Userid and Password of the SAP
Instance you want to connect to and hit the Next button.
6. Eclipse will then try to connect to your SAP system.
7. Once the connection is established, you then have to specify the name, a
description and the development package of the BSP Application (in our example
Z_EPMPRODUI5APP) which will be created as the corresponding repository
object (if a transportable package is selected, in the next step a transport request
has to be selected or created).
8. So Select the Create a new BSP Application check box and provide the name
Z_EPMProductsApp and a description as EMP Product Catalgue Display App
9. For Package I have selected a local object $TMP.
Beginners SAP
Page 43
10. On the next screen you may need to provide a transport request but since this is a
local object ($TMP) hence no transport request is necessary. Simply hit the finish
button on the next screen.
Beginners SAP
Page 44
Beginners SAP
Page 45
12. Now you can see that our SAPUI5 application in Eclipse is connected to the
Backend BSP application in SAP. Which means once our development in Eclipse
is complete; we can simply transport the whole application to Quality and
Production as a single transport request.
13. However note that we have still not transferred any content from Eclipse to
Backend SAP system. To do that follow the steps as outlined below.
Page 46
15. The system shows a list of all the resources which you have developed in your
application. Select all the ones which are relevant. Since we are deploying this
application on to server for the first time hence we have to select all the
resources. Click the Finish button.
Beginners SAP
Page 47
16. You can now log into our SAP backend and location our SAPUI5 Application as a
BSP Application in the backend
Beginners SAP
Page 48
17. Our Application is fully functional now, however we still havent added any
functional code to this application.
18. To test that our deployment works correctly lets add a simple Hello World
statement to the code.
This code we will add in the View of the Application so that this Text will be
displayed when we execute out application.
So in eclipse, locate the ProductList.view.js File and here in the createContent Function
add the following code
return new sap.ui.commons.TextView({text:"Hello World"});
Beginners SAP
Page 49
Hit Save in Eclipse and then Submit your change to SAP through Team/Submit.
Now its time to test our changes.
To do so follow the steps below.
1. In the Project Explorer, Right click on the Application Name and Select Run
As/ Web App Preview.
Beginners SAP
Page 50
Beginners SAP
Page 51
Note: In-case you do not see your output Hello world here, then most probably there is a miss-match in
the library used in the bootstrap script. I had this issue which I resolved by changing
1. The Resource library file path in the index.html
2. And changing the file reference from sap.m to sap.ui.commons
This becomes clearer to identify such issues when you understand debugging SAPUI5 applications.
Debugging the application becomes easier in an external browser. You can launch the external
browser by clicking the open in external browser button.
Beginners SAP
Page 52
This will launch your default browser which in my case is Internet Explorer. If you
want you can even copy this url and open the page in your favorite browser. Firefox
and Chrome are two browsers which are preferred by developers because of a
number of developer extensions the provide.
The above was a test to check that the application is functioning properly on the
frontend local development front.
Beginners SAP
Page 53
Beginners SAP
Page 54
2. You may get a popup to put in your user id and password for the SAP system.
3. So here we see the same output as the one we got from our local Eclipse.
Beginners SAP
Page 55
Page 56
Beginners SAP
Page 57
3. And the following code ( Below I will walk through the code step by step here so that you
understand the main points )
Beginners SAP
Page 58
oTable.addColumn(new sap.ui.table.Column({
label : new sap.ui.commons.Label({
text : "Product Name"
}),
template : new sap.ui.commons.TextView({
text : "{Name}"
}),
sortProperty : "Name"
}));
oTable.addColumn(new sap.ui.table.Column({
label : new sap.ui.commons.Label({
text : "Price",
textAlign : sap.ui.core.TextAlign.End
}),
template : new sap.ui.commons.TextView({
text : "{Price}",
textAlign : sap.ui.core.TextAlign.End
}),
sortProperty : "Price"
}));
oTable.addColumn(new sap.ui.table.Column({
label : new sap.ui.commons.Label({
text : "Currency"
}),
template : new sap.ui.commons.TextView({
text : "{CurrencyCode}"
}),
sortProperty : "CurrencyCode"
}));
oTable.addColumn(new sap.ui.table.Column({
label : new sap.ui.commons.Label({
text : "Supplier"
}),
template : new sap.ui.commons.TextView({
text : "{SupplierName}"
}),
sortProperty : "SupplierName"
}));
// bind table rows to the OData entity set
oTable.bindRows("/EpmProducts");
return oTable;
// alternatively:
// oTable.bindAggregation(sName, oBindingInfo)
// oTable.bindAggregation( "rows", "/EpmProducts" );
Beginners SAP
Page 59
The code is clear enough, but for further understanding go through the screen shots below
Beginners SAP
Page 60
Beginners SAP
Page 61
After refresh.
Beginners SAP
Page 62
The Images are not appearing here because the appropriate service for Images is inactive in my
server. Usually you will find this service at the following location.
/sap/public/bc/NWDEMO_MODEL/
TO enable this service execute the transaction SICF and navigate to the path and activate the
service.
Beginners SAP
Page 63
Now we can refresh the application again. This time the images appear in the table
Beginners SAP
Page 64
Thank you for Referring to this guide. I hope it was helpful for you. In case you have any
feedback or issues regarding the use of this guide
Contact me on my blog www.beginners-sap.com
Beginners SAP
Page 65
Beginners SAP
Page 66