Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member

1). Step-by-Step to upload the file attachment in CRM WebUI using Net Weaver Gateway.


Create the project in SEGW Transaction Code and the Entity Type:

In the Entity Type Properties select the check box: Media

And the properties of Entity Type are:

And then map the RFC function module for the Create Operation in the Entity Set.

And do the mapping for Get Entity (Read) Operation in the Entity Set.

Then Redefine the DEFINE method in the *MPC_EXT class and add the below logic:

  METHOD define.
super
->define( ).
DATA: lo_entity   TYPE REF TO /iwbep/if_mgw_odata_entity_typ,
       lo_property
TYPE REF TO /iwbep/if_mgw_odata_property.


lo_entity
= model->get_entity_type ( iv_entity_name = 'TerritoryFileAttachment' ).

 
IF lo_entity IS BOUND.
   lo_property
= lo_entity->get_property( iv_property_name = 'MIME_TYPE' ).
  
IF lo_property IS BOUND.
     lo_property
->set_as_content_type( ).
  
ENDIF.
ENDIF.

ENDMETHOD.


Then Redefine the CREATE_STREAM Method (/IWBEP/IF_MGW_APPL_SRV_RUNTIME~CREATE_STREAM) in the *DPC_EXT class and implement the below logic to upload the file attachment into the CRM WebUI for a given Territory Plan.

All input parameters/values we have to get into the SLUG parameter from the UI Side (If we have multiple input parameter values then with concatenation of multiple parameter values with delimiter we have to get the values in SLUG parameter).

  METHOD /iwbep/if_mgw_appl_srv_runtime~create_stream.

DATAls_file_attach               TYPE        ztp_s_file_attachment,
       lv_tp_guid                  
TYPE        crm_mktpl_ib_mpl_guid,
       ls_key                      
TYPE        /iwbep/s_mgw_tech_pair,
       lt_keys                     
TYPE        /iwbep/t_mgw_tech_pairs,
       lv_entityset_name           
TYPE        string,
       lv_entity_name              
TYPE        string,
       lo_tech_read_request_context
TYPE REF TO /iwbep/cl_sb_gen_read_aftr_crt,
       ls_entity                   
TYPE REF TO data,
       ls_string                   
TYPE        string.

DATAls_bo                  TYPE        sibflporb,
       lt_properties         
TYPE        sdokproptys,
       ls_properties         
TYPE        sdokpropty,
       lt_file_access        
TYPE        sdokfilacis,
       ls_file_access        
TYPE        sdokfilaci,
       lt_file_content_binary
TYPE        sdokcntbins,
       ls_loio               
TYPE        skwf_io,
       ls_phio               
TYPE        skwf_io,
       ls_error              
TYPE        skwf_error,
       lv_file_size          
TYPE        i,
       lt_messages           
TYPE        zif_zdmtp_service=>bapiret2_t,
       ls_messages           
TYPE        bapiret2,
       lo_dp_facade          
TYPE REF TO /iwbep/if_mgw_dp_facade,
       lv_destination        
TYPE        rfcdest,
       lr_dmtp_service       
TYPE REF TO zcl_dmtp_service,
       lv_tp_id              
TYPE        crm_mktpl_campaignid.


FIELD-SYMBOLS: <ls_data> TYPE any.

CLEAR: ls_file_attach, lv_tp_guid, ls_bo, lt_properties, ls_properties,
        lt_file_access
, ls_file_access, lt_file_content_binary,ls_loio,
        ls_phio
, ls_error, lv_file_size.

***IV_SLUG parameter will be passed from the front-end side
SPLIT iv_slug AT '/' INTO ls_file_attach-tp_id
                       ls_file_attach
-filename
                       ls_file_attach
-name
                       ls_file_attach
-description.

****File Type(MIME TYPE)****
ls_file_attach
-mime_type  = is_media_resource-mime_type.


****File Content in XSTRING.*****
ls_file_attach
-file_value = is_media_resource-value.

****Convert the Territory Plan ID into GUID****
CALL FUNCTION 'CONVERSION_EXIT_CGPLP_INPUT'
EXPORTING
   
input  = ls_file_attach-tp_id
IMPORTING
   
output = lv_tp_guid.

****Build Attachment Business Object****
ls_bo
-catid  = 'BO'.
ls_bo
-typeid = 'BUS2010010' .
ls_bo
-instid = lv_tp_guid.

****Build Attachment Properties****
ls_properties
-name  = 'KW_RELATIVE_URL'. "NAME
ls_properties
-value = ls_file_attach-name.
APPEND ls_properties TO lt_properties.
CLEAR ls_properties.

ls_properties
-name  = 'DESCRIPTION'. "DESCRIPTION
ls_properties
-value = ls_file_attach-description.
APPEND ls_properties TO lt_properties.
CLEAR ls_properties.

ls_properties
-name  = 'MIMETYPE'. "MIME TYPE
ls_properties
-value = ls_file_attach-mime_type.
APPEND ls_properties TO lt_properties.
CLEAR ls_properties.

****Convert the Attachment File Data from XSTRING to BINARY****
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
  
buffer                    = ls_file_attach-file_value
IMPORTING
   output_length
= lv_file_size
TABLES
   binary_tab   
= lt_file_content_binary.

****Build File Access Information****
ls_file_access
-file_size  = lv_file_size.
ls_file_access
-binary_flg = abap_true.
ls_file_access
-file_name  = ls_file_attach-filename.
ls_file_access
-mimetype   = ls_file_attach-mime_type.
APPEND ls_file_access TO lt_file_access.
CLEAR ls_file_access.

****Upload the Attachment for Territory Plan in CRM WEBUI****
CALL METHOD cl_crm_documents=>create_with_table
EXPORTING
   business_object    
= ls_bo
   properties         
= lt_properties
   file_access_info   
= lt_file_access
   file_content_binary
= lt_file_content_binary
IMPORTING
   loio               
= ls_loio
   phio               
= ls_phio
   error              
= ls_error.


IF ls_error IS INITIAL.
  ls_file_attach
-file_loio_guid = ls_loio-objid.
ELSE.
  ls_messages
-id                   = ls_error-id.
  ls_messages
-number           = ls_error-no.
  ls_messages
-type                = ls_error-type .
  ls_messages
-message_v1 = ls_error-v1 .
  ls_messages
-message_v2 = ls_error-v2 .
  ls_messages
-message_v3 = ls_error-v3 .
  ls_messages
-message_v4 = ls_error-v4 .
 
APPEND ls_messages TO lt_messages.

me
->/iwbep/if_sb_dpc_comm_services~rfc_save_log(
EXPORTING
   iv_entity_type
= iv_entity_name
   it_return     
= lt_messages
   it_key_tab    
= it_key_tab ).

****Call RFC commit work****
me
->/iwbep/if_sb_dpc_comm_services~commit_work(
EXPORTING
iv_rfc_dest
= lv_destination) .


RETURN.
ENDIF.


*-------------------------------------------------------------------------*
*             -****Read After Create -******
*-------------------------------------------------------------------------*
CREATE OBJECT lo_tech_read_request_context.
* Create key table for the read operation
ls_key
-name  = 'TP_ID'.
ls_key
-value    = ls_file_attach-tp_id.
APPEND ls_key TO lt_keys.

ls_key
-name  = 'FILENAME'.
ls_key
-value    = ls_file_attach-filename.
APPEND ls_key TO lt_keys.

ls_key
-name  = 'IV_OBJECT'.
ls_key
-value    = 'FILEATTACH'.
APPEND ls_key TO lt_keys.

ls_key
-name  = 'FILE_LOIO_GUID'.
ls_key
-value    = ls_file_attach-file_loio_guid.
APPEND ls_key TO lt_keys.

****Set into request context object the key table and the entity set name****
lo_tech_read_request_context
->set_keys( IMPORTING et_keys = lt_keys ).

lv_entityset_name
= io_tech_request_context->get_entity_set_name( ).

lo_tech_read_request_context
->set_entityset_name( IMPORTING ev_entityset_name = lv_entityset_name ).


****Call read after create****
/iwbep/if_mgw_appl_srv_runtime
~get_entity(
EXPORTING
   iv_entity_name         
= iv_entity_name
   iv_entity_set_name     
= iv_entity_set_name
   iv_source_name         
= iv_source_name
   it_key_tab             
= it_key_tab
   io_tech_request_context
= lo_tech_read_request_context
   it_navigation_path     
= it_navigation_path
IMPORTING
   er_entity              
= ls_entity ).

****Send the read response to the caller interface****
ASSIGN ls_entity->* TO <ls_data>.

IF <ls_data> IS ASSIGNED.
   copy_data_to_ref
(

        EXPORTING

            is_data = <ls_data>
   
CHANGING 

            cr_data = er_entity ).
ENDIF.
ENDIF.
ENDMETHOD.


Once the CREATE_STREAM method redefines is done then we can test the service using the REST Client to upload the file attachment into CRM WebUI for Territory Plan.

Please Note: Maximum file size 30 MB will allow to upload the file from CRM WebUI standard functionality.

Test the service using the Rest-client.

First get the CSRF-Token value while calling below service.

Then in the response we will get the CSRF Token Value.

Then give the CSRF Token value and SLUG parameter in the Headers and choose the file to upload it via OData Service. Once we click on send then file will be uploaded in CRM WebUI for a given Territory Plan.

2). Step-by-Step to Read/Download the file attachment from CRM WebUI using Net Weaver Gateway.


Continuation with Step1, Redefine the GET_STREAM Method (/IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_STREAM) in the *DPC_EXT class and implement the below logic to read/download the file attachment from the CRM WebUI for a given Territory Plan.

  METHOD /iwbep/if_mgw_appl_srv_runtime~get_stream.
DATAls_key_tab         TYPE LINE OF /iwbep/t_mgw_name_value_pair,
       ls_stream         
TYPE              ty_s_media_resource,
       is_file_attachment
  TYPE              ztp_s_file_attachment,
       es_file_attach    
TYPE              ztp_s_file_attachment,
       lv_media_value    
TYPE               xstringval,
       lv_mime_type      
TYPE               string,
       lo_data           
TYPE REF TO  zcl_dmtp_service,
       ls_header         
TYPE               ihttpnvp.

****Read the Key Field Values****
CLEAR: is_file_attachment, ls_key_tab.
READ TABLE it_key_tab INTO ls_key_tab WITH KEY name = 'TP_ID'.
IF sy-subrc = 0.
  is_file_attachment
-tp_id = ls_key_tab-value.
ENDIF.

CLEAR ls_key_tab.
READ TABLE it_key_tab INTO ls_key_tab WITH KEY name = 'FILENAME'.
IF sy-subrc = 0.
  is_file_attachment
-filename = ls_key_tab-value.
ENDIF.

CLEAR ls_key_tab.
READ TABLE it_key_tab INTO ls_key_tab WITH KEY name = 'IV_OBJECT'.
IF sy-subrc = 0.
  is_file_attachment
-iv_object = ls_key_tab-value.
ENDIF.

CLEAR ls_key_tab.
READ TABLE it_key_tab INTO ls_key_tab WITH KEY name = 'FILE_LOIO_GUID'.
IF sy-subrc = 0.
  is_file_attachment
-file_loio_guid = ls_key_tab-value.
ENDIF.

****Read File Attachment for Territory Plan****

    DATA:ls_loio                TYPE skwf_io,
       lt_loio               
TYPE skwf_ios,
       lt_properties_result  
TYPE crm_kw_propst,
       ls_properties_result  
TYPE crm_kw_props,
       ls_error              
TYPE skwf_error,
       lt_file_content_ascii 
TYPE sdokcntascs,
       lt_file_content_binary
TYPE sdokcntbins,
       lt_file_access        
TYPE sdokfilacis,
       ls_file_access        
TYPE sdokfilaci,
       iv_length             
TYPE i,
       ls_bo                 
TYPE sibflporb,
       lv_tp_guid            
TYPE crm_mktpl_ib_mpl_guid,
       ls_doc_property       
TYPE sdokproptl.

CLEAR: ls_loio, lt_file_access, lt_file_content_ascii, lt_file_content_binary,
        ls_error
, iv_length, ls_bo, lv_tp_guid, lt_properties_result, lt_loio,
        ls_properties_result
, ls_doc_property.

****Convert the Territory Plan ID into GUID****
CALL FUNCTION 'CONVERSION_EXIT_CGPLP_INPUT'
EXPORTING
  
input  = is_file_attachment-tp_id
IMPORTING
  
output = lv_tp_guid.

****Build Attachment Business Object****
ls_bo
-catid  = 'BO'.
ls_bo
-typeid = 'BUS2010010' .
ls_bo
-instid = lv_tp_guid.

****Get the Attachment Properties information****
CALL METHOD cl_crm_documents=>get_info
EXPORTING
   business_object      
= ls_bo
IMPORTING
   ios_properties_result
= lt_properties_result
   loios                
= lt_loio.

****Read the File Attachment LOIO GUID and Classs and Object type****
READ TABLE lt_loio INTO ls_loio WITH KEY objid = is_file_attachment-file_loio_guid.
 
IF sy-subrc EQ 0.
******Get the File Attachment Data in Binary******
  
CALL METHOD cl_crm_documents=>get_with_table
  
EXPORTING
     loio               
= ls_loio
  
IMPORTING
     file_access_info   
= lt_file_access
     file_content_ascii 
= lt_file_content_ascii
     file_content_binary
= lt_file_content_binary
     error              
= ls_error.

IF ls_error IS INITIAL.
   es_file_attachment
-iv_object       = is_file_attachment-iv_object.
   es_file_attachment
-tp_id           = is_file_attachment-tp_id.
   es_file_attachment
-filename        = is_file_attachment-filename.
   es_file_attachment
-file_loio_guid  = is_file_attachment-file_loio_guid.


********Read File MIME TYPE and File Size********
READ TABLE lt_file_access INTO ls_file_access INDEX 1.
IF sy-subrc EQ 0.
  es_file_attachment
-mime_type ls_file_access-mimetype.
  iv_length                   
= ls_file_access-file_size.
ENDIF.

********Read Attachment NAME and DESCRIPTION values********
READ TABLE lt_properties_result INTO ls_properties_result WITH KEY objtype = ls_loio-objtype
                                                                  
class         = ls_loio-class
                                                                   objid  
= ls_loio-objid.
IF sy-subrc EQ 0.


**********Read NAME**********
CLEAR ls_doc_property.
READ TABLE ls_properties_result-properties INTO ls_doc_property WITH KEY name = 'KW_RELATIVE_URL'.
IF sy-subrc EQ 0.
  es_file_attachment
-name = ls_doc_property-value.
ENDIF.


**********Read DESCRIPTION***********
CLEAR ls_doc_property.
READ TABLE ls_properties_result-properties INTO ls_doc_property WITH KEY name = 'DESCRIPTION'.
IF sy-subrc EQ 0.
  es_file_attachment
-description = ls_doc_property-value.
ENDIF.


ENDIF.

IF lt_file_content_binary IS INITIAL.

******If file attachment format is .TXT then Convert ASCII to BINARY*****
CALL FUNCTION 'SCMS_TEXT_TO_BINARY'
IMPORTING
   output_length
= iv_length
TABLES
   text_tab     
= lt_file_content_ascii
   binary_tab   
= lt_file_content_binary
EXCEPTIONS
   failed       
= 1
  
OTHERS              = 2.


  
IF sy-subrc <> 0.
 
**Implement suitable error handling here
  
ENDIF.

ENDIF.


******Convert Binary Data to XSTRING********
CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
EXPORTING
   input_length
= iv_length
IMPORTING
  
buffer                  = es_file_attachment-file_value
TABLES
   binary_tab  
= lt_file_content_binary
EXCEPTIONS
   failed      
= 1
  
OTHERS            = 2.


  
IF sy-subrc <> 0.
***Implement suitable error handling here
  
ENDIF.

  ENDIF.

ENDIF.
************

IF es_file_attachment IS NOT INITIAL.
  
******Move the File Type(MIME TYPE) value to the final work area********
   ls_stream
-mime_type = es_file_attachment-mime_type.


  
******Move the File Content in XSTRING to the final work area**********
   ls_stream
-value     = es_file_attachment-file_value.

  
*******Fill the File Header Information to display the actual file name while downloading the file attachment********
   ls_header
-name = 'Content-Disposition'.
   ls_header
-value  = 'inline; filename="'.

  
CONCATENATE ls_header-value  ls_file_attach-filename '"' INTO ls_header-value.

   set_header
( is_header = ls_header ).
ENDIF.

CALL METHOD me->copy_data_to_ref
EXPORTING
   is_data
= ls_stream
CHANGING
   cr_data
= er_stream.

ENDMETHOD.


Once the GET_STREAM method redefines is done then we can test the service using the REST Client to Read/Download the file attachment from CRM WebUI for Territory Plan using OData Service.

To test the service for Reading/Downloading the corresponding file from CRM WebUI call the service in chrome Browser with all key field parameter values with $value.

https://<Host:Port>/sap/opu/odata/sap/ZSN_DM_TP_SRV/TerritoryFileAttachmentSet(TP_ID='CRM-XN14-CRM-1...


3 Comments