2023 Apr 20 7:18 AM
Hi,
I want to create a Document number with any file(PDF, JPG, TXT Etc) through BAPI, can anyone have Custom BAPI to create Document in DMS server, please help me on this requirement.
Thanks,
Satheesh,
2023 Apr 20 7:30 AM
2023 Apr 20 8:20 AM
Hello Satheesh,
the following blog describes it in some detail with sample code:
Regards, Ulrich
2023 Apr 20 12:55 PM
Can you please provide the BAPI code for create Document in DMS server (CV01N) if you have any
2023 Apr 20 9:09 PM
With "BAPI code" you mean "ABAP code"?
I only have the C/C++, Java and C# code. I thought the question was about how to call the DMS BAPIs via RFC from an external program, because the question is tagged with "NW ABAP Remote Function Call (RFC)", which is the sapnwrfc.dll for C/C++.
2023 Apr 20 9:42 AM
I guess you mean DMS from the CA-DMS application component in SAP ERP (CV01N, etc.), not the BC-SRV-KPR-DMS (Knowledge PROvider). You don't need to worry about the "server" where it's stored, you just need to use the BAPI for CA-DMS, named BAPI_DOCUMENT_*
Lots of questions and answers in the Web.
2023 Apr 20 10:52 PM
2023 Apr 21 7:29 AM
Hi Sandra,
I want to Create document in DMS server with RFC/BAPI, can you please share me the BAPI code for create document in DMS server through BAPI_DOCUMENT_CREATE2.
I have develpoped one BAPI for create document in DMS server, but using this code, when we are giving input in XSTRING, then only I am able to create Document, but that created document I am not able to open, please find the below code and give any other inputs.
BAPI Code below:
FUNCTION /PILOG/DMS_CREATE.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" CHANGING
*" VALUE(CS_FILE) TYPE /PILOG/DMS_FILE OPTIONAL
*"----------------------------------------------------------------------
DATA: LS_DOCDATA TYPE BAPI_DOC_DRAW2.
DATA: LS_OBJECTLINK TYPE BAPI_DOC_DRAD.
DATA: LT_OBJECTLINKS TYPE TABLE OF BAPI_DOC_DRAD.
DATA: LS_OBJECTDESCR TYPE BAPI_DOC_DRAT.
DATA: LT_OBJECTDESCR TYPE TABLE OF BAPI_DOC_DRAT.
DATA: LS_OBJECTFILE TYPE BAPI_DOC_FILES2.
DATA: LT_OBJECTFILES TYPE TABLE OF BAPI_DOC_FILES2.
DATA: RETURN TYPE BAPIRET2.
data: gs_tdwat TYPE tdwat,
access_info like STANDARD TABLE OF scms_acinf WITH HEADER LINE,
gt_bin type STANDARD TABLE OF raw255,
gs_bin type raw255.
data: LV_FILENAME type STRING.
DATA: dV_DOCTYPE TYPE DOKAR.
DATA: dV_objtype TYPE DOKOB value 'kna1'.
DATA: dV_dktxt TYPE Dktxt.
DATA: GV_FLAG TYPE FLAG.
if cs_file-DOKAR is NOT INITIAL and
cs_file-FILE_NAME is NOT INITIAL and
cs_file-FILE_TYPE is NOT INITIAL and
cs_file-DESCRIPTION is NOT INITIAL and
cs_file-XSTRING is NOT INITIAL.
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = cs_file-XSTRING
* APPEND_TO_TABLE = ' '
IMPORTING
OUTPUT_LENGTH = access_info-comp_size
tables
binary_tab = GT_BIN.
*CONCATENATE 'D:\usr\sap\S29\D00\work/' cs_file-file_name into LV_FILENAME.
CONCATENATE '/tmp/' cs_file-file_name into LV_FILENAME.
OPEN dataset LV_FILENAME for OUTPUT in BINARY MODE.
loop at gt_bin into GS_BIN.
TRANSFER GS_BIN to LV_FILENAME.
ENDLOOP.
close DATASET LV_FILENAME.
wait UP TO 1 SECONDS.
OPEN DATASET LV_FILENAME for OUTPUT in BINARY MODE.
if sy-subrc = 0.
GV_FLAG = 'X'.
ENDIF.
CLOSE DATASET LV_FILENAME.
IF GV_FLAG IS NOT INITIAL.
ls_docdata-documenttype = cs_file-dokar.
ls_docdata-documentversion = '00'."cs_file-dokar.
ls_docdata-documentpart = '000'."cs_file-dokar.
ls_docdata-statusextern = 'AA'."cs_file-dokar.
**ls_docdata-statusintern = 'AA'."cs_file-dokar.
ls_objectdescr-description = cs_file-description.
ls_objectdescr-language = 'E'.
ls_objectdescr-language_iso = 'EN'.
APPEND ls_objectdescr TO lt_objectdescr.
** ls_objectfile-originaltype = originaltype .
** ls_objectfile-sourcedatacarrier = sourcedatacarrier.
ls_objectfile-storagecategory = 'DMS_C1_ST'."storagecategory .
ls_objectfile-created_by = sy-uname.
ls_objectfile-docfile = LV_FILENAME.
ls_objectfile-wsapplication = cs_file-file_type."wsapplication.
ls_objectfile-checkedin = 'X'."cs_file-file_type.
ls_objectfile-statusextern = 'AR'."cs_file-file_type.
** ls_objectfile-statusintern = 'WR'."cs_file-file_type.
APPEND ls_objectfile TO lt_objectfiles.
CALL FUNCTION 'BAPI_DOCUMENT_CREATE2'
EXPORTING
documentdata = ls_docdata
* HOSTNAME =
* DOCBOMCHANGENUMBER =
* DOCBOMVALIDFROM =
* DOCBOMREVISIONLEVEL =
* CAD_MODE = ' '
PF_FTP_DEST = 'SAPFTPA'
PF_HTTP_DEST = 'SAPHTTP'
** DEFAULTCLASS = 'X'
IMPORTING
* DOCUMENTTYPE =
DOCUMENTNUMBER = cs_file-doknr
* DOCUMENTPART =
* DOCUMENTVERSION =
RETURN = return
TABLES
* CHARACTERISTICVALUES =
* CLASSALLOCATIONS =
DOCUMENTDESCRIPTIONS = lt_objectdescr
* OBJECTLINKS =
* DOCUMENTSTRUCTURE =
DOCUMENTFILES = lt_objectfiles.
* LONGTEXTS =
* COMPONENTS =
if cs_file-doknr is NOT INITIAL.
COMMIT WORK.
CONCATENATE 'DMS Document' cs_file-doknr 'Generated Successfully' into cs_file-message SEPARATED BY space.
else.
cs_file-message = 'error while create document'.
endif.
ELSE.
cs_file-message = 'error while creating file in application server' .
endif.
else.
cs_file-message = 'pass all the required parameters'.
endif.
2023 Apr 21 7:34 AM
2023 Apr 21 9:21 AM
Please use the button [CODE] to format code, which makes the code appear colored/indented, it will be easier for people to look at it.
Please show the images instead of hyperlinks, in order to have both the text and the image in one place.
Just copy/paste the image:
Thank you!
2023 Apr 21 9:28 AM
I can't read very well your code because not indented and no colors, but I can see that you are writing extra 0 bytes to the file, and you are doing lot of useless stuff. Also file on application server -> SAPFTPA and SAPHTTPA (both with trailing "A").
2023 Apr 21 12:18 PM
Hi,
I am able to create Document with my provided code, but when I am trying to open that file, it is showing erroe as "It is not supported file type or the file has been damaged". can you please help on this
2023 Apr 21 1:18 PM
I'm sorry, I would like to fix your code, but as I said I can't read very well your code because not indented and no colors, hopefully you can use the button [CODE] to format code, which makes the code appear colored/indented,
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = cs_file-XSTRING
...
2023 Apr 21 2:02 PM
Hi Sandra Rossi,
Please find the below code,
FUNCTION /PILOG/DMS_CREATE.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" CHANGING
*" VALUE(CS_FILE) TYPE /PILOG/DMS_FILE OPTIONAL
*"----------------------------------------------------------------------
DATA: LS_DOCDATA TYPE BAPI_DOC_DRAW2.
DATA: LS_OBJECTLINK TYPE BAPI_DOC_DRAD.
DATA: LT_OBJECTLINKS TYPE TABLE OF BAPI_DOC_DRAD.
DATA: LS_OBJECTDESCR TYPE BAPI_DOC_DRAT.
DATA: LT_OBJECTDESCR TYPE TABLE OF BAPI_DOC_DRAT.
DATA: LS_OBJECTFILE TYPE BAPI_DOC_FILES2.
DATA: LT_OBJECTFILES TYPE TABLE OF BAPI_DOC_FILES2.
DATA: RETURN TYPE BAPIRET2.
data: gs_tdwat TYPE tdwat,
access_info like STANDARD TABLE OF scms_acinf WITH HEADER LINE,
gt_bin type STANDARD TABLE OF raw255,
gs_bin type raw255.
data: LV_FILENAME type STRING.
DATA: dV_DOCTYPE TYPE DOKAR.
DATA: dV_objtype TYPE DOKOB value 'kna1'.
DATA: dV_dktxt TYPE Dktxt.
DATA: GV_FLAG TYPE FLAG.
if cs_file-DOKAR is NOT INITIAL and
cs_file-FILE_NAME is NOT INITIAL and
cs_file-FILE_TYPE is NOT INITIAL and
cs_file-DESCRIPTION is NOT INITIAL and
cs_file-XSTRING is NOT INITIAL.
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = cs_file-XSTRING
* APPEND_TO_TABLE = ' '
IMPORTING
OUTPUT_LENGTH = access_info-comp_size
tables
binary_tab = GT_BIN.
*CONCATENATE 'D:\usr\sap\S29\D00\work/' cs_file-file_name into LV_FILENAME.
CONCATENATE '/tmp/' cs_file-file_name into LV_FILENAME.
OPEN dataset LV_FILENAME for OUTPUT in BINARY MODE.
loop at gt_bin into GS_BIN.
TRANSFER GS_BIN to LV_FILENAME.
ENDLOOP.
close DATASET LV_FILENAME.
wait UP TO 1 SECONDS.
OPEN DATASET LV_FILENAME for OUTPUT in BINARY MODE.
if sy-subrc = 0.
GV_FLAG = 'X'.
ENDIF.
CLOSE DATASET LV_FILENAME.
IF GV_FLAG IS NOT INITIAL.
ls_docdata-documenttype = cs_file-dokar.
ls_docdata-documentversion = '00'."cs_file-dokar.
ls_docdata-documentpart = '000'."cs_file-dokar.
ls_docdata-statusextern = 'AA'."cs_file-dokar.
**ls_docdata-statusintern = 'AA'."cs_file-dokar.
ls_objectdescr-description = cs_file-description.
ls_objectdescr-language = 'E'.
ls_objectdescr-language_iso = 'EN'.
APPEND ls_objectdescr TO lt_objectdescr.
** ls_objectfile-originaltype = originaltype .
** ls_objectfile-sourcedatacarrier = sourcedatacarrier.
ls_objectfile-storagecategory = 'DMS_C1_ST'."storagecategory .
ls_objectfile-created_by = sy-uname.
ls_objectfile-docfile = LV_FILENAME.
ls_objectfile-wsapplication = cs_file-file_type."wsapplication.
ls_objectfile-checkedin = 'X'."cs_file-file_type.
ls_objectfile-statusextern = 'AR'."cs_file-file_type.
** ls_objectfile-statusintern = 'WR'."cs_file-file_type.
APPEND ls_objectfile TO lt_objectfiles.
CALL FUNCTION 'BAPI_DOCUMENT_CREATE2'
EXPORTING
documentdata = ls_docdata
* HOSTNAME =
* DOCBOMCHANGENUMBER =
* DOCBOMVALIDFROM =
* DOCBOMREVISIONLEVEL =
* CAD_MODE = ' '
PF_FTP_DEST = 'SAPFTPA'
PF_HTTP_DEST = 'SAPHTTP'
** DEFAULTCLASS = 'X'
IMPORTING
* DOCUMENTTYPE =
DOCUMENTNUMBER = cs_file-doknr
* DOCUMENTPART =
* DOCUMENTVERSION =
RETURN = return
TABLES
* CHARACTERISTICVALUES =
* CLASSALLOCATIONS =
DOCUMENTDESCRIPTIONS = lt_objectdescr
* OBJECTLINKS =
* DOCUMENTSTRUCTURE =
DOCUMENTFILES = lt_objectfiles.
* LONGTEXTS =
* COMPONENTS =
.
if cs_file-doknr is NOT INITIAL.
COMMIT WORK.
CONCATENATE 'DMS Document' cs_file-doknr 'Generated Successfully' into cs_file-message SEPARATED BY space.
else.
cs_file-message = 'error while create document'.
endif.
ELSE.
cs_file-message = 'error while creating file in application server' .
endif.
else.
cs_file-message = 'pass all the required parameters'.
endif.
Thanks,Satheesh kumar
2023 Apr 21 8:12 PM
You write an empty file, because you do twice OPEN DATASET ... FOR OUTPUT ... (create or replace) consecutively on the same file path, and second time you just clear the file.
OPEN dataset LV_FILENAME for OUTPUT in BINARY MODE.
loop at gt_bin into GS_BIN.
TRANSFER GS_BIN to LV_FILENAME.
ENDLOOP.
close DATASET LV_FILENAME.
wait UP TO 1 SECONDS.
OPEN DATASET LV_FILENAME for OUTPUT in BINARY MODE.
if sy-subrc = 0.
GV_FLAG = 'X'.
ENDIF.
CLOSE DATASET LV_FILENAME.
Delete the second block [OPEN DATASET ... FOR OUTPUT ... ] ... [CLOSE DATASET ... ].
Remove even the logic about GV_FLAG (unclear name for "file has been created successfully", see Clean ABAP guide of the importance to use meaningful names), because you know that dataset is correctly written if SY-SUBRC = 0 after each dataset instruction (see ABAP documentation). An error may possibly trigger a class-based exception too.
NB: you are incorrectly mixing application server/frontend RFC destinations:
PF_FTP_DEST = 'SAPFTPA'
PF_HTTP_DEST = 'SAPHTTP'
For application server (which is your case), it must be all with trailing "A": PF_FTP_DEST = 'SAPFTPA'
PF_HTTP_DEST = 'SAPHTTPA'
For frontend (which is not your case), it must be without trailing "A": PF_FTP_DEST = 'SAPFTP'
PF_HTTP_DEST = 'SAPHTTP'
NB: calling SCMS_XSTRING_TO_BINARY is useless because you can directly write cs_file-XSTRING to the file:TRY.
OPEN dataset LV_FILENAME for OUTPUT in BINARY MODE.
IF sy-subrc = 0.
TRANSFER cs_file-XSTRING to LV_FILENAME. " <=================== write whole file
close DATASET LV_FILENAME.
IF sy-subrc <> 0.
" handle error although technically speaking in the current situation sy-subrc is always 0
ENDIF.
NB: cleanup your code before posting because many unused lines hurt like lowercase 'kna1':DATA: dV_objtype TYPE DOKOB value 'kna1'.
After calling a create/update/delete BAPI, you must call BAPI_TRANSACTION_COMMIT instead of COMMIT WORK as rule of thumb, because usually people don't know for which BAPI it is mandatory to call it. Same for BAPI_TRANSACTION_ROLLBACK/ROLLBACK WORK.
2023 Apr 22 4:47 PM
Hi Sandra Rossi,
I have changed the code as per your inputs, when I test it We are able to create Document in DMS server and able to see in CV03N as well.
But When I am trying to open that file in CV03N, it is showing below error, kindly help me on this.
please find below code .
FUNCTION /PILOG/DMS_CREATE.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" CHANGING
*" VALUE(CS_FILE) TYPE /PILOG/DMS_FILE OPTIONAL
*"----------------------------------------------------------------------
DATA: LS_DOCDATA TYPE BAPI_DOC_DRAW2.
DATA: LS_OBJECTLINK TYPE BAPI_DOC_DRAD.
DATA: LT_OBJECTLINKS TYPE TABLE OF BAPI_DOC_DRAD.
DATA: LS_OBJECTDESCR TYPE BAPI_DOC_DRAT.
DATA: LT_OBJECTDESCR TYPE TABLE OF BAPI_DOC_DRAT.
DATA: LS_OBJECTFILE TYPE BAPI_DOC_FILES2.
DATA: LT_OBJECTFILES TYPE TABLE OF BAPI_DOC_FILES2.
DATA: RETURN TYPE BAPIRET2.
data: gs_tdwat TYPE tdwat,
access_info like STANDARD TABLE OF scms_acinf WITH HEADER LINE,
gt_bin type STANDARD TABLE OF raw255,
gs_bin type raw255.
data: LV_FILENAME type STRING.
DATA: dV_DOCTYPE TYPE DOKAR.
**DATA: dV_objtype TYPE DOKOB value 'kna1'.
DATA: dV_dktxt TYPE Dktxt.
DATA: GV_FLAG TYPE FLAG.
if cs_file-DOKAR is NOT INITIAL and
cs_file-FILE_NAME is NOT INITIAL and
cs_file-FILE_TYPE is NOT INITIAL and
cs_file-DESCRIPTION is NOT INITIAL and
cs_file-XSTRING is NOT INITIAL.
***CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
*** EXPORTING
*** buffer = cs_file-XSTRING
**** APPEND_TO_TABLE = ' '
*** IMPORTING
*** OUTPUT_LENGTH = access_info-comp_size
*** tables
*** binary_tab = GT_BIN.
*CONCATENATE 'D:\usr\sap\S29\D00\work/' cs_file-file_name into LV_FILENAME.
CONCATENATE '/tmp/' cs_file-file_name into LV_FILENAME.
OPEN dataset LV_FILENAME for OUTPUT in BINARY MODE.
If sy-subrc = 0.
TRANSFER cs_file-XSTRING to LV_FILENAME.
close DATASET LV_FILENAME.
if sy-subrc <> 0.
"handle error although technically
endif.
ENDIF.
wait UP TO 1 SECONDS.
**OPEN DATASET LV_FILENAME for OUTPUT in BINARY MODE.
if sy-subrc = 0.
GV_FLAG = 'X'.
ENDIF.
** CLOSE DATASET LV_FILENAME.
IF GV_FLAG IS NOT INITIAL.
ls_docdata-documenttype = cs_file-dokar.
ls_docdata-documentversion = '00'."cs_file-dokar.
ls_docdata-documentpart = '000'."cs_file-dokar.
ls_docdata-statusextern = 'AA'."cs_file-dokar.
**ls_docdata-statusintern = 'AA'."cs_file-dokar.
ls_objectdescr-description = cs_file-description.
ls_objectdescr-language = 'E'.
ls_objectdescr-language_iso = 'EN'.
APPEND ls_objectdescr TO lt_objectdescr.
** ls_objectfile-originaltype = originaltype .
** ls_objectfile-sourcedatacarrier = sourcedatacarrier.
ls_objectfile-storagecategory = 'DMS_C1_ST'."storagecategory .
ls_objectfile-created_by = sy-uname.
** ls_objectfile-docpath = '/tmp/'.
ls_objectfile-docfile = LV_FILENAME.
ls_objectfile-wsapplication = cs_file-file_type."wsapplication.
ls_objectfile-checkedin = 'X'."cs_file-file_type.
ls_objectfile-statusextern = 'AA'."cs_file-file_type.
** ls_objectfile-statusintern = 'WR'."cs_file-file_type.
APPEND ls_objectfile TO lt_objectfiles.
CALL FUNCTION 'BAPI_DOCUMENT_CREATE2'
EXPORTING
documentdata = ls_docdata
* HOSTNAME =
* DOCBOMCHANGENUMBER =
* DOCBOMVALIDFROM =
* DOCBOMREVISIONLEVEL =
* CAD_MODE = ' '
PF_FTP_DEST = 'SAPFTPA'
PF_HTTP_DEST = 'SAPHTTPA'
** DEFAULTCLASS = 'X'
IMPORTING
* DOCUMENTTYPE =
DOCUMENTNUMBER = cs_file-doknr
* DOCUMENTPART =
* DOCUMENTVERSION =
RETURN = return
TABLES
* CHARACTERISTICVALUES =
* CLASSALLOCATIONS =
DOCUMENTDESCRIPTIONS = lt_objectdescr
* OBJECTLINKS =
* DOCUMENTSTRUCTURE =
DOCUMENTFILES = lt_objectfiles.
* LONGTEXTS =
* COMPONENTS =
.
if cs_file-doknr is NOT INITIAL.
** COMMIT WORK.
CONCATENATE 'DMS Document' cs_file-doknr 'Generated Successfully' into cs_file-message SEPARATED BY space.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
.
else.
cs_file-message = 'error while create document'.
endif.
ELSE.
cs_file-message = 'error while creating file in application server' .
endif.
else.
cs_file-message = 'pass all the required parameters'.
endif.
ENDFUNCTION.
Thanks.
2023 Apr 23 9:37 AM
Do I correctly see that your XSTRING is starting with EF BB BF? It's UTF-8 BOM, not PDF format at all...
The issue is not about the code, the issue is about the value of input XSTRING.
2023 Apr 24 6:07 AM
Hi Sandra,
Can you please help me on this value of input XTRING, how we can resolve this.
Thanks,
Satheesh.
2023 Apr 24 7:00 AM
It's wrong input value. How can you imagine that I can do anything about it? Only you can pass correct input value.
2023 Apr 21 8:13 PM