Skip to Content
Author's profile photo Former Member

File Upload/Download in CRM WebUI Using Net weaver Gateway/OData Services

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

/wp-content/uploads/2014/11/attach1_583205.png

And the properties of Entity Type are:

/wp-content/uploads/2014/11/attach2_583206.png

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

/wp-content/uploads/2014/11/attach3_583207.png

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

/wp-content/uploads/2014/11/attach4_583211.png

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_attachtp_id
                       ls_file_attach
filename
                       ls_file_attach
name
                       ls_file_attach
description.

****File Type(MIME TYPE)****
ls_file_attach
mime_type  = is_media_resourcemime_type.


****File Content in XSTRING.*****
ls_file_attach
file_value = is_media_resourcevalue.

****Convert the Territory Plan ID into GUID****
CALL FUNCTION ‘CONVERSION_EXIT_CGPLP_INPUT’
EXPORTING
   
input  = ls_file_attachtp_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_attachname.
APPEND ls_properties TO lt_properties.
CLEAR ls_properties.

ls_propertiesname  = ‘DESCRIPTION’. “DESCRIPTION
ls_properties
value = ls_file_attachdescription.
APPEND ls_properties TO lt_properties.
CLEAR ls_properties.

ls_propertiesname  = ‘MIMETYPE’. “MIME TYPE
ls_properties
value = ls_file_attachmime_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_attachfile_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_attachfilename.
ls_file_access
mimetype   = ls_file_attachmime_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_loioobjid.
ELSE.
  ls_messages
id                   = ls_errorid.
  ls_messages
number           = ls_errorno.
  ls_messages
type                = ls_errortype .
  ls_messages
message_v1 = ls_errorv1 .
  ls_messages
message_v2 = ls_errorv2 .
  ls_messages
message_v3 = ls_errorv3 .
  ls_messages
message_v4 = ls_errorv4 .
 
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_attachtp_id.
APPEND ls_key TO lt_keys.

ls_keyname  = ‘FILENAME’.
ls_key
value    = ls_file_attachfilename.
APPEND ls_key TO lt_keys.

ls_keyname  = ‘IV_OBJECT’.
ls_key
value    = ‘FILEATTACH’.
APPEND ls_key TO lt_keys.

ls_keyname  = ‘FILE_LOIO_GUID’.
ls_key
value    = ls_file_attachfile_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.

/wp-content/uploads/2014/11/attach5_583212.png

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

/wp-content/uploads/2014/11/attach6_583213.png

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.

/wp-content/uploads/2014/11/attach7_583220.png

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 sysubrc = 0.
  is_file_attachment
tp_id = ls_key_tabvalue.
ENDIF.

CLEAR ls_key_tab.
READ TABLE it_key_tab INTO ls_key_tab WITH KEY name = ‘FILENAME’.
IF sysubrc = 0.
  is_file_attachment
filename = ls_key_tabvalue.
ENDIF.

CLEAR ls_key_tab.
READ TABLE it_key_tab INTO ls_key_tab WITH KEY name = ‘IV_OBJECT’.
IF sysubrc = 0.
  is_file_attachment
iv_object = ls_key_tabvalue.
ENDIF.

CLEAR ls_key_tab.
READ TABLE it_key_tab INTO ls_key_tab WITH KEY name = ‘FILE_LOIO_GUID’.
IF sysubrc = 0.
  is_file_attachment
file_loio_guid = ls_key_tabvalue.
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_attachmenttp_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_attachmentfile_loio_guid.
 
IF sysubrc 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_attachmentiv_object.
   es_file_attachment
tp_id           = is_file_attachmenttp_id.
   es_file_attachment
filename        = is_file_attachmentfilename.
   es_file_attachment
file_loio_guid  = is_file_attachmentfile_loio_guid.


********Read File MIME TYPE and File Size********
READ TABLE lt_file_access INTO ls_file_access INDEX 1.
IF sysubrc EQ 0.
  es_file_attachment
mime_type ls_file_accessmimetype.
  iv_length                   
= ls_file_accessfile_size.
ENDIF.

********Read Attachment NAME and DESCRIPTION values********
READ TABLE lt_properties_result INTO ls_properties_result WITH KEY objtype = ls_loioobjtype
                                                                  
class         = ls_loioclass
                                                                   objid  
= ls_loioobjid.
IF sysubrc EQ 0.


**********Read NAME**********
CLEAR ls_doc_property.
READ TABLE ls_properties_resultproperties INTO ls_doc_property WITH KEY name = ‘KW_RELATIVE_URL’.
IF sysubrc EQ 0.
  es_file_attachment
name = ls_doc_propertyvalue.
ENDIF.


**********Read DESCRIPTION***********
CLEAR ls_doc_property.
READ TABLE ls_properties_resultproperties INTO ls_doc_property WITH KEY name = ‘DESCRIPTION’.
IF sysubrc EQ 0.
  es_file_attachment
description = ls_doc_propertyvalue.
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 sysubrc <> 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_attachmentfile_value
TABLES
   binary_tab  
= lt_file_content_binary
EXCEPTIONS
   failed      
= 1
  
OTHERS            = 2.


  
IF sysubrc <> 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_attachmentmime_type.


  
******Move the File Content in XSTRING to the final work area**********
   ls_stream
value     = es_file_attachmentfile_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_headervalue  ls_file_attachfilename ‘”‘ INTO ls_headervalue.

   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-1292′,FILENAME=’roadmap.docx’,IV_OBJECT=”,FILE_LOIO_GUID=’005056A501651ED48FB13A5BB66964C9′)/$value

/wp-content/uploads/2014/11/attach8_583221.png


Assigned Tags

      3 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Sanju Joseph
      Sanju Joseph

      I was looking for a feature like that in Odata service. Excellent blog.

      Author's profile photo Wolfgang Mayer
      Wolfgang Mayer

      Hello Ramprasad,

      where did you take the screenshot where's a box labelled "drag & drop a file here"?

      Regards

      Wolfgang

      Author's profile photo Former Member
      Former Member
      Blog Post Author

      Hi Wolfgang Mayer,

      In Google App if we download the DHC Plugin then in that DHC RestClient Plugin we can see the "Drag&Drop a File Here" Option.

      Please let me know if you need more information. Thank you.

      Regards,

      Ramprasad B.