- Add Custom XML Parts to PowerPoint using ABAP
- Download the PowerPoint from CRM Web UI
- Download the PowerPoint from backend SAP GUI
Add Custom XML Parts to PowerPoint using ABAP
- Prepare XML data
- Get the PowerPoint template
- Add Custom XML to PowerPoint template
- Integrate the above steps - XML, PowerPoint template, Custom XML
1. Prepare XML data
* Exporting EV_CUSTOMXML4PPT TYPE XSTRING
* Exception XSLT_ERROR
DATA:
lv_customxml4ppt TYPE string,
l_convout TYPE REF TO cl_abap_conv_out_ce,
lv_len TYPE i.
EXPORTING
data = lv_customxml4ppt
IMPORTING
buffer = ev_customxml4ppt
len = lv_len ).
ENDMETHOD.
2. Get the PowerPoint template
* Exporting EV_CONTENT TYPE XSTRING
lv_content TYPE xstring.
lo_mr_api->get(
EXPORTING
i_url = ppt_template_url "Give the template url in MIME repository here
IMPORTING
e_content = ev_content
EXCEPTIONS
OTHERS = 1
).
ENDMETHOD.
3. Add Custom XML to PowerPoint template
presentationpart TYPE REF TO cl_pptx_presentationpart,
l_packagecontent TYPE string,
customxmlpartcoll TYPE REF TO cl_openxml_partcollection,
customxmlpart TYPE REF TO cl_oxml_customxmlpart,
customxmlpropspart TYPE REF TOTO cl_oxml_customxmlpropspart,
propertyxml TYPE xstring,
preguid TYPE string,
guid TYPE string.
ppt = cl_pptx_document=>load_document( iv_data = iv_ppt ).
l_packagecontent = ppt->get_content_type( ).
presentationpart = ppt->get_presentationpart( ).
* get collection of customXML parts
customxmlpartcoll = presentationpart->get_customxmlparts( ).
* create a customXML part here
customxmlpart = presentationpart->add_customxmlpart( ).
* insert xml data
customxmlpart->feed_data( iv_customxml ).
* add customXML properties part
customxmlpropspart = customxmlpart->add_customxmlpropspart( ).
* create GUID string
preguid = cl_openxml_helper=>create_guid_string( ).
* enclose with {...} brackets
CONCATENATE '{' preguid '}' INTO guid.
* create custom XML property content
CALL TRANSFORMATION docx_create_custompropscontent
PARAMETERS guid = guid
SOURCE XML iv_customxml
RESULT XML propertyxml.
* insert propertyxml
customxmlpropspart->feed_data( propertyxml ).
ev_ppt = ppt->get_package_data( ).
CATCH cx_openxml_not_allowed.
CATCH cx_openxml_not_found.
CATCH cx_transformation_error.
RAISE xslt_error.
ENDTRY.
ENDMETHOD.
4. Integrate the above steps - XML, PowerPoint template, Custom XML
* Exception ERROR_OCCURRED
lv_ppt_template TYPE xstring.
IMPORTING
ev_customxml4ppt = lv_customxml4ppt
EXCEPTIONS
xslt_error = 1
OTHERS = 2
).
IF sy-subrc = 1.
RAISE error_occurred.
ELSEIF sy-subrc = 2.
RAISE error_occurred.
ENDIF.
IMPORTING
ev_content = lv_ppt_template
).
add_customxml2ppt(
EXPORTING
iv_ppt = lv_ppt_template
iv_customxml = lv_customxml4ppt
IMPORTING
ev_ppt = ev_xml_xstring_ppt
EXCEPTIONS
xslt_error = 1
OTHERS = 2
).
IF sy-subrc <> 0.
RAISE error_occurred.
ENDIF.
ENDMETHOD.
Download the PowerPoint from CRM Web UI
- Create a BSP Controller "downloadPowerPoint" for PowerPoint.
- Create a Controller class "ZCL_CRM_DOWNLOAD_POWERPOINT" with superclass CL_BSP_CONTROLLER2. Redefine the DO_REQUEST method with the below code.
lv_len TYPE i.
IMPORTING
ev_xml_xstring_ppt = lv_ppt
EXCEPTIONS
error_occurred = 1
).
lv_len = xstrlen( lv_ppt ).
CALL METHOD response->if_http_entity~append_data
EXPORTING
data = lv_ppt
length = lv_len.
CALL METHOD response->if_http_entity~set_header_field
EXPORTING
name = 'content-type' "#EC NOTEXT
value = 'application/vnd.openxmlformats-officedocument.presentationml.presentation'.
ENDMETHOD.
Download the PowerPoint from backend SAP GUI
DATA:
lv_path TYPE string,
lv_fullpath TYPE string,
lo_zip TYPE REF TO cl_abap_zip,
lv_zip_content TYPE xstring,
lv_bin_filesize TYPE i,
lt_bin_tab TYPE STANDARD TABLE OF x255,
lv_ppt TYPE xstring,
l_pptgen_api TYPE REF TO zcl_crm_download_powerpoint.
l_pptgen_api = zcl_crm_download_powerpoint=>get_api( ).
cl_gui_frontend_services=>directory_browse(
EXPORTING
window_title = 'Select the folder to save the generated powerpoints' ##no_text
initial_folder = lv_path
CHANGING
selected_folder = lv_path
EXCEPTIONS
OTHERS = 1
).
CONCATENATE lv_path '\filename.pptx'INTO lv_fullpath.
CALL METHOD l_pptgen_api->get_ppt_download(
IMPORTING
ev_xml_xstring_ppt = lv_ppt
EXCEPTIONS
error_occurred = 1
).
CREATE OBJECT lo_zip.
lo_zip->load(
EXPORTING
zip = lv_ppt
EXCEPTIONS
OTHERS = 1
).
lv_zip_content = lo_zip->save( ).
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = lv_zip_content
IMPORTING
output_length = lv_bin_filesize
TABLES
binary_tab = lt_bin_tab.
IF lv_fullpath IS NOT INITIAL.
cl_gui_frontend_services=>gui_download(
EXPORTING
bin_filesize = lv_bin_filesize
filename = lv_fullpath
filetype = 'BIN'
CHANGING
data_tab = lt_bin_tab
EXCEPTIONS
OTHERS = 1
).
IF sy-subrc <> 0.
"error handling
ENDIF.
ENDIF.