Skip to Content
Technical Articles
Author's profile photo Stefan Schnell

How To Use JSON DataBinding Without Gateway and OData

I describe here how to extend your copy of the function module (FM) RFC_READ_TABLE to get the result also in JSON format. On this way you have the tiny possibility to get a little bit the feeling of OData via SAP Gateway interface, e.g. if you are using an older version of an SAP AS.

Here I describe how to call a VBScript from JavaScript with IE 11 in edge mode.

With this descriptions and the COM Connector (CCo) is it now possible to realize a solution in the UI5 context. We will start with the html file:

<!doctype html>
<html>

  <head>

    <title>Table SFLIGHT</title>

    <meta http-equiv=”Content-Type” content=”text/html” />
    <meta charset=”ISO-8859-1″ />
    <meta http-equiv=”X-UA-Compatible” content=”IE=edge” />

    <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>

    <script language=”JavaScript”>

      function getData(TableName) {
        var wsh, pathName;
        if (“ActiveXObject” in window) {
          wsh = new ActiveXObject(“WScript.Shell”);
          if (typeof(wsh) == ‘object’) {
            pathName = location.pathname.substr(0,
              location.pathname.lastIndexOf(“/”))+”/”;
            pathName = pathName.slice(1);
            wsh.run(“wscript.exe \”” + pathName +
              “ReadTableJSON.vbs\” ” + TableName + ” \”” + pathName +
              “\””, 1, true);
            wsh = null;
          }
        }
        else {
          alert(“Your Browser doesn’t support ActiveXObject”);
        }
      }

      function Column(Text, Width) {
        var oLabel, oTemplate, oColumn;
        oLabel = new sap.ui.commons.Label();
        oLabel.setText(Text);
        oTemplate = new sap.ui.commons.TextView();
        oTemplate.bindProperty(“text”, Text);
        oColumn = new sap.ui.table.Column();
        oColumn.setLabel(oLabel);
        oColumn.setTemplate(oTemplate);
        oColumn.setWidth(Width);
        return oColumn;
      }

      function main() {
        var oModel, oTable;
        getData(“SFLIGHT”);
        oModel = new sap.ui.model.json.JSONModel();
        oModel.loadData(“SFLIGHT.json”);
        oTable = new sap.ui.table.Table();
        oTable.setTitle(“Table SFLIGHT”);
        oTable.setVisibleRowCount(15);
        oTable.setFirstVisibleRow(0);

        //-Add the columns———————————————-
        oTable.addColumn(Column(“CARRID”, “35px”));
        oTable.addColumn(Column(“CONNID”, “35px”));
        oTable.addColumn(Column(“FLDATE”, “50px”));
        oTable.addColumn(Column(“PRICE”, “50px”));
        oTable.addColumn(Column(“CURRENCY”, “50px”));
        oTable.addColumn(Column(“PLANETYPE”, “50px”));

        oTable.setModel(oModel);
        oTable.bindRows(“/SFLIGHT”);
        oTable.placeAt(“content”)
      }

    </script>
  </head>

  <body class=”sapUiBody” role=”application” onLoad=”main()”>
    <div id=”content” />
  </body>

</html>

 

As you can see it is not really the known standard. The main function gets at first the data as local JSON file via our copy of RFC_READ_TABLE from the table SFLIGHT. It loads the data, creates the table and adds the columns via an own function. I use only the set and get methods, it is better readable I think. Here now the VBScript file to get the data in JSON format:

'-Begin-----------------------------------------------------------------

  '-Directives----------------------------------------------------------
    Option Explicit

  '-Constants-----------------------------------------------------------
    Const RFC_OK = 0
    Const ForWriting = 2

  '-Sub getData---------------------------------------------------------
    Sub getData(tableName, pathName)

      '-Variables-------------------------------------------------------
        Dim SAP, hRFC, rc, hFuncDesc, hFunc, hTable, RowCount, i, hRow
        Dim charBuffer, charFile, FSO, F

      Set SAP = CreateObject("COMNWRFC")
      If IsObject(SAP) Then
        hRFC = SAP.RfcOpenConnection("ASHOST=ABAP, SYSNR=00, " & _
          "CLIENT=000, USER=BCUSER")
        If hRFC Then
          hFuncDesc = SAP.RfcGetFunctionDesc(hRFC, "Z_RFC_READ_TABLE_JSON")
          If hFuncDesc Then
            hFunc = SAP.RfcCreateFunction(hFuncDesc)
            If hFunc Then
              rc = SAP.RfcSetChars(hFunc, "QUERY_TABLE", tableName)
              rc = SAP.RfcSetChars(hFunc, "DELIMITER", "~")
              If SAP.RfcInvoke(hRFC, hFunc) = RFC_OK Then
                If SAP.RfcGetTable(hFunc, "DATA_JSON", hTable) = RFC_OK Then
                  rc = SAP.RfcGetRowCount(hTable, RowCount)
                  rc = SAP.RfcMoveToFirstRow(hTable)
                  For i = 1 To RowCount
                    hRow = SAP.RfcGetCurrentRow(hTable)
                    rc = SAP.RfcGetChars(hRow, "LINE", charBuffer, 1024)
                    charFile = charFile & RTrim(charBuffer) & vbCrLf
                    If i < RowCount Then
                      rc = SAP.RfcMoveToNextRow(hTable)
                    End If
                  Next

                  '-Write JSON file-------------------------------------
                    Set FSO = CreateObject("Scripting.FileSystemObject")
                    If IsObject(FSO) Then
                      Set F = FSO.OpenTextFile(pathName & tableName & _
                        ".json", ForWriting, True)
                      F.Write charFile
                      F.Close
                      Set FSO = Nothing
                    End If

                End If
              End If
            rc = SAP.RfcDestroyFunction(hFunc)
            End If
          End If
          rc = SAP.RfcCloseConnection(hRFC)
        End If
        Set SAP = Nothing
      End If

    End Sub

  '-Main----------------------------------------------------------------

    getData WScript.Arguments.Item(0), WScript.Arguments.Item(1)

'-End-------------------------------------------------------------------

 

With this mixture of RFC libary via CCo, VBScript and JavaScript in IE 11 you can easily simulate an UI5 environment on the presentation server, without any other server. Also it is not necessary, with the modifiaction of your copy of RFC_READ_TABLE, to have the newest AS environment.

001.JPG

On this way you can realize a relatively lightweight solution, to simulate OData via SAP Gateway and to use it in your UI5 environment on your presentation server.

Enjoy it.

Assigned Tags

      2 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Stefan Schnell
      Stefan Schnell
      Blog Post Author

      Hello community,

      here a tiny function to validate the JSON file:

        //-function isJSON----------------------------------------------------
          function isJSON(FileName) {
      
            //-Variables------------------------------------------------------
              var strFile;
      
            $.ajax({
              url: FileName,
              type: 'get',
              async: false,
              success: function(jsonFile) {
                strFile = String(jsonFile);
              }
            });
      
            try {
              JSON.parse(strFile);
            }
            catch (event) {
              return false;
            }
            return true;
      
          }
      

      Cheers

      Stefan

      Author's profile photo Michael Wegelin
      Michael Wegelin

      Cool, thanks!