Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
stefan_schnell
Active Contributor
9,132
Here a step by step guide to expose the RFC-enabled function module RFC_READ_TABLE as WebService and how to use it via JavaScript:

  1. Start transaction code SE80 and create a new Enterprise Service.

  2. Choose Service Provider and press Continue button in the dialog.



  3. Choose Existing ABAP Object and press Continue button.

  4. Name the service definition, the description and press Continue button.

  5. Choose Function Module and press Continue button.

  6. Name Function Module with RFC_READ_TABLE.

  7. Accept SOAP Appl, set Profile to Authentication with User and Password, No Transport Guarantee and press Continue button.

  8. Choose the Package you want, I my case I activate Local Object CheckBox, and press Continue button.

  9. Press Complete button.

  10. Now Save and Activate the Service Definition.

  11. Start the transaction code SOAMANAGER, log on and select the link Web Service Configuration.

  12. Search for your service name, in my case ZTEST. Press the Apply Selection button.

  13. Choose Configurations tab below and choose the link Create.

    Set the Service Name, the Description, the Binding Name and press the button Apply Settings.



  14. Scroll down and choose in Provider Security User ID/Password and Save your configuration.


  15. Choose the Overview tab and select the link Open WSDL document for selected binding or service.

    A new browser window will be opened, scroll down until address location tag. Here you find the URL of the web service.



    Thats all to expose a RFC function module as WebService.

  16. Here now a complete request to call the WebService with the table USR01.POST /sap/bc/srt/rfc/sap/ztest2/001/ztest2/rfc_read_table HTTP/1.1
    Host: ABAP
    Accept: */*
    Authorization: Basic YmN1c2VyOm1pbmlzYXA=
    content-length: 428
    content-type: text/xml

    <?xml version="1.0" encoding="UTF-8"?>
    <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <env:Header/>
    <env:Body>
    <ns1:RFC_READ_TABLE xmlns:ns1="urn:sap-com:document:sap:rfc:functions">
    <DATA />
    <FIELDS />
    <OPTIONS />
    <QUERY_TABLE>USR01</QUERY_TABLE>
    </ns1:RFC_READ_TABLE>
    </env:Body>
    </env:Envelope>


  17. Here now a JavaScript program to call the SAP WebService.
    <!doctype html>
    <html>

    <head>
    <title>Calling Web Service from jQuery</title>

    <script type="text/javascript" src="jquery.min.js"></script>

    <script type="text/javascript">
    $(document).ready(function () {
    $("#btnCallWebService").click(function (event) {
    var wsUrl = "http://ABAP:8080/sap/bc/srt/rfc/sap/ztest/001/ztest/rfc_read_table";
    var soapRequest = '<?xml version="1.0" encoding="UTF-8"?>' +
    '<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema">' +

    '<env:Body>' +
    '<ns1:RFC_READ_TABLE xmlns:ns1="urn:sap-com:document:sap:rfc:functions">' +
    '<DATA />' +
    '<FIELDS />' +
    '<OPTIONS />' +
    '<QUERY_TABLE>USR01</QUERY_TABLE>' +
    '</ns1:RFC_READ_TABLE>' +
    '</env:Body>' +
    '</env:Envelope>';
    $.ajax({
    type: "POST",
    url: wsUrl,
    contentType: "text/xml",
    dataType: "xml",
    headers: {
    "Authorization": "Basic " + btoa("bcuser:minisap"),
    "Accept": "*/*",
    "Host": "ABAP"
    },
    data: soapRequest,
    success: processSuccess,
    });
    error: processError
    });
    });

    function processSuccess(data, status, req) {
    alert('success');
    if (status == "success")
    alert(req.responseText);
    }

    function processError(data, status, req) {
    alert(req.responseText + " " + status);
    }

    </script>

    </head>

    <body>
    <h3>
    Calling SAP Web Services via JavaScript with jQuery/AJAX
    </h3>
    <input id="btnCallWebService" value="Call web service" type="button" />
    </body>

    </html>



With this knowledge it should be now not difficult to expose any RFC-enable function module as WebService and to use it inside your JavaScript program.
2 Comments
Labels in this area