Get attachments from CRM document

Problem: you have a CRM document which contains attachments. You want to get these attachments – send them to another system or download as a file.

Solution: Getting attachments’ contents is not as easy as it should be. You need to execute few Function Modules one by one to get desired data. Here is what needs to be done.

First you need to get the information about all the attachments which were added to the document. For this you need to call SKWG_OLS_BO_LINKS_GET FM. It requires you to provide basic information about document which you want to inspect. Information such as:

  • document GUID
  • document type
  • document category

In return you will receive list of attachments assigned to the given document. This list will now be used to get detailed information and contents of each of the attachments.

Detailed information is provided by FM called SKWF_LOIO_ALL_PHIOS_GET. This information is needed to get contents of the attachment – using CRM_ICSS_GET_SR_ATTA_CONTENT FM.

Each attachment can be either ASCII or a binary file. You need to download proper type of data to have your file correct. Unfortunately, despite of what parameters might tell you, FM CRM_ICSS_GET_SR_ATTA_CONTENT can return only one type of data at a time (ASCII or binary). So, you need to first download the ASCII data, and then, if ASCII is empty, download binary data. This part is skipped in the example code below.

DATA:
  ls_business_object TYPE sibflporb,
  lv_error TYPE skwf_error,
  lt_ios TYPE TABLE OF skwf_io,
  ls_ios TYPE skwf_io,
  lt_phios TYPE TABLE OF skwf_io,
  lt_prop_res TYPE TABLE OF sdokproptl,
  lt_prop_req TYPE TABLE OF sdokproptn,
  ls_phios TYPE skwf_io,
  lt_content_asc TYPE TABLE OF sdokcntasc,
  lt_content_raw TYPE TABLE OF sdokcntbin.
 
* for example: 45EB8D0880F110EE000000000AC90357
ls_business_object-instid = iv_document_guid.
* "for example: BUS2000116
ls_business_object-typeid = iv_document_type.
ls_business_object-catid = 'BO'.
 
CALL FUNCTION 'SKWG_OLS_BO_LINKS_GET'
  EXPORTING
    business_object = ls_business_object
    cspc            = 'CRM'
  IMPORTING
    error           = lv_error
  TABLES
    ios             = lt_ios.
IF lv_error IS NOT INITIAL.
  WRITE lv_error.
  RETURN.
ENDIF.
 
LOOP AT lt_ios INTO ls_ios.
  CALL FUNCTION 'SKWF_LOIO_ALL_PHIOS_GET'
    EXPORTING
      loio               = ls_ios
    TABLES
      phios              = lt_phios
      properties_request = lt_prop_req
      properties_result  = lt_prop_res.
 
  READ TABLE lt_phios INTO ls_phios INDEX 1.
  IF sy-subrc = 0.
    CALL FUNCTION 'CRM_ICSS_GET_SR_ATTA_CONTENT'
      EXPORTING
        phio           = ls_phios
        iv_x_raw       = 'X'
        iv_x_asc       = 'X'
      TABLES
        et_content_asc = lt_content_asc
        et_content_raw = lt_content_raw.
  ENDIF.
ENDLOOP.
DATA:
  ls_business_object TYPE sibflporb,
  lv_error TYPE skwf_error,
  lt_ios TYPE TABLE OF skwf_io,
  ls_ios TYPE skwf_io,
  lt_phios TYPE TABLE OF skwf_io,
  lt_prop_res TYPE TABLE OF sdokproptl,
  lt_prop_req TYPE TABLE OF sdokproptn,
  ls_phios TYPE skwf_io,
  lt_content_asc TYPE TABLE OF sdokcntasc,
  lt_content_raw TYPE TABLE OF sdokcntbin.

* for example: 45EB8D0880F110EE000000000AC90357
ls_business_object-instid = iv_document_guid.
* "for example: BUS2000116
ls_business_object-typeid = iv_document_type.
ls_business_object-catid = 'BO'.

CALL FUNCTION 'SKWG_OLS_BO_LINKS_GET'
  EXPORTING
    business_object = ls_business_object
    cspc            = 'CRM'
  IMPORTING
    error           = lv_error
  TABLES
    ios             = lt_ios.
IF lv_error IS NOT INITIAL.
  WRITE lv_error.
  RETURN.
ENDIF.

LOOP AT lt_ios INTO ls_ios.
  CALL FUNCTION 'SKWF_LOIO_ALL_PHIOS_GET'
    EXPORTING
      loio               = ls_ios
    TABLES
      phios              = lt_phios
      properties_request = lt_prop_req
      properties_result  = lt_prop_res.

  READ TABLE lt_phios INTO ls_phios INDEX 1.
  IF sy-subrc = 0.
    CALL FUNCTION 'CRM_ICSS_GET_SR_ATTA_CONTENT'
      EXPORTING
        phio           = ls_phios
        iv_x_raw       = 'X'
        iv_x_asc       = 'X'
      TABLES
        et_content_asc = lt_content_asc
        et_content_raw = lt_content_raw.
  ENDIF.
ENDLOOP.

Komentowanie jest zakończone.