Skip to Content
Author's profile photo Balbino Soares Ferreira Filho

How to create and consume gateway service via SAPUI5 – Part 3

In this third and last part of this series, we will consume the OData service previously created, using SAPUI5, displaying the query results in a table.

We need to use the project created in eclipse in the first part of this series and consume the service created in second part. The part one and two can be found in the following links.

How to create and consume a gateway service via SAPUI5 – Part 1

How to create and consume gateway service via SAPUI5 – Part 2

Let´s change the project according with this example.

First, we need to change index.html file, including the sap.ui.table component. See example below.


<script src="resources/sap-ui-core.js"
        id="sap-ui-bootstrap"
        data-sap-ui-libs="sap.ui.commons, sap.ui.table"
        data-sap-ui-theme="sap_bluecrystal"> 
</script>

Now we need to change the controller, including the logic to create a model that will connect to Odata service. In airline.controller.js, create a new method getModel. Here we will inform the url from Odata service which we create in second part of our tutorial. Please check the url created in service and replace in your code.


getModel: function(){
// Return model based in OData Service
return new sap.ui.model.odata.ODataModel("http://Yourserver:Port/sap/opu/odata/SAP/ZHELLOODATA_SRV");
}

Replace the url by the url from your service, you can find it in the service created, see the previously blog post.

/wp-content/uploads/2016/04/image18_920589.jpg

The last change will be made in view file. Here we going to do the logic to create a table that display the results from the Odata Service.

In createContent method enter the code below.


var layout = new sap.ui.commons.layout.MatrixLayout('layout');
layout.setWidth('100%');
var rPannel = new sap.ui.commons.Panel('rPannel');
var rTitle = new sap.ui.commons.Title('rTitle');
rTitle.setText('Airlines');
rPannel.setTitle(rTitle);
var oTable = new sap.ui.table.DataTable();
oTable.addColumn(new sap.ui.table.Column({
label : new sap.ui.commons.Label({
text : "Airline"
}),
template : new sap.ui.commons.TextField().bindProperty("value",
"Carrid"),
sortProperty : "Carrid"
}));
oTable.addColumn(new sap.ui.table.Column({
label : new sap.ui.commons.Label({
text : "Airline Name"
}),
template : new sap.ui.commons.TextField().bindProperty("value",
"Carrname"),
sortProperty : "Carrname"
}));
oTable.addColumn(new sap.ui.table.Column({
label : new sap.ui.commons.Label({
text : "Currency"
}),
template : new sap.ui.commons.TextField().bindProperty("value",
"Currcode"),
sortProperty : "Currcode"
}));
oTable.addColumn(new sap.ui.table.Column({
label : new sap.ui.commons.Label({
text : "Url"
}),
template : new sap.ui.commons.TextField().bindProperty("value",
"Url"),
sortProperty : "Url"
}));
oTable.setModel(oController.getModel());
oTable.bindRows("/SCARRSet");
rPannel.addContent(oTable);
layout.createRow(rPannel);
this.addContent(layout);

In the logic above we have some points of attention: The name of properties of table column is the same displayed in metadata, or the same of the columns of the DDIC table. In line 48, we have to set the name of our collection, we can find in the XML, see 3th image of this blog. You need enter the value with a ‘/’, just like you can see in this example.

Now you can save and submit the files. To do this, right click over the project name, on project explorer, then choose team submit, this procedure was demonstrated in first article. Let’s run and test, the final result is like a demonstrate in image below.

/wp-content/uploads/2016/04/image_920753.jpg

I appreciate so much to publish this blogs and want to continue publish more and more about my studies and learning.

Thank you all!

Assigned Tags

      7 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Swapan Sarkar
      Swapan Sarkar

      Had trouble with the following line for some time ...

      var oTable = new sap.ui.table.DataTable();

      Fixed by replacing DataTable() by Table().

       

      Author's profile photo Atul Jaiswal
      Atul Jaiswal

      Hi,

      Thanks for such a detailed blog for the beginners.

      I have a question. I followed the exact steps still i do not see the results. For me my Odata service is returning results however UI5 App shows no data as attached screen print. Could you please suggest.

       

      Thanks,

      Atul

      Author's profile photo Balbino Soares Ferreira Filho
      Balbino Soares Ferreira Filho
      Blog Post Author

      Check if the bindRows method of the table is equal to entity set name of the gateway service.

      In my example the line oTable.bindRows("/SCARRSet");  

      Also check if the set name in bindRows methos is started with /.

       

       

       

       

      Author's profile photo Shubham Dayanand Annadate
      Shubham Dayanand Annadate

      Hi Balbino Soares Ferreira Filho,

      Yes bindRows method contains the same name of entity set name and starting with / but still its not showing data. Exactly followed same all 3 parts. Facing same issue no data found.

      Author's profile photo jhansi challa
      jhansi challa

      Superb. Excellent post. Helped me a lot. Thanks alot

      Author's profile photo Former Member
      Former Member

      Hi Balbino,

      i am new to SAP UI5 ,SAP Netweaver Gateway ,ODATA and all these stuffs..

      When i was searching i got your link,which was very good,but got confused on how the connection happens between SAPUI5 Eclipse(where i have developed a webpage)--to SAP Netweaver Gateway---ODATA to be precise .

      --> what is Actual flow to get data from ABAP system and where BSP page is used & why and what is link up between all these Components.

       

      Please help in making a clarity on this soon..

       

      Thanks in advance !!

       

       

       

       

      Author's profile photo Nelson Kharat
      Nelson Kharat

      I am not getting data. Please help!!!

       

      I am trying to fetch table from the Products from services.odata. I tried but I am not able to display the data in it. Although I am getting rows, but with no data.

      when I try to F12 , I see that $metadata is not found. Please help with the below coding. Thanks in advance.

       

      Controller:

       

      sap.ui.controller("material.material", {

      /**
      * Called when a controller is instantiated and its View controls (if available) are already created.
      * Can be used to modify the View before it is displayed, to bind event handlers and do other one-time initialization.
      * @memberOf material.material
      */
      onInit: function() {
      //Create a Model

      var oModel = new sap.ui.model.odata.v2.ODataModel('proxy/https/services.odata.org/OData/OData.svc/');

      var oTable = sap.ui.getCore().byId('TAB1');

      //get Model for table
      oTable.setModel(oModel);
      oTable.bindRows("/Products");

      },

       


       

      in View:

       

      sap.ui.jsview("material.material", {

      /** Specifies the Controller belonging to this View.
      * In the case that it is not implemented, or that "null" is returned, this View does not have a Controller.
      * @memberOf material.material
      */
      getControllerName : function() {
      return "material.material";
      },

      /** Is initially called once after the Controller has been instantiated. It is the place where the UI is constructed.
      * Since the Controller is given to this method, its event handlers can be attached right away.
      * @memberOf material.material
      */
      createContent : function(oController) {

      var oTable = new sap.ui.table.Table('TAB1', {

      title : 'Example on SAP UI5',
      visibleRowCount : 5,
      NavigationMode : sap.ui.table.NavigationMode.Paginator
      });

      oTable.addColumn(new sap.ui.table.Column({
      label : new sap.ui.commons.Label({
      text : "ID"
      }),
      template : new sap.ui.commons.TextField().bindProperty("value",
      "ID"),
      sortProperty : "ID"
      }));
      oTable.addColumn(new sap.ui.table.Column({
      label : new sap.ui.commons.Label({
      text : "Name"
      }),
      template : new sap.ui.commons.TextField().bindProperty("value",
      "Name"),
      sortProperty : "Name"
      }));

      return oTable;

      }
      });


       

      Index.html

       

      <!DOCTYPE HTML>
      <html>
      <head>
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta http-equiv='Content-Type' content='text/html;charset=UTF-8'/>

      <script src="resources/sap-ui-core.js"
      id="sap-ui-bootstrap"
      data-sap-ui-libs="sap.ui.commons, sap.ui.table"
      data-sap-ui-theme="sap_bluecrystal">
      </script>
      <!-- add sap.ui.table,sap.ui.ux3 and/or other libraries to 'data-sap-ui-libs' if required -->

      <script>

      sap.ui.localResources("material");
      var view = sap.ui.view({id:"idmaterial1", viewName:"material.material", type:sap.ui.core.mvc.ViewType.JS});
      view.placeAt("content");

      </script>

      </head>
      <body class="sapUiBody" role="application">
      <div id="content"></div>
      </body>
      </html>