Quantcast
Channel: SCN : All Content - SAP CRM: Webclient UI - Framework
Viewing all articles
Browse latest Browse all 4552

How to: Add Custom XML Parts to Microsoft PowerPoint using ABAP, and download from CRM Web UI/backend SAP GUI

$
0
0
This document explains how to:

Add Custom XML Parts to PowerPoint using ABAP

  1. Prepare XML data
  2. Get the PowerPoint template
  3. Add Custom XML to PowerPoint template
  4. Integrate the above steps - XML, PowerPoint template, Custom XML

1. Prepare XML data

METHOD prepare_customxml.
*     Exporting  EV_CUSTOMXML4PPT  TYPE XSTRING
*     Exception  XSLT_ERROR
  CONSTANTS:  lc_encode        TYPE abap_encod VALUE 'UTF-8'.
  DATA:
         lv_customxml4ppt              TYPE string,
         l_convout                           TYPE REF TO cl_abap_conv_out_ce,
         lv_len                                 TYPE i.
  l_convout = cl_abap_conv_out_ce=>create( encoding = lc_encode ).
  lv_customxml4ppt = '<xmlPPT value="This is an example of valid xml" />'. "You can add a valid xml here
  l_convout->convert(
       EXPORTING
              data   = lv_customxml4ppt
       IMPORTING
              buffer = ev_customxml4ppt
              len    = lv_len ).

ENDMETHOD.

2. Get the PowerPoint template

               The template can be uploaded to the MIME repository and can be read from there.
METHOD get_ppt_template.
*     Exporting EV_CONTENT TYPE XSTRING
  DATA:
  lo_mr_api                  TYPE REF TO if_mr_api,
  lv_content                 TYPE xstring.
  lo_mr_api = cl_mime_repository_api=>get_api( ).
  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

METHOD add_customxml2ppt.
*     Importing      IV_PPT                     TYPE XSTRING
*     Importing      IV_CUSTOMXML     TYPE XSTRING
*     Exporting      EV_PPT                    TYPE XSTRING   
*     Exception      XSLT_ERROR
  DATA:
    ppt                               TYPE REF TO cl_pptx_document,
    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.
  TRY.
      ppt = cl_pptx_document=>load_document( iv_data = iv_ppt ).
      l_packagecontent = ppt->get_content_type( ).
* get the maindocument part
      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_format.
    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

METHOD get_ppt_download.
*      Exporting EV_XML_XSTRING_PPT      TYPE XSTRING
*      Exception ERROR_OCCURRED
  DATA:
              lv_customxml4ppt      TYPE xstring,
              lv_ppt_template          TYPE xstring.
  prepare_customxml(
    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.
  get_ppt_template(
    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

  1. Create a BSP Controller "downloadPowerPoint" for PowerPoint.
  2. Create a Controller class "ZCL_CRM_DOWNLOAD_POWERPOINT" with superclass CL_BSP_CONTROLLER2. Redefine the DO_REQUEST method with the below code.
METHOD do_request.
DATA:
lv_ppt           TYPE xstring.
lv_len            TYPE i.
 
get_ppt_download(
      IMPORTING
        ev_xml_xstring_ppt = lv_ppt
      EXCEPTIONS
        error_occurred     = 1
                             ).
  lv_len = xstrlen( lv_ppt ).
*   Export response data
CALL METHOD response->if_http_entity~append_data
    EXPORTING
      data   = lv_ppt
      length = lv_len.
*   Set response content-type as PowerPoint
  CALL METHOD response->if_http_entity~set_header_field
    EXPORTING
      name  = 'content-type'                                "#EC NOTEXT
"content-type for PPTX
      value = 'application/vnd.openxmlformats-officedocument.presentationml.presentation'.
"Content types
"PowerPoint (*.pptx) - 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
"PowerPoint Macro-Enabled (*.pptm) - 'application/vnd.ms-powerpoint.presentation.macroenabled.12'

ENDMETHOD.
             
   3.  Set the contoller class to the BSP Controller. Now you can use this controller to download the file.
PPT_BSPController.png

Download the PowerPoint from backend SAP GUI

     Create a Report Program with the following code and execute.

 

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.

 

             
The implementation for Microsoft Excel and Word would be documented soon.

Viewing all articles
Browse latest Browse all 4552

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>