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: 

Upload Excel to content server using an RFC

Former Member
0 Kudos

Hi ,

We are trying to upload an excel file to the SAP content server using an RFC. The RFC will be invoked from .NET .

Created an RFC , which uses SAPoffice objects to upload the file to the content server. Uses various FM like

SO_LOIO_PHIO_CREATE,SO_SPLIT_FILE_AND_PATH and SO_KPRO_DATA_INTO_OBJCONT_PUT just to name a few. This RFC works fine when I test from

SAP GUI, but when we test from the RFC, it errors out. The file is stored in the local folder and the file path is sent thru the RFC.

From .NET the user uses single signon to logon to SAP.

Appreciate any input.

Thanks

Lalitha

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi

Lalitha,

If you give the file path to the RFC it will wont work. For handling this requirement you need to pass the entire file to the RFC not the Path.

What you can do is  read the file in binary mode form .net application and pass this binary file to the RFC.

Regards,

Mitul.

26 REPLIES 26

Former Member
0 Kudos

Hi

Pls check RFC destination is configured correctly or not..and also pls check SM58 to check the RFC log entries.

Thanks,

Chandra

former_member222709
Contributor
0 Kudos

Hi Lalitha,

There are many possible reasons for the RFC errors.

1. Incorrect Username/Password.

2. User Not Authorised for RFC or other authorisations for the Transaction.

3. Firewalls in the source and destination system which restrcit access.

Hence, if you can share some inputs about the error received and whether your RFC call reached SAP and then SAP rejected the call or whatever the error maybe.

Regards,

Pranav.

Former Member
0 Kudos

Hi

Lalitha,

If you give the file path to the RFC it will wont work. For handling this requirement you need to pass the entire file to the RFC not the Path.

What you can do is  read the file in binary mode form .net application and pass this binary file to the RFC.

Regards,

Mitul.

0 Kudos

Hi Mitul,

I will try this option, Can you give me more details as to how to get the file in Bin format into SAP, what would be the data type etc, coz right now for the RFC, we have all the fields defined as type char, and later move it to an internal table with the right datatype.

I will talk to the .NET resource also, but would be great if you could let me know more info regarding this.

@Pranav and Chandra, the RFC connection as such is working , we were able to test the communication between the 2, it fails when I try to use one of the SAPOffice FMs.

Thanks

Lalitha

0 Kudos

Hi Lalitha,

Actually your .net application should send the binary file while calling the Rfc.

In Rfc you can create a import parameter of type XSTRING to get the file.

Now if you can given me some details of your application like file type and after geting the file what you want to do with it. I can help in this, as I'm working on similar type of application.

Regards,

Mitul

0 Kudos

Hi Mitul,

We are trying to do an interface with .NET, The user will have an excel file (which has some inbuilt macros for validations) , The excel has the header and line items (could be upto 10,000 lines) , Have an option to Upload, when they click on upload, .NET will send the header and line items in an XML format to SAP using an RFC. At our end in SAP, we have defined Table types and line types to receive this RFC and then park it using a BDC call on FB01. Now depending on the document amount workflow will be triggered, when the WF is triggered they would like to be able to see the excel template before they approve the document for posting.

So we need to be able to upload the excel file into the SAP content server, so that we can rebuild the excel file when the approver wants to view it.

Now to do this , I tried the Functions available in SAPOffice, it worked fine in SAP, I was able to upload and then using that flowitem number, my colleague was able to view the excel. but when calling the same RFC from .NET, it fails when trying to split the file name itself using the FM SO_SPLIT_FILE_AND_PATH . .NET sends only the file path, and am trying to upload it from the users local system.

So I am assuming the OLE object option might not work for the RFC since it is being called from .NET. The user does sign on to SAP before the RFC is called.

Please let me know if you want me to email the current code that works in SAP.

So Basically I want to be able to load an Excel file into the SAP content server, using an RFC
(we are using the same RFC for parking and posting the document also) called from .NET.
and later on I shd be able to view the excel from SAP, like when the approver opens the workitem to approve (the approval screen will be custom) they will have the option to view the uploaded excel file.

Thank you for checking this

Lalitha

0 Kudos

Hi Lalitha,

Sorry for getting confused , but I want to clarify the following:

1. Thanks for the detailed description, but the confusion is whether you want the RFC to pass the File path or the file?

2. If the file needs to be transferred by the RFC in XML, then, the file path getting distorted is not applicable.

3. If .NET is able to read the file, but, unable to pass it to SAP - here we have options.

Regards,

Pranav.

0 Kudos

Hi Lalitha,

Got your scenario. I am working same type application but its a java standalone application.  

Now, where you want to store the file for later viewing  some DMS (like KPRO) server?

Regards

Mitul

0 Kudos

Hi Mitul,

Your right, KPRO is where I want to store the file,

So I can read from there later on in SAP

@ Pranav-passing data from .net is not a problem

I want to upload the whole file to SAP content server.

@Gaurab-Rfc connection is working fine, we r able to pass data back and forth from . Net to SAP.

Thank you all, appreciate it

Lalitha

0 Kudos

Hi,

    In order to store the file in DMS, you can use BAPIs 'BAPI_DOCUMENT_CREATE2' & 'BAPI_DOCUMENT_CREATENEWVERSION' and 'BAPI_DOCUMENT_CREATENEWVRS2'. So each time if you upload and excel file you need to create a new document, you can also upload into the same document create using BAPI 'BAPI_DOCUMENT_CHANGE2'. The DMS can be linked to Purchase Orders, Materials, Inspection lots etc, it can be linked to FI related things also not sure on this. Details of the BAPIs are given under there documentations, please refer them.

The uploaded documents can be viewed in CV03N transaction in SAP.

Thanks & Regards

Bala Krishna

0 Kudos

Hi,

    In order to store the file in DMS, you can use BAPIs 'BAPI_DOCUMENT_CREATE2' & 'BAPI_DOCUMENT_CREATENEWVERSION' and 'BAPI_DOCUMENT_CREATENEWVRS2'. So each time if you upload and excel file you need to create a new document, you can also upload into the same document create using BAPI 'BAPI_DOCUMENT_CHANGE2'. The DMS can be linked to Purchase Orders, Materials, Inspection lots etc, it can be linked to FI related things also not sure on this. Details of the BAPIs are given under there documentations, please refer them.

The uploaded documents can be viewed in CV03N transaction in SAP.

Thanks & Regards

Bala Krishna

0 Kudos

Hi,

Now from .net app are you getting the file  as FileStream? As earlier I've said create an Import parameter in UR RFc with type XSTRING.Once this is done you can proceed further.

Regards,

Mitul.

0 Kudos

Hi Mitul,

I have not tried this option yet, the .net resource is yet to get back to me after checking if they can send the file as binary, so with your suggestion , can I get the file with just one call ?

Will try the code you suggested today and give am update .

@Bala-but can this Bali be called from .net?

I will check it out

Thanks and Regards

Lalitha

0 Kudos

Hi,

   It is a BAPI, so you can easily call it from .Net.

Thanks & Regards

Bala Krishna

0 Kudos

Hi Lalitha,

First He(.NET Resource) has to send Binary format from .NET Application,

After Getting File Check with this code ,

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

*"*"Local Interface:

*"  IMPORTING

*"     VALUE(IV_FLAG) TYPE  CHAR2

*"     VALUE(IV_BASEFILE) TYPE  XSTRING OPTIONAL

*"     VALUE(IV_VENDORCODE) TYPE  LIFNR OPTIONAL

*"     VALUE(IV_DOCUMENTNO) TYPE  BAPI_DOC_DRAW2-DOCUMENTNUMBER

*"       OPTIONAL

*"     VALUE(IV_ATTACHMENT1) TYPE  XSTRING OPTIONAL

*"     VALUE(IV_CREATEDBY) TYPE  ZCREATEDBY OPTIONAL

*"     VALUE(IV_REMARKS) TYPE  ZREMARKS OPTIONAL

*"  EXPORTING

*"     VALUE(EV_DOCUMENTNUMBER) LIKE  BAPI_DOC_AUX-DOCNUMBER

*"  TABLES

*"      S_ERRORS STRUCTURE  BAPIRET2 OPTIONAL

*"      S_DOCUMENTFILES STRUCTURE  BAPI_DOC_FILES2 OPTIONAL

*"      S_ITEM_DETAIL STRUCTURE  YFI_VP_ITEM OPTIONAL

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

   DATA: ls_error TYPE bapiret2.

  CONSTANTS: c_path_name TYPE localfile VALUE '/tmp/',

           c_http_dest TYPE rfcdes-rfcdest VALUE 'SAPHTTPA ',

           c_ftp_dest  TYPE rfcdes-rfcdest VALUE 'SAPFTPA ',

           c_x(1)      TYPE c VALUE 'X'.

  DATA: iv_documentdata    TYPE  bapi_doc_draw2,

        wa_doc             TYPE bapi_doc_files2,

        wa_files           TYPE bapi_doc_files2,

        it_files           TYPE TABLE OF bapi_doc_files2,

        wa_attachment      TYPE xstring,

        it_attachment      LIKE TABLE OF wa_attachment,

        wa_documentfiles   TYPE bapi_doc_files2,

        it_docdescriptions TYPE STANDARD TABLE OF  bapi_doc_drat,

        wa_docdescriptions TYPE bapi_doc_drat,

        wa_return          TYPE bapiret2,

        w_file_name        TYPE localfile,

        w_msg(80)          TYPE c,

        lv_filename        TYPE string,

        lv_fileextension   TYPE c LENGTH 3,

        lv_index2          TYPE n VALUE 1,

        p_rc TYPE inri-returncode.

  DESCRIBE TABLE s_documentfiles LINES sy-tfill.

  IF sy-tfill > 0.

    READ TABLE s_documentfiles INTO wa_documentfiles

                               INDEX 1.

* Assigning The Value DUPLICATE(D) If Document Exist

    CALL FUNCTION 'NUMBER_GET_NEXT'                         "#EC *

            EXPORTING

              nr_range_nr      = 'N1'

              object           = 'YFIGL_EJV'

*             SUBOBJECT\       = 'ZDOCNUM'

            IMPORTING

              number           = iv_documentno

              returncode       = p_rc

            EXCEPTIONS

              interval_not_found            = 1

              number_range_not_intern       = 2

              object_not_found              = 3

              quantity_is_0                 = 4

              quantity_is_not_1             = 5

              interval_overflow             = 6

              buffer_overflow               = 7

              OTHERS                        = 8 .

    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'

      EXPORTING

        input  = iv_documentno

      IMPORTING

        output = iv_documentno.

    CONCATENATE 'CP' iv_documentno INTO   iv_documentno.

* populate document data

    iv_documentdata-documenttype    = 'INV'.

    iv_documentdata-documentnumber  = iv_documentno.

    iv_documentdata-documentpart    = '000'.

    iv_documentdata-documentversion = '00'.

*Filling The Attachments in Structure

    IF iv_basefile IS NOT INITIAL.

      wa_attachment = iv_basefile.

      APPEND wa_attachment TO it_attachment.

      CLEAR wa_attachment.

    ELSE.

      wa_return-type        = 'E'.

      wa_return-id          = '00'.

      wa_return-number      = '1'.

      wa_return-message     = 'Base file stream data cannot be blank'(031)."#EC NOTEXT

      APPEND wa_return TO s_errors.

      EXIT.

    ENDIF.

    IF iv_attachment1 IS NOT INITIAL.

      wa_attachment = iv_attachment1   .

      APPEND wa_attachment TO it_attachment.

      CLEAR wa_attachment.

    ENDIF.

    CLEAR wa_doc.

    CLEAR it_files[].

    LOOP AT  s_documentfiles INTO wa_doc.

      CLEAR: lv_filename, lv_fileextension.

      SPLIT wa_doc-docpath AT '.' INTO lv_filename lv_fileextension .

      IF lv_fileextension = 'txt'.

        lv_fileextension = 'XLS'.

      ENDIF.

      TRANSLATE lv_fileextension TO UPPER CASE.

      wa_files-wsapplication = lv_fileextension.

      CONCATENATE c_path_name

                  lv_filename

                  '.'

                  wa_files-wsapplication

                  INTO w_file_name.

      CONDENSE w_file_name  NO-GAPS.

*            TRANSFER w_file_name TO c_log.

      OPEN DATASET w_file_name FOR OUTPUT

                               IN BINARY MODE

                               MESSAGE w_msg.

      IF sy-subrc <> 0.

        wa_return-type        = 'E'.

        wa_return-id          = sy-msgid.

        wa_return-number      = sy-msgno.

        wa_return-message     = w_file_name.

        wa_return-message_v1  = sy-msgv1.

        wa_return-message_v2  = sy-msgv2.

        wa_return-message_v3  = sy-msgv3.

        wa_return-message_v4  = sy-msgv4.

        APPEND wa_return TO s_errors .

        EXIT.

        "MESSAGE a899(zz) WITH w_file_name w_msg 'SY-SUBRC:' sy-subrc.

      ENDIF.

*Transfer Attachment Content to Application Server

      CLEAR wa_attachment.

      READ TABLE it_attachment INDEX lv_index2

                               INTO wa_attachment.

      TRANSFER wa_attachment TO w_file_name.

      CLOSE DATASET w_file_name.

*            TRANSFER 'move data to IT_FILES' TO c_log.

* Filling Document Files structure

      wa_files-documentnumber  = iv_documentno."wa_doc-documentnumber.

      wa_files-storagecategory = 'ZSC_AP'.

      wa_files-wsapplication   = lv_fileextension.

      wa_files-docfile         = w_file_name.

      APPEND wa_files TO it_files.

*      CLEAR w_file_name.

      CLEAR s_errors.

*            CLOSE DATASET c_log.

      lv_index2 = lv_index2 + 1.

    ENDLOOP.

*Filling Document Description Structure

    wa_docdescriptions-language    = 'EN'.

    wa_docdescriptions-description = iv_documentno."wa_doc-documentnumber.

    APPEND wa_docdescriptions TO it_docdescriptions.

    CALL FUNCTION 'BAPI_DOCUMENT_CREATE2'

  EXPORTING

    documentdata         = iv_documentdata

    pf_ftp_dest        = c_ftp_dest

    pf_http_dest         = c_http_dest

  IMPORTING

    documentnumber       = iv_documentno

    return               = wa_return

  TABLES

    documentdescriptions = it_docdescriptions

    documentfiles        = it_files.

0 Kudos

Hi Mitul,

Thank you for the code, I will try it out, am still waiting on the confirmation from the .NET person. He shd let me know today and after that I can try this out. Can I test this in SAP itself ?

Thanks and Regards

Lalitha

0 Kudos

Hi Lalitha,

You can Try This Code.

Let me Know if it is resolved .

Regards,

Mitul.

0 Kudos

Hi Mitul,

Quick question on your code, will a FI Document get created ?

or do we need a document for this code to work ?

IV_DOCUMENTNO - was wondering about this field.

Thanks and Regards

Lalitha

P.S - Sorry, could'nt try out the code from last 2 days,

0 Kudos

Hi Lalitha,

It will not Create FI document its will create a Document in KPRO Only.

This a Document No what is Created in KPRO.

Regards,

Mitul.

0 Kudos

Hi Mitul,

Am getting some syntax error, but that is fine, just wanted to check what are the import parameters i need to pass?

Just the filestream or are some other parameters needed?

And can I test it in sap first before I get .net involved, if I can convert an excel to binary , was hoping to test in sap itself.

Thank you

Lalitha

0 Kudos

Hi,

     The BAPI can be directly used with .Net, ask you .Net developer about that. If he agrees you can directly specify the path where the excel file is located to the bapi 'BAPI_DOCUMENT_CREATE2'.

Just try to execute in the bapi in SE37.

Goto SE37, Menu Function Module>Test>Test Sequence

Give the Bapi name 'BAPI_DOCUMENT_CREATE2' first and 'BAPI_TRANSACTION_COMMIT' second.

Execute.

Provide below values in importing parameter DOCUMENTDATA

DOCUMENTTYPE      " check in your system which document types configured

DOCUMENTNUMBER

DOCUMENTVERSION

DOCUMENTPART

DESCRIPTION

In Tables DOCUMENTFILES create one entry with below fields

STORAGECATEGORY ='DMS_CT_ST'

WSAPPLICATION        = 'XLS'

DOCFILE = 'C:\Temp\Reocrds.xls'    Path of excel file on local directory.

Click on execute.

For any other details click on the BAPI documentation in SE37.

Thanks & Regards

Bala Krishna

0 Kudos

Hi Mitul,

This worked , was able to upload an XSTRING to the DMS server, we might have to do some configuration for the storage category and the document type, but for now we just tested with some existing ones and were able to upload to the server and then view it from CV03N.

Thanks Bala , for your suggestion also, the BAPI_COMMIT was an important step, without it the document number was generated, but the actual document was not saved into the server.

The only difference I see from the existing code ( the one that works fine in SAP) is that once the file is uploaded, it gives a different number, and the one we used gives a different one.,

the object type is SOFM and the object ID example is FOL26000000000004EXT29000000000163,

where the 163 seems to be the document ID.

Maybe something is different with the storage category etc, will check with our DMS team.

Now we do need to be able to read and display this file on a click of a button by the approver, the BAPI BAPI_DOCUMENT_CHECKOUTVIEW2 seems promising, it does download the file to the specified directory, so maybe we can download it to a temp folder and open it up.

Appreciate all the responses,

Thank you,

Lalitha

0 Kudos

Hi Mitul,

Just wanted to check if there is a way to read this excel file that is uploaded to the content server.

I tried the BAPI_DOCUMENT_CHECKOUTVIEW2, it downloads to the temp folder in the local drive and then we need to read the excel file. Is there any other option, I am concerned since we will be trying to read the excel in background mode , so not sure if this option will work.

Any input is appreciated.

Thanks

Lalitha

0 Kudos

Hi,

     Try with below way.

1) Create program, first call the Bapi 'BAPI_DOCUMENT_GETDETAIL2', to read the Document details.

2) Based on the above bapi values returned in the tables parameter DOCUMENTFILES, you can get the attached xls file details.

3) Use function module SCMS_R3DB_GET to get the xls contents in binary table, table parameter DATA_BIN will have the binary content.

4) Have to see for fm s to convert binay into text, so that we use that for processing.

Above

CREP_ID = DOCUMENTFILES-STORAGECATEGORY+0(6).

DOC_ID   = DOCUMENTFILES-FILE_ID.

COMP_ID = DOCUMENTFILES-DOCFILE. " We have to take only the file name with extension.

Thanks & Regards

Bala Krishna

0 Kudos

Hi Bala,

Thank you, I tried this, but not able to find a FM to convert the binary to string /text.

Let me know if there is some other option, thank you

Lalitha

gaurab_banerji
Active Participant
0 Kudos

go to sm58, open rfc name and test rfc connection.. if test shows green line... and some seconds etc, then rfc is fine, in case of red line, there is a problem