ABAP Code to Read SAP DMS Document Binary Output
If you have a requirement, to read the SAP DMS document data in any rest api or odata service by passing the DMS document number as input and getting binary output in xstring format , you can use this Function Module Code
Source code
_________________________________________________________________________
FUNCTION ZC4C_DMS_BINARY_DATA1.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" VALUE(IP_DOKNR) TYPE DRAW-DOKNR OPTIONAL
*" EXPORTING
*" VALUE(EP_MSG) TYPE STRING
*" VALUE(ES_ACCESS_INFO) TYPE SCMS_ACINF
*" VALUE(EP_XSTRING) TYPE XSTRING
*" VALUE(EP_FILE_TYPE) TYPE DAPPL
*" VALUE(EP_FILE_NAME) TYPE CHAR200
*"----------------------------------------------------------------------
"This FM will return the DMS attachment output in binary format
DATA : GS_DRAD TYPE DRAD,
GS_DRAW TYPE DRAW,
GT_FILES TYPE STANDARD TABLE OF DMS_DOC_FILES,
GS_FILES TYPE DMS_DOC_FILES,
GTE_FILES TYPE DMS_TBL_FILE,
GTE_PHIOS TYPE DMS_TBL_PHIO,
GSE_PHIOS TYPE DMS_REC_PHIO,
GT_DOC2L TYPE STANDARD TABLE OF DMS_DOC2LOIO,
GS_DOC2L TYPE DMS_DOC2LOIO,
GT_PH TYPE STANDARD TABLE OF DMS_PH_CD1,
GS_PH TYPE DMS_PH_CD1,
GS_TDWAT TYPE TDWAT.
DATA : ACCESS_INFO LIKE STANDARD TABLE OF SCMS_ACINF WITH HEADER LINE,
CONTENT_TXT LIKE STANDARD TABLE OF SDOKCNTASC WITH HEADER LINE,
CONTENT_BIN LIKE STANDARD TABLE OF SDOKCNTBIN WITH HEADER LINE,
CONTENT_BIN1 LIKE STANDARD TABLE OF SDOKCNTBIN WITH HEADER LINE,
GT_BIN TYPE STANDARD TABLE OF RAW255,
GS_BIN TYPE RAW255.
IF IP_DOKNR IS NOT INITIAL.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
INPUT = IP_DOKNR
IMPORTING
OUTPUT = IP_DOKNR.
SELECT SINGLE * FROM DRAD INTO CORRESPONDING FIELDS OF GS_DRAD WHERE DOKNR = IP_DOKNR.
SELECT SINGLE * FROM DRAW INTO CORRESPONDING FIELDS OF GS_DRAW WHERE DOKNR = IP_DOKNR.
SELECT * FROM DMS_DOC_FILES INTO CORRESPONDING FIELDS OF TABLE GT_FILES
WHERE DOKNR = GS_DRAW-DOKNR.
"for storage categry
SELECT * FROM DMS_DOC2LOIO INTO CORRESPONDING FIELDS OF TABLE GT_DOC2L
WHERE DOKNR = GS_DRAW-DOKNR.
IF GT_DOC2L[] IS NOT INITIAL.
SELECT * FROM DMS_PH_CD1 INTO CORRESPONDING FIELDS OF TABLE GT_PH
FOR ALL ENTRIES IN GT_DOC2L WHERE LOIO_ID = GT_DOC2L-LO_OBJID.
ENDIF.
LOOP AT GT_FILES INTO GS_FILES WHERE DOKNR = GS_DRAW-DOKNR.
CLEAR : GS_DOC2L.
READ TABLE GT_DOC2L INTO GS_DOC2L WITH KEY DOKNR = GS_DRAW-DOKNR.
CLEAR : GS_PH.
IF GS_DOC2L-LO_OBJID IS NOT INITIAL.
READ TABLE GT_PH INTO GS_PH WITH KEY LOIO_ID = GS_DOC2L-LO_OBJID.
CALL FUNCTION 'SCMS_DOC_READ'
EXPORTING
* MANDT = SY-MANDT
STOR_CAT = GS_PH-STOR_CAT
* CREP_ID = ' '
DOC_ID = GS_PH-PHIO_ID "'0272694A0C801ED98FFC26109FA20587'
* PHIO_ID =
SIGNATURE = 'X'
* SECURITY = ' '
* NO_CACHE = ' '
* RAW_MODE = ' '
* IMPORTING
* FROM_CACHE =
* CREA_TIME =
* CREA_DATE =
* CHNG_TIME =
* CHNG_DATE =
* STATUS =
* DOC_PROT =
TABLES
ACCESS_INFO = ACCESS_INFO
CONTENT_TXT = CONTENT_TXT
CONTENT_BIN = CONTENT_BIN
EXCEPTIONS
BAD_STORAGE_TYPE = 1
BAD_REQUEST = 2
UNAUTHORIZED = 3
COMP_NOT_FOUND = 4
NOT_FOUND = 5
FORBIDDEN = 6
CONFLICT = 7
INTERNAL_SERVER_ERROR = 8
ERROR_HTTP = 9
ERROR_SIGNATURE = 10
ERROR_CONFIG = 11
ERROR_FORMAT = 12
ERROR_PARAMETER = 13
ERROR = 14
OTHERS = 15.
IF SY-SUBRC <> 0.
* Implement suitable error handling here
ENDIF.
* IF lt_sdokcntbin IS NOT INITIAL.
IF CONTENT_BIN[] IS NOT INITIAL.
* READ TABLE lt_access INTO ls_access INDEX 1.
READ TABLE ACCESS_INFO INDEX 1.
ES_ACCESS_INFO = ACCESS_INFO.
* convert to string
CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
EXPORTING
* input_length = ls_access-comp_size
INPUT_LENGTH = ACCESS_INFO-COMP_SIZE
IMPORTING
BUFFER = EP_XSTRING
TABLES
BINARY_TAB = CONTENT_BIN
EXCEPTIONS
FAILED = 1
OTHERS = 2.
IF SY-SUBRC EQ 0.
ENDIF.
ENDIF.
* CONCATENATE EP_XSTRING EP_XSTRING INTO EP_XSTRING.
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
BUFFER = EP_XSTRING
IMPORTING
OUTPUT_LENGTH = ACCESS_INFO-COMP_SIZE "gv_filesize
TABLES
BINARY_TAB = GT_BIN.
EP_FILE_TYPE = GS_FILES-DAPPL.
EP_FILE_NAME = GS_FILES-FILENAME.
ENDIF.
CLEAR : GS_FILES.
EXIT.
ENDLOOP.
ELSE.
EP_MSG = 'Pass all the required parameters'.
ENDIF.
ENDFUNCTION.
________________________________________________________________________