cancel
Showing results for 
Search instead for 
Did you mean: 

How to upload file from Web Dynpro into R/3 (BAPI_DOCUMENT_CREATE2)

Former Member
0 Kudos

I have to upload file of any kind using Web Dynpro into SAP R/3 and call BAPI_DOCUMENT_CREATE2 function.

I tried to use BAPI_DOCUMENT_CREATE2 PF_FTP_DEST="SAPFTP" parameter to upload local client "C:\xxx.doc" file but I got function dump because it is web application running under Portal.

I tried to store the file in Web Dynpro server directory but this is Windows server and SAP Unix server can't reach the file.

I tried to upload the file (using fileupload ui) and pass it in bin format (RAW) to RFC function but after that I don't know which standard function I should use to store this RAW format file in SAP directory.

Does anyone have a good idea how to solve this problem? Thanks.

Chang Chung Chi

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Unfortunatly, I think there is no SAP RFC standard function to do this.

I resolved this issue by developing my own RFC function which encapsuled the BAPI_DOCUMENT_CREATE2.

The ABAP program should be (you must performed some adaptations) :

FUNCTION zbapi_doc_create_with_attach .
*"----------------------------------------------------------------------
*"*"Local interface:
*"  IMPORTING
*"     VALUE(DOCUMENTDATA) LIKE  BAPI_DOC_DRAW2 STRUCTURE
*"        BAPI_DOC_DRAW2
*"     VALUE(BIN_CONTENT) TYPE  XSTRING
*"  EXPORTING
*"     VALUE(DOCUMENTTYPE) LIKE  BAPI_DOC_AUX-DOCTYPE
*"     VALUE(DOCUMENTNUMBER) LIKE  BAPI_DOC_AUX-DOCNUMBER
*"     VALUE(DOCUMENTPART) LIKE  BAPI_DOC_AUX-DOCPART
*"     VALUE(DOCUMENTVERSION) LIKE  BAPI_DOC_AUX-DOCVERSION
*"     VALUE(RETURN) LIKE  BAPIRET2 STRUCTURE  BAPIRET2
*"  TABLES
*"      CHARACTERISTICVALUES STRUCTURE  BAPI_CHARACTERISTIC_VALUES
*"       OPTIONAL
*"      CLASSALLOCATIONS STRUCTURE  BAPI_CLASS_ALLOCATION OPTIONAL
*"      DOCUMENTDESCRIPTIONS STRUCTURE  BAPI_DOC_DRAT OPTIONAL
*"      OBJECTLINKS STRUCTURE  BAPI_DOC_DRAD OPTIONAL
*"      DOCUMENTSTRUCTURE STRUCTURE  BAPI_DOC_STRUCTURE OPTIONAL
*"      DOCUMENTFILES STRUCTURE  BAPI_DOC_FILES2 OPTIONAL
*"      LONGTEXTS STRUCTURE  BAPI_DOC_TEXT OPTIONAL
*"      COMPONENTS STRUCTURE  BAPI_DOC_COMP OPTIONAL
*"----------------------------------------------------------------------


**..... Document data
  DATA: ls_doc    LIKE bapi_doc_files2.

**..... Bapi return structure
  DATA: ls_return LIKE bapiret2.

**.... Originals that are checked in at the same time
  DATA: lt_files LIKE bapi_doc_files2 OCCURS 0 WITH HEADER LINE.


  DATA:
        tstampl           LIKE tzonref-tstampl,
        tstampl_c(21)     TYPE c,
        file_name         TYPE localfile,
        msg(80)           TYPE c.

  CONSTANTS path_name    TYPE localfile
             VALUE '/tmp/'.

  CONSTANTS log TYPE localfile VALUE '/tmp/logFO.txt'.

* ---------------------------------------------------------------------
* Create document
* ---------------------------------------------------------------------
  DESCRIBE TABLE documentfiles LINES sy-tfill.
  IF sy-tfill > 0.

    CLEAR ls_doc.
    CLEAR lt_files.

    LOOP AT  documentfiles INTO ls_doc FROM 1 TO 1.

      OPEN DATASET log FOR OUTPUT IN TEXT MODE.
      TRANSFER ls_doc-wsapplication TO log.
      TRANSFER ls_doc-description TO log.


      GET TIME STAMP                        FIELD tstampl.
      UNPACK tstampl                        TO tstampl_c.
      CLEAR file_name.
      CONCATENATE path_name
                      '/'
                      sy-repid
                      '_'
                      'ATT'
                      tstampl_c
                      '.'
                      ls_doc-wsapplication
                      INTO file_name.

      CONDENSE file_name                    NO-GAPS.
      transfer file_name to log.
      OPEN DATASET file_name                FOR OUTPUT
                                            IN BINARY MODE
                                            MESSAGE msg.
      IF sy-subrc                           <> 0.
        MESSAGE a899(zz)                   WITH file_name
                                                msg
                                                'SY-SUBRC:'
                                                sy-subrc.
      ENDIF.
      TRANSFER bin_content      TO file_name.
      CLOSE DATASET file_name.
      transfer 'move data to lt_files' to log.
      MOVE:documentdata-documenttype  TO lt_files-documenttype,
           documentdata-documentpart  TO lt_files-documentpart,
           documentdata-documentversion TO lt_files-documentversion,
           ls_doc-description TO      lt_files-description,
           '1'                          TO lt_files-originaltype,
           'Z_TEST_SAP'                 TO lt_files-storagecategory,
           ls_doc-wsapplication        TO lt_files-wsapplication,
           file_name                    TO lt_files-docfile,
           'X'                          TO lt_files-checkedin.
      TRANSLATE lt_files-wsapplication TO UPPER CASE.
      APPEND lt_files.


**
      CLEAR ls_return.
      transfer 'call BAPI_DOCUMENT_CREATE2' to log.

      CALL FUNCTION 'BAPI_DOCUMENT_CREATE2'
           EXPORTING
                documentdata    = documentdata
                pf_http_dest    = 'SAPHTTPA'
           IMPORTING
                documentnumber  = documentnumber
                documenttype    = documenttype
                documentversion = documentversion
                documentpart    = documentpart
                return          = return
           TABLES
                objectlinks     = objectlinks
                documentfiles   = lt_files
           EXCEPTIONS
                OTHERS          = 1.

      CLOSE DATASET log.


    ENDLOOP.
  ENDIF.



ENDFUNCTION.

So, You can map the bin_content attribute with a binary field of your Web application context.

Fred.

bruno_esperanca
Contributor
0 Kudos

Hi Olivieri,

So the idea is to create a temporary file in the server? Is it possible to call BAPI_DOCUMENT_CREATE2 with the contents of the file? I'm using a Vault so the file doesn't need to be created in the sap system (I guess).

Thanks.

BR,

Bruno

Answers (9)

Answers (9)

Former Member
0 Kudos

Hi,

When I try to use the code provided by Olivieri my BAPI_DOCUMENT_CREATE2 call simply exit with no error message and no dumb in ST22..

Because of that I can't detect what is the problem...

Someone have ideia what can be causing this ?

I'm trying to upload just one doc File.

function zbapi_doc_create_with_attach .

*"----------------------------------------------------------------------

*"*"Interface local:

*"  IMPORTING

*"     VALUE(DOCUMENTDATA) LIKE  BAPI_DOC_DRAW2 STRUCTURE

*"        BAPI_DOC_DRAW2

*"     VALUE(BIN_CONTENT) TYPE  XSTRING

*"  EXPORTING

*"     VALUE(DOCUMENTTYPE) LIKE  BAPI_DOC_AUX-DOCTYPE

*"     VALUE(DOCUMENTNUMBER) LIKE  BAPI_DOC_AUX-DOCNUMBER

*"     VALUE(DOCUMENTPART) LIKE  BAPI_DOC_AUX-DOCPART

*"     VALUE(DOCUMENTVERSION) LIKE  BAPI_DOC_AUX-DOCVERSION

*"     VALUE(RETURN) LIKE  BAPIRET2 STRUCTURE  BAPIRET2

*"  TABLES

*"      CHARACTERISTICVALUES STRUCTURE  BAPI_CHARACTERISTIC_VALUES

*"       OPTIONAL

*"      CLASSALLOCATIONS STRUCTURE  BAPI_CLASS_ALLOCATION OPTIONAL

*"      DOCUMENTDESCRIPTIONS STRUCTURE  BAPI_DOC_DRAT OPTIONAL

*"      OBJECTLINKS STRUCTURE  BAPI_DOC_DRAD OPTIONAL

*"      DOCUMENTSTRUCTURE STRUCTURE  BAPI_DOC_STRUCTURE OPTIONAL

*"      DOCUMENTFILES STRUCTURE  BAPI_DOC_FILES2 OPTIONAL

*"      LONGTEXTS STRUCTURE  BAPI_DOC_TEXT OPTIONAL

*"      COMPONENTS STRUCTURE  BAPI_DOC_COMP OPTIONAL

*"----------------------------------------------------------------------

**..... Document data

  data: ls_doc    like bapi_doc_files2.

**..... Bapi return structure

  data: ls_return like bapiret2.

**.... Originals that are checked in at the same time

  data: lt_files like bapi_doc_files2 occurs 0 with header line.

  data:

        tstampl           like tzonref-tstampl,

        tstampl_c(21)     type c,

        file_name         type localfile,

        msg(80)           type c.

  constants path_name    type localfile

             value '/tmp/'.

  constants log type localfile value '/tmp/logFO.txt'.

* ---------------------------------------------------------------------

* Create document

* ---------------------------------------------------------------------

   clear ls_doc.

  clear lt_files.

  open dataset log for output in text mode encoding default.

  transfer ls_doc-wsapplication to log.

  transfer ls_doc-description to log.

  get time stamp                        field tstampl.

  unpack tstampl                        to tstampl_c.

  clear file_name.

  concatenate path_name

                  sy-repid

                  '_'

                  'ATT'

                  tstampl_c

                  '.'

                  'doc'

                  into file_name.

  condense file_name                    no-gaps.

  transfer file_name to log.

  open dataset file_name                for output

                                        in binary mode

                                        message msg.

  if sy-subrc                           <> 0.

    message a899(zz)                   with file_name

                                            msg

                                            'SY-SUBRC:'

                                            sy-subrc.

  endif.

  transfer bin_content      to file_name.

  close dataset file_name.

  transfer 'move data to lt_files'  to log.

  move:documentdata-documenttype    to lt_files-documenttype,

       documentdata-documentpart        to lt_files-documentpart,

       documentdata-documentversion    to lt_files-documentversion,

       'Test  description                         to lt_files-description,

       '1'                                              to lt_files-originaltype,

       'ZDMS_C1_ST'                           to lt_files-storagecategory,

       'WRD'                                       to lt_files-wsapplication,

       file_name                                  to lt_files-docfile,

       'X'                                             to lt_files-checkedin.

  translate lt_files-wsapplication to upper case.

  append lt_files.

  clear characteristicvalues[].

  data: ls_characteristicvalues like characteristicvalues.

  ls_characteristicvalues-classtype = '017'.

  ls_characteristicvalues-classname = 'ZDMS_XXX'.

  ls_characteristicvalues-charname  = 'ZDMS_XPTO'.

  ls_characteristicvalues-charvalue = 'XXX'.

  append ls_characteristicvalues to characteristicvalues.

  clear ls_return.

  transfer 'call BAPI_DOCUMENT_CREATE2' to log.

  call function 'BAPI_DOCUMENT_CREATE2'

    exporting

      documentdata    = documentdata

      pf_http_dest    = 'SAPHTTPA'

    importing

      documentnumber  = documentnumber

      documenttype    = documenttype

      documentversion = documentversion

      documentpart    = documentpart

      return          = return

    tables

      characteristicvalues = characteristicvalues

      classallocations     = classallocations

      objectlinks          = objectlinks

      documentfiles        = lt_files

    exceptions

      others               = 1.

  if return-type ca 'EA'.

    call function 'BAPI_TRANSACTION_ROLLBACK'.

  else.

    call function 'BAPI_TRANSACTION_COMMIT'

      exporting

        wait = 'X'.

  endif.

  close dataset log.

endfunction.

Former Member
0 Kudos

Hello guys,

we are looking forward to create a Web Service that should be able to create a Document Info Record on SAP ERP Application Server. As we don't have installed the SAP Standard Web Services on our SAP ERP ECC 6.0 system we manually have to create a Web Service with the help of the standard BAPI BAPI_DOCUMENT_CREATE2.

As the current file upload from User Clients with a Web Service based on the Standard BAPI BAPI_DOCUMENT_CREATE2 is not possible I also would like to know how the Web application looks like that calls the BAPI solution from Fred (via BIN_Content).

Thanks a lot!!

Regards,

Kriz

Former Member
0 Kudos

In fact it was for the hara parida question ... but same answer !

Former Member
0 Kudos

Hi All,

I am trying out the above code. I am able to sucessfully upload the file into R/3. But when i try to open the file from CV03N the file appears to be in a corrupted format. Can someone let me know what is the format of data that is to be passed to the parameter BIN_CONTENT in the RFC?

Thanks,

Prasath N

Former Member
0 Kudos

Dear All,

Can anyone help me in getting the RFC which will upload an image file from XI to KPro(KPro is like DMS).

Thanks & Regards

Rajiv Roshan

Former Member
0 Kudos

HI all:

Former Member
0 Kudos

HI all:

I NEED TO UPLOAD A FILE FROM WD TO r3( DMS), SUCH AS A function Cv01n TO A WBS OR OTHERS.

NOW I CAN IMPORT THE FILE WITH A PARAMETER TYPE XSTING INTO r3. BUT HOW CAN I DO WITH THE xstring parameter,PUT IT INTO CV04N.

NEED ALL OF URS HELP,THANKS ALL A LOT.

Former Member
0 Kudos

HI Fred,

Thanks for the wonderful Input.

I have the same requirement where I need offer the document uploading option in my java webdynpro screen to the enduser's through which they will upload the supporting document which can be maximum of size 10 MB.User can upload 2 or 3 documents at a time.

for that I have referred the below link :

https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/00062266-3aa9-2910-d485-f1088c3a...

Now I have to send this document to SAP R/3 from Webdynpro.

I have referred your custom RFC you says that -The ABAP program should be (you must performed some adaptations) : what type of adaption I should perform?

You can map the bin_content attribute with a binary field of your Web application context. -


How to map this bin_Content attribute with a binary field

Please explain with your example

Thanks in Advance,

Dharani

Former Member
0 Kudos

Hi Chang,

Sorry, my answer will arrive a little bit late ...

The answer to your question is yes.

You can use it for any upload. You have only to manage WSApplication type.

I use it for doc and pdf file.

Regards.

Fred.

Former Member
0 Kudos

Hi Vicky.

I have a similar project. I will upload a file from somewhere on the web, but not with a web dynpro.

Have you had any luck?

Thanks in advance for any input.

Steve

Former Member
0 Kudos

hi ravi,

could yoy explain me more on fred's RFC.could you give me

all the steps from web dynpro to sap r/3.its very urgent.

thanx in advance.

regards.

pp

Former Member
0 Kudos

Hi Hari,

Just wondering if you managed to get a head around this one. Did this upload from WebDynpro to R/3 work for you at all?

Thanks,

Vicky

Former Member
0 Kudos

hi fred,

i got everything about how to upload a file from web dynpro to DMS.could you explore on that given code.but i want to upload any file like pdf,xls and doc.will it possible by your givrn code or i have to do something.kindly give me some input.itsvery urgrnt.

thanx in advance

regards,

pp

Former Member
0 Kudos

I applied Fred's solution and it worked.

Sorry Ravi, I think the WS_UPLOAD solution won't work because I call RFC function by Web Dynpro and this ABAP function (WS_UPLOAD / WS_DOWNLOAD) doesn't work in background (RFC) process.

Thank all of you.

Regards,

Chang

Former Member
0 Kudos

hi

np. You are right. But in your case you can use Open dataset like i mentioned earlier and fred has infact given a detailed example.

regards

ravi

Former Member
0 Kudos

Hi,

I have same requirement in my project,can u tell me how to do it from Web Dynpro java.

Thanks and Regards,

Pravin

Former Member
0 Kudos

Hi Chang,

I have same requirement as I want to upload file from web application to R/3, Can you provide me a sample code with respect to Webdynpro Java.

Thanks in advance.

Pravin.

Former Member
0 Kudos

HI Chang/Pravin,

I have same requirement as I want to upload file from web application to R/3, Can you provide me a sample code with respect to Webdynpro Java.

Thanks in Advance,

Dharani

Former Member
0 Kudos

Hi

I havent used this BAPI. But i guess You can try these steps

Create a wrapper RFC which makes a call to WS_DOWNLOAD and BAPI_DOCUMENT_CREATE.

The reason for using WS_DOWNLOAD would be that you can directly download the data to your local filesystem. Then you will have the file on your pc.

WS_DOWNLOAD Function module would take in FileName and FileType as input.

But if you would like to write the file to the server where SAP is running (unix in your case) then you could use the Transfer api in abap to write the data.

Check this link. It talk about writing the file.

http://help.sap.com/saphelp_47x200/helpdata/en/fc/eb3c8c358411d1829f0000e829fbfe/frameset.htm

This will help you in sorting out your problem. Well the above approaches gives you and idea on how you can write the files to your local filesystem as well as the server.

Note : WS_DOWNLOAD and WS_UPLOAD function modules will be deal with the files on the client system. Open dataset and Transfer deal with files on the server.

Hope that was helpful.

regards

ravi