Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to create a Document in DMS server through a BAPI

SATHEESHKUMAR_1
Explorer
0 Kudos
1,680

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,

19 REPLIES 19

FredericGirod
Active Contributor
1,339

Did you have search a little bit before asking ?

Ulrich_Schmidt
Product and Topic Expert
Product and Topic Expert
0 Kudos
1,339

Hello Satheesh,

the following blog describes it in some detail with sample code:

Calling DMS BAPIs

Regards, Ulrich

0 Kudos
1,339

Can you please provide the BAPI code for create Document in DMS server (CV01N) if you have any

1,339

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

Sandra_Rossi
Active Contributor
0 Kudos
1,339

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.

Sandra_Rossi
Active Contributor
0 Kudos
1,339

How is your question related to RFC please? (your tag)

SATHEESHKUMAR_1
Explorer
0 Kudos
1,339

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.

SATHEESHKUMAR_1
Explorer
0 Kudos
1,339

Structure with fields for /pilog/dms_file as below.

image.png

Sandra_Rossi
Active Contributor
0 Kudos
1,339

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!

Sandra_Rossi
Active Contributor
0 Kudos
1,339

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").

SATHEESHKUMAR_1
Explorer
0 Kudos
1,339

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

Sandra_Rossi
Active Contributor
0 Kudos
1,339

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

SATHEESHKUMAR_1
Explorer
1,339

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

Sandra_Rossi
Active Contributor
0 Kudos
1,339

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.

0 Kudos
1,339

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.

0 Kudos
1,339

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.

0 Kudos
1,339

Hi Sandra,

Can you please help me on this value of input XTRING, how we can resolve this.

Thanks,

Satheesh.

0 Kudos
1,339

It's wrong input value. How can you imagine that I can do anything about it? Only you can pass correct input value.

Sandra_Rossi
Active Contributor
0 Kudos
1,339

Thanks a lot!