Skip to Content
Author's profile photo Matthijs Mennens

Creating a DMS attachment with SAPUI5

Introduction

This blog will guide you through the process of uploading a DMS document via a SICF service (REST). This blog will only feature the essentials. So, no error handling or anything of that sort.

 

Following steps will be taken to create and test such a service:

  1. Creating the web service in SAP Backend
    1. Creating the ZIF_REST Interface.
    2. Creating a Handler Class
    3. Creating the class for a POST Request
    4. Creating a node in transaction SICF
  2. Creating the SAPUI5 frontend
    1. Creating a new project
  3. Testing the service with SAPUI5 frontend

 

1.    Creating the web service in SAP Backend

1.1.    Creating the ZIF_REST Interface.

To start off with, let’s create an interface in ‘SE24’ (if it doesn’t exist yet) which we will use for almost all classes in this guide. The interface consists of two methods, one for the request and one for the response.

 

Add the attributes ‘RESPONSE‘ and ‘REQUEST‘ to the interface.

 

The method ‘SET_RESPONSE’ has an importing parameter ‘IS_DATA’ with the ‘XSTRING’ type. Make sure you activate the interface.

 

1.2.    Creating a Handler Class

Go to ‘SE24’ and create a new class called ‘ZCL_TEST_DMS_ATTACHMENT. Select the tab ‘Interfaces’ and add interface ‘IF_HTTP_EXTENSION’.

 

Go back to the tab ‘Methods’ and you’ll see a method has been added. Add another method called ‘GET_REST’.

 

Add the following parameters to the method ‘GET_REST’.

 

Copy the following code to your method ‘IF_HTTP_EXTENSION~HANDLE_REQUEST’.

It will execute the ‘GET_REST’ method to get the class we want to execute to get or process the data.

METHOD IF_HTTP_EXTENSION~HANDLE_REQUEST.

***************************************************************************
" VARIABLES
***************************************************************************
DATA: LO_REST_CLASS     TYPE REF TO ZIF_REST.
DATA: LV_REASON         TYPE STRING.
DATA: LO_ERROR          TYPE REF TO CX_ROOT.

***************************************************************************
" GET THE CLASS WE WANT TO EXECUTE
***************************************************************************
TRY.

LO_REST_CLASS ?= GET_REST( IO_SERVER = SERVER ).

***************************************************************************
" EXECUTE THE CLASS
***************************************************************************
LO_REST->HANDLE_REQUEST( ).

***************************************************************************
" CATCH IF IT PRODUCES ERRORS
***************************************************************************
CATCH CX_ROOT INTO LO_ERROR.

LV_REASON = LO_ERROR->GET_TEXT( ).
SERVER->RESPONSE->SET_STATUS( CODE = 500   
REASON = LV_REASON ).

ENDTRY.

ENDMETHOD.​

 

This method will first check what type of request we’re dealing with (GET, PUT, POST, … etc.).  It will then append the name to the base class name. So, if a POST request is executed, the name of the class it will execute is ‘ZCL_TEST_DMS_ATTACHMENT_POST’.

METHOD GET_REST.

***************************************************************************
" VARIABLES
***************************************************************************
DATA: LV_CLASS_NAME            TYPE SEOCLSNAME.
DATA: LV_REQUEST_METHOD        TYPE STRING.
DATA: LV_ERROR                 TYPE TEXT255.

***************************************************************************
" GET REQUEST METHOD TO GET CLASS NAME TO EXECUTE
***************************************************************************
LV_REQUEST_METHOD = IO_SERVER->REQUEST->GET_HEADER_FIELD( '~request_method' ).
CONCATENATE 'ZCL_TEST_DMS_ATTACHMENT_' LV_REQUEST_METHOD INTO LV_CLASS_NAME.

***************************************************************************
" RETURN CLASS OBJECT
***************************************************************************
TRY.
CREATE OBJECT EO_REST
TYPE (LV_CLASS_NAME)
EXPORTING
IO_REQUEST   = IO_SERVER->REQUEST
IO_RESPONSE  = IO_SERVER->RESPONSE.

***************************************************************************
" CATCH ERRORS
***************************************************************************
CATCH CX_SY_CREATE_OBJECT_ERROR.
LV_ERROR = 'Method ''&'' not supported'(001).
ENDTRY.

ENDMETHOD.

1.3.    Creating the class for a POST Request

Go to ‘SE24’ and create a new class ‘ZCL_TEST_DMS_ATTACHMENT_POST’ and add interface ‘ZIF_REST’.

 

Your class will now have the first two methods below. Add a new method: ‘CONSTRUCTOR’.

 

Add the following parameters to ‘CONSTRUCTOR’. This will instantiate the class when called upon.

 

Open method ‘HANDLE_REQUEST’. This method will be executed from the handler class (‘ZCL_TEST_DMS_ATTACHMENT’).

  METHOD ZIF_REST~HANDLE_REQUEST.

**************************************************************************************
    " TYPES 
**************************************************************************************
    TYPES: BEGIN OF TYPE_FILE,
             NAME    TYPE STRING,
             TYPE    TYPE STRING,
             OBJECT  TYPE CHAR30,
             OBJTYPE TYPE DOKOB,
             DESCR   TYPE STRING,
             DOCTYPE TYPE DOKAR,
             HEXCONT TYPE XSTRING,
           END OF TYPE_FILE.

**************************************************************************************
    " VARIABLES AND OTHERS
**************************************************************************************
    "Tables
    DATA: LT_DOC_LINKS        TYPE TABLE OF BAPI_DOC_DRAD.
    DATA: LT_DOC_DESCR        TYPE TABLE OF BAPI_DOC_DRAT.
    DATA: LT_DOC_FILES        TYPE TABLE OF BAPI_DOC_FILES2.

    "Structures
    DATA: LS_FILE               TYPE TYPE_FILE.
    DATA: LS_DOCDATA            TYPE BAPI_DOC_DRAW2.
    DATA: LS_DOC_LINK           TYPE BAPI_DOC_DRAD.
    DATA: LS_DOC_DESCR          TYPE BAPI_DOC_DRAT.
    DATA: LS_DOC_FILE           TYPE BAPI_DOC_FILES2.
 
    "Variables
    DATA: LV_JSON_BODY  TYPE STRING.
    DATA: LV_NOTIF_NUMBER       TYPE QMNUM.
    DATA: LV_EQUI_NUMBER        TYPE EQUNR.
    DATA: LV_FUNCL_NUMBER       TYPE TPLNR.
    DATA: LV_DOC_NUMBER         TYPE DOKNR.
    DATA: LV_FILENAME           TYPE STRING.
    DATA: LV_OUTPUT_LENGTH      TYPE I.

    "Objects
    DATA: LR_DESERIALIZER  TYPE REF TO CL_TREX_JSON_DESERIALIZER.

    CREATE OBJECT LR_DESERIALIZER.

**************************************************************************************
    " GET JSON DATA FROM POST REQUEST
**************************************************************************************
    LV_JSON_BODY = ME->ZIF_REST~REQUEST->GET_CDATA( ).

**************************************************************************************
    " CONVERT JSON STRING TO ABAP STRUCTURE
**************************************************************************************
    LR_DESERIALIZER->DESERIALIZE(
     EXPORTING
       JSON = LV_JSON_BODY
     IMPORTING
       ABAP = LS_FILE ).

**************************************************************************************
    " CONVERT HEX STRING TO FILE AND SAVE IT IN TEMP FOLDER
**************************************************************************************
    CONCATENATE '/usr/sap/' LS_FILE-NAME '.' LS_FILE-TYPE INTO LV_FILENAME.

    IF LS_FILE-HEXCONT IS NOT INITIAL.
      LV_OUTPUT_LENGTH = XSTRLEN( LS_FILE-HEXCONT ).
      OPEN DATASET LV_FILENAME FOR OUTPUT IN BINARY MODE.
        TRANSFER LS_FILE-HEXCONT TO LV_FILENAME.
      CLOSE DATASET LV_FILENAME.
    ENDIF.

**************************************************************************************
    " SET VALUES OF LINK TABLES
**************************************************************************************
    LS_DOCDATA-DOCUMENTTYPE = LS_FILE-DOCTYPE.
    LS_DOCDATA-DOCUMENTVERSION = '00'.
    LS_DOCDATA-DOCUMENTPART = '000'.
    LS_DOCDATA-LABORATORY = '001'.

    LS_DOC_DESCR-DESCRIPTION  = LS_FILE-DESCR.
    LS_DOC_DESCR-LANGUAGE     = 'E'.
    LS_DOC_DESCR-LANGUAGE_ISO = 'EN'.
    APPEND LS_DOC_DESCR TO LT_DOC_DESCR.

    LS_DOC_FILE-ORIGINALTYPE      = '001'.
    LS_DOC_FILE-STORAGECATEGORY   = 'DMS_C1_ST'.
    LS_DOC_FILE-SOURCEDATACARRIER = 'SAP-SYSTEM'.
    LS_DOC_FILE-CREATED_BY        = SY-UNAME.
    LS_DOC_FILE-DOCFILE           = LV_FILENAME.
    LS_DOC_FILE-WSAPPLICATION     = LS_FILE-TYPE.
    APPEND LS_DOC_FILE TO LT_DOC_FILES.

    LS_DOC_LINK-OBJECTTYPE    = LS_FILE-OBJTYPE.

    "set object and object type when dealing with a notification
    IF LS_FILE-OBJTYPE EQ 'PMQMEL'.
      UNPACK LS_FILE-OBJECT TO LV_NOTIF_NUMBER.
      LS_DOC_LINK-OBJECTKEY = LV_NOTIF_NUMBER.
    ENDIF.

    "set object and object type when dealing with an equipment
    IF LS_FILE-OBJTYPE EQ 'EQUI'.
      UNPACK LS_FILE-OBJECT TO LV_EQUI_NUMBER.
      LS_DOC_LINK-OBJECTKEY = LV_EQUI_NUMBER.
    ENDIF.

    "set object and object type when dealing with a functional location
    IF LS_FILE-OBJTYPE EQ 'IFLOT'.
      UNPACK LS_FILE-OBJECT TO LV_FUNCL_NUMBER.
      LS_DOC_LINK-OBJECTKEY = LV_FUNCL_NUMBER.
    ENDIF.

    APPEND LS_DOC_LINK TO LT_DOC_LINKS.

**************************************************************************************
    " CREATE ATTACHMENT
**************************************************************************************
    CALL FUNCTION 'BAPI_DOCUMENT_CREATE2'
      EXPORTING
        DOCUMENTDATA         = LS_DOCDATA
        PF_FTP_DEST          = 'SAPFTPA'
        PF_HTTP_DEST         = 'SAPHTTP'
      IMPORTING
        DOCUMENTNUMBER       = LV_DOC_NUMBER
      TABLES
        DOCUMENTDESCRIPTIONS = LT_DOC_DESCR
        OBJECTLINKS          = LT_DOC_LINKS
        DOCUMENTFILES        = LT_DOC_FILES.

**************************************************************************************
    " COMMIT
**************************************************************************************
    COMMIT WORK.

  ENDMETHOD.

 

For the sake of keeping it simple, I won’t be added code to method ‘SET_RESPONSE’. If you want more information on that, please read my other blog: https://blogs.sap.com/2018/06/28/writing-a-sicf-service/

 

Now open method ‘CONSTRUCTOR’ and add following code. This method will instantiate the request and response when the class is called.

METHOD CONSTRUCTOR.

ME->ZIF_REST~RESPONSE = IO_RESPONSE.
ME->ZIF_REST~REQUEST = IO_REQUEST.

ENDMETHOD.

 

1.4.    Creating a node in transaction SICF

Go to transaction ‘SICF’ and find a fitting node to which we can append a new one. In this case, we’ll opt for the already existing ‘ZREST’ node. Right click the node and add a new sub-element. We’ll name this node ‘DMS_ATTACH‘.

 

Add a fitting description for the service node.

 

Navigate to the ‘Hander List’ tab and add the handler we created (‘ZCL_TEST_DMS_ATTACHMENT’).

 

For the sake of security, navigate to the tab ‘Logon Data‘. I’m just going to fill it like this, but you should add your own security to make it safe!!!

 

 

 

Make sure you save the service properly.

 

Go back to the node list and right click the node you created and click ‘Activate service’.

 

Click one of the two following buttons to active the node/service.

 

2.    Creating the SAPUI5 frontend

2.1.    Creating a new project

 

Create a new project with a view and controller called ‘Main‘ and add the code below.

 

Main.view.xml

This view contains:

  • 4 input fields (sap.m.Input)
    • Object number (QMNUM, EQUNR, TPLNR)
    • Object type (DOKOB)
    • Document type
    • Description
  • a file uploader (sap.ui.unified.FileUploader)
    • triggers the ‘onChange‘ event which will save the last selected file to a controller variable
  • a button (sap.m.Button)
    • triggers the ‘onConfirm‘ event which will upload the file to the SAP backend
<mvc:View controllerName="FileUploader.controller.Main" xmlns:html="http://www.w3.org/1999/xhtml" xmlns:mvc="sap.ui.core.mvc" displayBlock="true" xmlns="sap.m" xmlns:f="sap.ui.layout.form" xmlns:u="sap.ui.unified">
	<App>
		<pages>
			<Page title="File uploader">
				<content>
				<VBox class="sapUiSmallMargin">
						<Input type="text" id="idObjectNumber" placeholder="object number" width="225px" />
						<Input type="text" id="idObjectType" placeholder="object type" width="225px" />		
						<Input type="text" id="idDocType" placeholder="document type" width="225px" />
						<Input type="text" id="idDescription" placeholder="description" width="225px" />		
						<u:FileUploader
							id="fileUploader"
							name="myFileUpload"
							width="340px"
							uploadUrl="upload/"
							tooltip="Upload your file to the local server"
							change="onChange"/>
						<Button
							text="Upload File"
							press="onConfirm"/>
					</VBox>
				</content>
			</Page>
		</pages>
	</App>
</mvc:View>

 

Main.controller.js

This controller contains 6 methods:

  • onChange
    • passes the last selected file to a controller variable
  • onConfirm
    • is triggered when the upload button is pressed
    • executes the ‘uploadFile‘ method with the last selected file as a parameter
  • uploadFile
    • gets the data from the input fields
    • reads the file content as transforms it to hexadecimal string with the help of method ‘convertBinaryToHex
    • creates a stringied JSON object that is readable by the SAP backend with method ‘createJsonObjectForFileInfo
    • executes the ‘postAttachment‘ method
  • postAttachment
    • executes a POST request to the SAP backend
  • createJsonObjectForFileInfo
    • creates a stringied JSON object that is readable by the SAP backend
  • convertBinaryToHex
    • converts binary string to hexadecimal string
sap.ui.define([
    "sap/ui/core/mvc/Controller"
], function(Controller) {
    "use strict";

    return Controller.extend("FileUploader.controller.Main", {

        onChange: function(oEvent) { // When the user selects a file
            this.lastSelectedFile = oEvent.getParameter("files")[0];
        },

        onConfirm: function() { // When the user presses the upload button
            this.uploadFile(this.lastSelectedFile);
        },

        uploadFile: function(file) {
            var that = this;

            var fileName = file.name.split(".")[0];
            var fileType = file.name.split(".")[1];

            var objectNumber = this.byId("idObjectNumber").getValue();
            var objectType = this.byId("idObjectType").getValue();
            var docType = this.byId("idDocType").getValue();
            var description = this.byId("idDescription").getValue();

            var reader = new FileReader();

            reader.onload = function(e) {
                var raw = e.target.result;
                var hexString = that.convertBinaryToHex(raw).toUpperCase();
                var fileAsJsonString = that.createJsonObjectForFileInfo(fileName, fileType, hexString, docType, description, objectNumber, objectType);
                that.postAttachment(fileAsJsonString);
            };

            reader.onerror = function() {
                sap.m.MessageToast.show("error");
            };
            reader.readAsArrayBuffer(file);
        },

        postAttachment: function(fileAsJsonString) {
            var url = "http://xx.xxx.xx.xxx:8000/zrest/dms_attach?sap-client=400";
            var promise = new Promise(
                function(resolve, reject) {

                    var xhttp = new XMLHttpRequest();
                    xhttp.open("POST", url, true);

                    xhttp.onreadystatechange = function() {
                        if (this.readyState === 4) {
                            if (this.status === 200) {
                                sap.m.MessageToast.show("success");
                            } else {
                                sap.m.MessageToast.show("error");
                            }
                        }
                    }

                    xhttp.send(fileAsJsonString);
                });

            return promise;
        },
        
        // create sap json string 
        createJsonObjectForFileInfo: function(fileName, fileType, hexString, docType, description, objectNumber , objectType) {
            return '{name:"' + fileName + '",' +
                'type:"' + fileType + '",' +
                'object:"' + objectNumber + '",' +
                'objtype:"' + objectType + '",' +
                'descr:"' + description + '",' +
                'doctype:"' + docType + '",' +
                'hexcont:"' + hexString + '"}';
        },

        convertBinaryToHex: function(buffer) {
            return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('');
        }
    });
});

 

 

3.    Testing the service with SAPUI5 frontend

  1. Add an object (notification number, equipment number, functional location number)
  2. Choose the fitting object type (PMQMEL, EQUI, IFLOT)
  3. Choose a document type
  4. Add a fitting description
  5. Select a file
  6. Press the upload button

 

That’s about it for this guide! I await your feedback. 🙂

Assigned Tags

      1 Comment
      You must be Logged on to comment or reply to a post.
      Author's profile photo Chaouki Akir
      Chaouki Akir

      Hello,

       

      in step 1, we have to use back end transactions SE24/SICF.

      ==> using SAPGUI, I can connect to my SAP system and call needed transactions. So, no problem

       

      But, in step 2, you create a new SAPUI5 project.

      ==> how do you do that ? What editing tool are you using ? Isn't it SAP Web IDE ? If yes, how do you link SAP Web IDE and your SAP back end ?

       

      Chaouki