Skip to Content
For a complete list of all my blogs regarding content management, please see here.
I create a utility class with method CREATE_DOC. It has following four input parameters:
  • iv_data type xstring – the binary data which you would like to store as attachment
  • iv_bor_type type string – the BOR type of your business object. You can view view business object model in tcode SWO1
  • iv_guid type raw16 – the guid of your business object instance
  • iv_file_name type string – the file name which will appear in attachment assignment block.
The source code of method below: ( in fact all attributes for an attachment could be available in the input parameters of this method. For simplicity reason I
just hard code them in the sample code )
DATA:ls_bo              TYPE sibflporb,
         ls_prop            TYPE LINE OF sdokproptys,
         lt_prop            TYPE sdokproptys,
         lt_properties_attr TYPE crmt_attr_name_value_t,
         ls_file_info       TYPE sdokfilaci,
         lt_file_info       TYPE sdokfilacis,
         lt_file_content    TYPE sdokcntbins,
         lv_length          TYPE i,
         lv_file_xstring    TYPE xstring,
         ls_loio            TYPE skwf_io,
         ls_phio            TYPE skwf_io,
         ls_error           TYPE skwf_error.
    ls_prop-name = 'DESCRIPTION'.
    ls_prop-value = 'created by Tool'. " replace it with your own description for attachment
    APPEND ls_prop TO lt_prop.
    ls_prop-name = 'KW_RELATIVE_URL'.
    ls_prop-value = iv_file_name. " in the sample code I just reuse file name as relative url
    APPEND ls_prop TO lt_prop.
    ls_prop-name = 'LANGUAGE'.
    ls_prop-value = sy-langu.
    APPEND ls_prop TO lt_prop.
    lv_file_xstring = iv_data.
    CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
      EXPORTING
        buffer        = lv_file_xstring
      IMPORTING
        output_length = lv_length
      TABLES
        binary_tab    = lt_file_content.
    ls_file_info-binary_flg = 'X'.
    ls_file_info-file_name = iv_file_name.
    ls_file_info-file_size = lv_length.
    ls_file_info-mimetype = 'image/jpeg'. "use the correct mime type for your attachment
    APPEND ls_file_info TO lt_file_info.
    ls_bo-INSTID = iv_guid.
    ls_bo-typeid = iv_bor_type.
    ls_bo-catid = 'BO'.
    CALL METHOD cl_crm_documents=>create_with_table
      EXPORTING
        business_object     = ls_bo
        properties          = lt_prop
        properties_attr     = lt_properties_attr
        file_access_info    = lt_file_info
        file_content_binary = lt_file_content
        raw_mode            = 'X'
      IMPORTING
        loio                = ls_loio
        phio                = ls_phio
        error               = ls_error. " evaluate if there is anything wrong during creation

COMMIT WORK.
I write a piece of code to test it. After report runs I could see the generated attachment.
/wp-content/uploads/2013/11/clipboard1_334378.png
/wp-content/uploads/2013/11/clipboard2_334400.png
You can also test whether the attachment is created successfully in the backend. Test class method get_info in SE24.
Specify importing parameter BUSINESS_OBJECT:
/wp-content/uploads/2013/11/clipboard3_334401.png
/wp-content/uploads/2013/11/clipboard4_334402.png
Execute and you should get result as below: one physical object and one logical object according to how-is-attachment-physically-stored-in-database-table-in-cm-framework.
/wp-content/uploads/2013/11/clipboard5_334403.png
Never forget to call COMMIT WORK in your code, since the persistence of the relationship between attachment and your business object are implemented via generic object service in a update process.You could easily find this via SAT trace on your code
/wp-content/uploads/2013/11/clipboard7_334404.png
or switch on update debugging in your debugger settings.
/wp-content/uploads/2013/11/clipboard8_334405.png
To report this post you need to login first.

2 Comments

You must be Logged on to comment or reply to a post.

  1. B.Chella Pandian

    Hi Jerry,

    In My requirement I need to attach a MS word file (.docx) which is available in the Application server to a one One order.

    I have the Application server File path with me. Now I need to attach that file to the Quotation order.

    Can please advise me how to proceed.

    Regards,

    Chella.

    (0) 
    1. B.Chella Pandian

      Attach a file form Application server to One order. Same solution as above.

        DATA :

        lv_fname TYPE  string,

        lv_filename TYPE  string,

        lin TYPE i,

        lv_extn TYPE  string,

        lv_mimetype TYPE  w3conttype,

        lt_properties TYPE sdokproptys,

        ls_properties LIKE LINE OF lt_properties,

        lt_file_access_info    TYPE sdokfilacis,

        ls_file_access_info LIKE LINE OF lt_file_access_info,

        lt_file_content_binary TYPE sdokcntbins,

        ls_file_content_binary LIKE LINE OF lt_file_content_binary.

        DATA: ls_bor TYPE sibflporb,

        lv_loio TYPE skwf_io,

        lv_phio TYPE skwf_io,

        lv_error TYPE skwf_error.

        DATA : v_file TYPE string,

             lv_msg TYPE string.

        DATA: result_tab TYPE STANDARD TABLE OF string.

        v_file = ‘/tmp/sapinst/dbclient.lst’.

        SPLIT v_file AT ‘/’ INTO TABLE result_tab.

        DESCRIBE TABLE result_tab LINES lin.

        READ TABLE result_tab INDEX lin INTO lv_filename.

      *——————————————————————————*

      *      read the file from the application server

      *——————————————————————————*

        OPEN DATASET v_file FOR INPUT MESSAGE lv_msg IN LEGACY BINARY MODE.

        IF sy-subrc NE 0.

          RETURN.

        ELSE.

          WHILE ( sy-subrc EQ 0 ).

            READ DATASET v_file INTO ls_file_content_binary.

            IF NOT ls_file_content_binary IS INITIAL.

              APPEND ls_file_content_binary TO lt_file_content_binary.

            ENDIF.

            CLEAR ls_file_content_binary.

          ENDWHILE.

        ENDIF.

        CLOSE DATASET v_file.

        MOVE: ‘BUS2000115’ TO ls_bor-typeid,

              ‘BO’ TO ls_bor-catid,

              ‘0026B98B6AD11ED4B6AC5E61242D9CFE’ TO ls_bor-instid.

        CALL FUNCTION ‘CRM_KW_SPLIT_FILENAME’

          EXPORTING

            iv_path      = v_file

          IMPORTING

            ev_filename  = lv_fname

            ev_extension = lv_extn

            ev_mimetype  = lv_mimetype.

        DESCRIBE TABLE lt_file_content_binary.

        ls_file_access_info-file_size  = sy-tfill * sy-tleng.

        ls_file_access_info-binary_flg = ‘X’.

        ls_file_access_info-first_line = 1.

        ls_file_access_info-file_name  = v_file. “FILE path ON al11.

        ls_file_access_info-mimetype   = lv_mimetype.

        ls_file_access_info-property   = lv_extn.

        APPEND ls_file_access_info TO lt_file_access_info.

        ls_properties-name = ‘KW_RELATIVE_URL’.

        ls_properties-value = lv_filename. ” E.g.: EXCEL.xlsx

        APPEND ls_properties TO lt_properties.

        ls_properties-name = ‘DESCRIPTION’.

        ls_properties-value = lv_filename. “E.g.: Excel file

        APPEND ls_properties TO lt_properties.

        ls_properties-name = ‘LANGUAGE’.

        ls_properties-value = ‘EN’. ” E.g.: ‘EN’.

        APPEND ls_properties TO lt_properties.

        CALL METHOD cl_crm_documents=>create_with_table

          EXPORTING

            business_object     = ls_bor    ” Local Persistent Object Reference – BOR-Compatible

            properties          = lt_properties    ” SDOK: Object Attribute, Name and Feature

            file_access_info    = lt_file_access_info    ” SDOK: Entries for Document Contents in Internal Tables

            file_content_binary = lt_file_content_binary    ” SDOK: Line of Binary Document Content for WWW Server

            raw_mode            = ‘X’    ” KW Framework: Boolean Flag (‘X’ Active, ‘ ‘ Deleted)

          IMPORTING

            loio                = lv_loio

            phio                = lv_phio

            error               = lv_error.

      (0) 

Leave a Reply