Skip to Content
Author's profile photo Ramakrishnappa Gangappa

Attach files to GOS with Save, Retrieve, Delete functionality in SAP Web Dynpro ABAP – Part 1

Purpose:

Application for attaching files to GOS with save, retrieve & delete functionality in Webdynpro ABAP

Scenario:

I would like to explain the functionality of how to save,retrieve & delete files / Notes  during attachments to GOS in Webdynpro ABAP application.

Please refer the below link for more info on GOS

GOS ( Generic Object Services )

Here I would consider the scenario of SAP FSRI module, and attach files / notes against a policy number.

Class: CL_GOS_API

Provides access to the GOS attachment list for applications. In the first step, the application must create an instance of the class CL_GOS_API and then register its unique application object at the same time. Currently, application objects of the categories BOR Object and Persistent Classes are supported. The attachment list can then be read, and the content of individual attachments can be read, modified, deleted, or created.

Pre-requisite:

Basic knowledge of Webdynpro ABAP,& OO ABAP

Step by Step Process:

Step 1:

Go t-code SE11 and create a custom structure YST_GOS_ATTACH_LIST as below

i.e. add a field ICON and include structure GOS_S_ATTA as below

str1.PNG

str2.PNG

Step 2:

Go to t-code SE80 and create a web dynpro component as below

1.PNG

Step 3:

Go to context tab of component controller and create a node ATTACHMENTS by using structure YST_GOS_ATTACH_LIST and set the properties as below

2_1.PNG

Step 4:

Create another node “ATTACHMENT_CONTENTS” by using structure GOS_S_ATTCONT as below

2_2.PNG

Step 5:

Go to attributes tab of component controller and create the global attributes GO_GOS & GV_USER_ACTION as below

2_3.PNG

Step 5A:

Create methods in component controller as below

2_4.PNG

Now, the write the code as below

Method: DO_ON_CREATE

com_DO_ON_CREATE.PNG

   

DO_ON_CREATE

METHOD do_on_create .

  ” Set user action
  “======================================
  wd_this->gv_user_action = 1.”Create

  ” Refresh attachment list ctx
  “=============================================
  DATA lo_node TYPE REF TO if_wd_context_node.

  lo_node = wd_context->get_child_node(
        name = wd_this->wdctx_attachment_contents ).

  lo_node->invalidate( ).

  ” Show respective popup window
  “=============================
  wd_this->show_popup(
    EXPORTING
      io_view_ctrl   = io_view_ctrl
      id_button_kind = 3
      id_action1 = ‘SAVE’
      id_window_name = id_window_name
  ).

ENDMETHOD.

Method: DO_SAVE

DO_SAVE

METHOD do_save .

  DATA: ls_attacont   TYPE gos_s_attcont,
        ls_atta_key   TYPE gos_s_attkey,
        lv_roltype    TYPE oblroltype,
        lv_commit     TYPE flag,
        lo_node       TYPE REF TO if_wd_context_node,
        lo_element    TYPE REF TO if_wd_context_element.

  ” Read notes detail context
  “====================================
  lo_node = wd_context->get_child_node(
        name       = wd_this->wdctx_attachment_contents
  ).

  lo_node->get_element( )->get_static_attributes(
  IMPORTING static_attributes = ls_attacont ).
  ” Attach notes
  “=======================================
  TRY.

      ls_attacont-atta_cat = cl_gos_api=>c_msg.
      ” Check if attachment documents
      IF ls_attacont-content_x IS INITIAL.
        ls_attacont-tech_type = ‘TXT’.
        lv_roltype = cl_gos_api=>c_annotation.
      ELSE.
        lv_roltype = cl_gos_api=>c_attachment.
      ENDIF.

      IF wd_this->gv_user_action EQ 1.”Create

        wd_this->go_gos->insert_al_item(
          EXPORTING
            is_attcont =   ls_attacont
            iv_roltype =  lv_roltype
      RECEIVING
        rv_commit  = lv_commit
        ).

      ELSEIF wd_this->gv_user_action EQ 2.”Change
        lv_commit =
         wd_this->go_gos->update_al_item( is_attcont = ls_attacont ).
      ELSE.”delete

        MOVE-CORRESPONDING ls_attacont TO ls_atta_key.

        lv_commit =
         wd_this->go_gos->delete_al_item( is_atta_key = ls_atta_key ).
*          CATCH cx_gos_api.  ” GOS API: Exception
      ENDIF.

    CATCH cx_gos_api.                                   “#EC NO_HANDLER
  ENDTRY.

  IF lv_commit EQ abap_true.
    CALL FUNCTION ‘BAPI_TRANSACTION_COMMIT’
      EXPORTING
        wait = abap_true.
  ENDIF.

  “===============================
  ” Reload the context
  “===============================
  wd_this->load_attachment_list( ).
ENDMETHOD.

Method: LOAD_ATTACHMENT_CONTENTS

comp_LOAD_ATTACHMENT_CONTENTS.PNG

LOAD_ATTACHMENT_CONTENTS

METHOD load_attachment_contents .

  DATA: ls_atta_key     TYPE gos_s_attkey,
        ls_attacont     TYPE gos_s_attcont,
        lv_window_name  TYPE string,
        lv_title        TYPE string,
        lv_text         TYPE string,
        lv_button_kind  TYPE wdr_popup_button_kind.
  ” Prepare key
  MOVE-CORRESPONDING is_atta TO ls_atta_key.

  “get attachment content
  TRY.
      ls_attacont =
        wd_this->go_gos->get_al_item( is_atta_key = ls_atta_key ).
    CATCH cx_gos_api.                                   “#EC NO_HANDLER
  ENDTRY.

  “set data to context
  DATA lo_node    TYPE REF TO if_wd_context_node.
  DATA lo_element TYPE REF TO if_wd_context_element.

  lo_node = wd_context->get_child_node(
            name = wd_this->wdctx_attachment_contents ).

  lo_element = lo_node->get_element( ).

  lo_element->set_static_attributes( static_attributes = ls_attacont ).

  ” Based on content, show popup window
  “============================
  CLEAR lv_button_kind.
  IF wd_this->gv_user_action EQ 2.
    lv_title = ‘Change’.
    lv_button_kind = 3.
  ELSEIF wd_this->gv_user_action EQ 3.
    lv_title = ‘Display’.
  ELSEIF wd_this->gv_user_action EQ 4.
    lv_title = ‘Delete’.
    lv_button_kind = 3.
  ENDIF.

  IF ls_attacont-content_x IS INITIAL.
    lv_window_name = ‘W_ATTACH_NOTES’.
    lv_text = ‘Notes’.
    CONCATENATE lv_title lv_text INTO lv_title SEPARATED BY space.
  ELSE.
    lv_window_name = ‘W_ATTACH_FILES’.
    lv_text = ‘File’.
    CONCATENATE lv_title lv_text INTO lv_title SEPARATED BY space.

  ENDIF.

  wd_this->show_popup(
    EXPORTING
      io_view_ctrl   = io_view_ctrl
      id_button_kind = lv_button_kind
      id_title = lv_title
      id_action1 = ‘SAVE’
      id_window_name = lv_window_name
  ).
ENDMETHOD.

Method: LOAD_ATTACHMENT_LIST

LOAD_ATTACHMENT_LIST

METHOD load_attachment_list .
  “===================================

  DATA: lt_atta               TYPE TABLE OF gos_s_atta,
        lt_attachment_list    TYPE wd_this->elements_attachments,
        ls_attachment_list    LIKE LINE OF lt_attachment_list.

  FIELD-SYMBOLS: <ls_atta> LIKE LINE OF lt_atta.

TRY.
      lt_atta =  wd_this->go_gos->get_atta_list( ).
    CATCH cx_gos_api.                                   “#EC NO_HANDLER
  ENDTRY.

  CLEAR lt_attachment_list.

  LOOP AT lt_atta ASSIGNING <ls_atta>.
    CLEAR ls_attachment_list.
    MOVE-CORRESPONDING <ls_atta> TO ls_attachment_list.

    CONDENSE ls_attachment_list-tech_type.

    IF ls_attachment_list-tech_type EQ ‘TXT’ OR
       ls_attachment_list-tech_type IS INITIAL.
      ls_attachment_list-icon = ‘ICON_ANNOTATION’.
    ELSE.
      ls_attachment_list-icon = ‘ICON_ATTACHMENT’.
    ENDIF.
    APPEND ls_attachment_list TO lt_attachment_list.
  ENDLOOP.

  “===========================
  ” Set data to context node
  “===========================
  DATA lo_node TYPE REF TO if_wd_context_node.

  lo_node = wd_context->get_child_node(
      name       = wd_this->wdctx_attachments
  ).

  lo_node->bind_table(
    EXPORTING
      new_items            =  lt_attachment_list
  ).

ENDMETHOD.

Method: SHOW_POPUP

com_show_popup.PNG

SHOW_POPUP

method SHOW_POPUP .
  “Purpose: Display popup window of More CoB
  “————————————————
  “Data declarations
  “————————————————
  DATA lo_window_manager TYPE REF TO if_wd_window_manager.
  DATA lo_api_component  TYPE REF TO if_wd_component.
  DATA lo_window         TYPE REF TO if_wd_window.
  DATA lv_title          TYPE string.

  “—————————————————-
  “get view contoller, window manager
  lo_api_component           = wd_this->wd_get_api( ).

  lo_window_manager          = lo_api_component->get_window_manager( ).
*  “—————————————————-
  lv_title = id_title.
  IF lv_title IS INITIAL.
    lv_title = ‘Create Attachments’.
  ENDIF.

  “creat a pop up window
  CALL METHOD lo_window_manager->create_window
    EXPORTING
*     modal                = ABAP_TRUE
      window_name          = id_window_name
      title                = lv_title
*     close_button         = ABAP_TRUE
      button_kind          = id_button_kind
*     message_type         = IF_WD_WINDOW=>CO_MSG_TYPE_NONE
*     close_in_any_case    = ABAP_TRUE
*     message_display_mode =
*     default_button       =
      is_resizable         = abap_false
    RECEIVING
      window               = lo_window.

  IF lo_window IS BOUND.
    IF id_button_kind EQ 3.

      IF id_action1 IS NOT INITIAL.
        CALL METHOD lo_window->subscribe_to_button_event
          EXPORTING
            button            = 4
            button_text       = id_action1_text
*           tooltip           =
            action_name       = id_action1
            action_view       = io_view_ctrl
            is_default_button = abap_true.
      ENDIF.

      IF id_action2 IS NOT INITIAL.
        CALL METHOD lo_window->subscribe_to_button_event
          EXPORTING
            button            = 6
*           button_text       =
*           tooltip           =
            action_name       = id_action2
            action_view       = io_view_ctrl
            is_default_button = abap_false.
      ENDIF.
    ENDIF.
    lo_window->open( ).
  ENDIF.
  “—————————————————-
  “————————————————
endmethod.

Step 6:

Go to view V_MAIN and create context attribute  POLICY_ID under node CONTEXT as below

3_1.PNG

Step 7:

Map the context node ATTACHMENTS from component controller to V_MAIN view’s controller as below

3_1_1.PNG

Step 8:

Create an input field and bind the value to context attribute POLICY_ID as below

3_2.PNG

Step 9:

Create a button “BTN_GET_LIST” and create an action “GET_LIST” and bind it to the event of button as below

3_3.PNG

Step 9 A:

Write the below code in event handler method ONACTIONGET_LIST as below

Here, we are creating an instance to GOS for the given policy number.

ONACTIONGET_LIST

METHOD onactionget_list .
  ” Data declarations
  DATA lv_pol_id  TYPE /msg/h_pol_id.

  “==========================
  “read policy number
  “==========================
  wd_context->get_attribute(
    EXPORTING
      name  =  ‘POLICY_ID’
    IMPORTING
      value = lv_pol_id
  ).

  “============================
  “Create an instance of GOS
  “============================
  DATA ls_object TYPE gos_s_obj.

  CLEAR ls_object.
  ls_object-instid = lv_pol_id. ” Policy Number
  ls_object-typeid = ‘/MSG/HRISK’.
  ls_object-catid = ‘BO’.

  TRY.
      wd_comp_controller->go_gos =
      cl_gos_api=>create_instance( is_object = ls_object ).
    CATCH cx_gos_api.                                   “#EC NO_HANDLER
  ENDTRY.

  “============================
  “Load attachment list
  “============================
  wd_comp_controller->load_attachment_list( ).
ENDMETHOD.

Step 10:

Create a table ui element TBL and bind the data source property to node ATTACHMENTS as below

3_4.PNG

Step 11:

Create the columns and bind to the context attributes as shown below

3_5.PNG

Step 12:

Create an extra column called “ACTIONS” for display, change, delete actions with SelectedCellVariant “KEY” as below

3_5_1.PNG

Step 13:

Right click on column ACTIONS and choose “Insert Cell Variant” as below

/wp-content/uploads/2014/03/3_5_2_408212.png

Step 14:

Set the variantKey “KEY” to TableMultiCellEditor as shown below

3_5_3.PNG

Step 15 :

Create a button for display and bind it to the action DO_ACTION as below

3_5_4.PNG

Step 16 :

Create a button for change and bind it to the action DO_ACTION as below

3_5_5.PNG

Step 17 :

Create a button for delete and bind it to the action DO_ACTION  as below

3_5_6.PNG

Step 17A:

Write the below code in event handler method ONACTIONDO_ACTION

v_main_do_action.PNG

ONACTIONDO_ACTION

METHOD onactiondo_action .
  DATA ls_attachments TYPE wd_this->element_attachments.
  DATA lo_view_ctrl   TYPE REF TO if_wd_view_controller.

  CASE id.
    WHEN ‘BTN_CHANGE’.
      wd_comp_controller->gv_user_action = 2.”Change
    WHEN ‘BTN_DISPLAY’.
      wd_comp_controller->gv_user_action = 3.”Display
    WHEN ‘BTN_DELETE’.
      wd_comp_controller->gv_user_action = 4.”Delete
    WHEN OTHERS.
  ENDCASE.

  “get the current row data
  CALL METHOD context_element->get_static_attributes
    IMPORTING
      static_attributes = ls_attachments.

  “=========================
  “Load attachment content
  “=========================
  lo_view_ctrl ?= wd_this->wd_get_api( ).

  CALL METHOD wd_comp_controller->load_attachment_contents
    EXPORTING
      is_atta      = ls_attachments
      io_view_ctrl = lo_view_ctrl.
ENDMETHOD.

Step 17B:

Go to actions tab of view V_MAIN and create an action SAVE for save action on popup window for attaching notes or files. i.e. we have subscribed the button “OK” of popup window to SAVE action on view V_MAIN

Write the below code in event handler method ONACTIONSAVE.

ONACTIONSAVE

” Save attachements

wd_comp_controller->do_save( ).




Continued……

Attach files to GOS with Save, Retrieve, Delete functionality in SAP Web Dynpro ABAP – Part 2

Assigned Tags

      10 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Hello

      Can you add the implementation code for method LOAD_ATTACHMENT_LIST ?

      And also code to implement for events DO_ACTION and GET_LIST ?

      Thx

      Xavier

      Author's profile photo Ramakrishnappa Gangappa
      Ramakrishnappa Gangappa
      Blog Post Author

      Hi Xavier,

      Thanks a lot for going through the document in details  🙂 ... I appreciate your findings.. yah! missed to include the implementation of those methods 😕 ..

      I have added the implementation code of those methods 🙂

      Regards,

      Rama

      Author's profile photo Former Member
      Former Member

      We need to find out the  minimum Netweaver Enhancement Pack level that  is required for CL_GOS_API - so we can get this to work in our webdynpro development. 

      Author's profile photo Ramakrishnappa Gangappa
      Ramakrishnappa Gangappa
      Blog Post Author

      Hi,

      As for as I know, the class CL_GOS_API is available from NW 7.31.

      Author's profile photo Former Member
      Former Member

      Hi,

      Class CL_GOS_API and structure GOS_S_ATTA does not exist in our system ECC 6.0,.

      What to do? do we need to activate object or patch update?

      Author's profile photo Ramakrishnappa Gangappa
      Ramakrishnappa Gangappa
      Blog Post Author

      Hi John,

      AFAIK,the class CL_GOS_API is available from NW 7.31. I think you might need to upgrade your system to EHP6.

      Author's profile photo Former Member
      Former Member

      Dear Ramakrishnappa Gangappa,

                        Thanks for u r Sharing the Good Document regarding GOS In WebDynpro. I'm Getting the all that u r shared but after upload then i want to see the attachments like(PDF,PNG,JPEG,doc..) files are converted as a raw string data type data, I'm unable to get the exact output that i have uploaded. while click on download button it's path changed like C:/FAKEPATH/KOLA.jpg. Please help me regarding this.

      Thanks & Regards,

      Suresh Reddy.

      Author's profile photo Ramakrishnappa Gangappa
      Ramakrishnappa Gangappa
      Blog Post Author

      Hi Suresh,

      Thank you for your feedback.

      Please have look at the below thread:

      File path defaulted to 'C:\fakepath' when attaching

      Hope this helps you.

      Regards,

      Rama

      Author's profile photo Former Member
      Former Member

      Dear Ramakrishnapppa,

                     Thanks for your feedback i'm getting the exact output in internet Explorer Browser with your Suggestion but in the google chrome browser and also it's work for .PDF Files. Suppose if i upload the file formats like .PNG,.DOC,.JPEG and Excel then it woks like Pdf file viewing on the Browser. If is not possible what is alternative solution for this.

      Thanks & Regards,

      Suresh Reddy.

      Author's profile photo Former Member
      Former Member

      Hi

      I am trying to do the same, But i am getting the Error:

      500 SAP Internal Server Error

      ERROR: Could not find attribute ATTACHMENTS (termination: RABAX_STATE)

      please help me, I am new for WD.