Attachments of different types of files Using Webdynpro ABAP
How to attach files
through Web Dynpro Application ?
Steps :
O/P:
To get above Output we need to follow below steps:
Table Required as Below:
Create a web dynpro application as below:
1.
Create a web dnpro application with one main
view.
Go to CONTEXT node of
Main View and Assign 2 nodes as
below:
Cardinality of nodes as follows :
Go to Layout Tab
To Achieve the below :
We need to create UI Elements as below
Text View Properties:
File Upload properties:
Attach Button Properties:
Source Code for On Action ( ATTACH ) :
method onactionattach .
data lo_nd_input type ref to if_wd_context_node.
data lo_el_input type ref to if_wd_context_element.
data ls_input type wd_this->element_input.
data lo_nd_attachment_list type ref to if_wd_context_node.
data lt_attachment_list type wd_this->elements_attachments.
data ls_attachment_list like line of lt_attachment_list.
data lv_temp type string.
*&==========================================================
*& Read the details of file chosen for attachment
*&==========================================================
lo_nd_input = wd_context->get_child_node( name = wd_this->wdctx_input ).
* get element via lead selection
lo_el_input = lo_nd_input->get_element( ).
* get all declared attributes
lo_el_input->get_static_attributes(
importing
static_attributes = ls_input ).
" return if nothing to do
if ls_input-file_data is initial.
" Notify the user, please choose a file
return.
endif.
*&==========================================================
*& Read the attachment list
*&==========================================================
* navigate from <CONTEXT> to <ATTACHMENT_LIST> via lead selection
lo_nd_attachment_list =
wd_context->get_child_node( name = wd_this->wdctx_attachments ).
lo_nd_attachment_list->get_static_attributes_table(
importing table = lt_attachment_list ).
*&==========================================================
*& Prepare data to save
*&==========================================================
clear ls_attachment_list.
ls_attachment_list-mandt = sy-mandt.
ls_attachment_list-uname = sy-uname.
" Generate Unique identifier
try.
ls_attachment_list-guid = cl_system_uuid=>create_uuid_c22_static( ).
catch cx_uuid_error. " Error Class for UUID Processing Errors.
endtry.
ls_attachment_list-file_data = ls_input-file_data.
*& Get file name from path
while ls_input-file_name ca '\'.
split ls_input-file_name at '\'
into lv_temp ls_input-file_name.
endwhile.
ls_attachment_list-file_name = ls_input-file_name.
ls_attachment_list-file_type = ls_input-file_type.
ls_attachment_list-erdat = sy-datum.
ls_attachment_list-erzet = sy-timlo.
ls_attachment_list-status = abap_false."attachment is new
append ls_attachment_list to lt_attachment_list.
"==========================================================
" Bind data to context
"==========================================================
lo_nd_attachment_list->bind_table(
exporting
new_items = lt_attachment_list ).
endmethod.
data lo_nd_input type ref to if_wd_context_node.
data lo_el_input type ref to if_wd_context_element.
data ls_input type wd_this->element_input.
data lo_nd_attachment_list type ref to if_wd_context_node.
data lt_attachment_list type wd_this->elements_attachments.
data ls_attachment_list like line of lt_attachment_list.
data lv_temp type string.
*&==========================================================
*& Read the details of file chosen for attachment
*&==========================================================
lo_nd_input = wd_context->get_child_node( name = wd_this->wdctx_input ).
* get element via lead selection
lo_el_input = lo_nd_input->get_element( ).
* get all declared attributes
lo_el_input->get_static_attributes(
importing
static_attributes = ls_input ).
" return if nothing to do
if ls_input-file_data is initial.
" Notify the user, please choose a file
return.
endif.
*&==========================================================
*& Read the attachment list
*&==========================================================
* navigate from <CONTEXT> to <ATTACHMENT_LIST> via lead selection
lo_nd_attachment_list =
wd_context->get_child_node( name = wd_this->wdctx_attachments ).
lo_nd_attachment_list->get_static_attributes_table(
importing table = lt_attachment_list ).
*&==========================================================
*& Prepare data to save
*&==========================================================
clear ls_attachment_list.
ls_attachment_list-mandt = sy-mandt.
ls_attachment_list-uname = sy-uname.
" Generate Unique identifier
try.
ls_attachment_list-guid = cl_system_uuid=>create_uuid_c22_static( ).
catch cx_uuid_error. " Error Class for UUID Processing Errors.
endtry.
ls_attachment_list-file_data = ls_input-file_data.
*& Get file name from path
while ls_input-file_name ca '\'.
split ls_input-file_name at '\'
into lv_temp ls_input-file_name.
endwhile.
ls_attachment_list-file_name = ls_input-file_name.
ls_attachment_list-file_type = ls_input-file_type.
ls_attachment_list-erdat = sy-datum.
ls_attachment_list-erzet = sy-timlo.
ls_attachment_list-status = abap_false."attachment is new
append ls_attachment_list to lt_attachment_list.
"==========================================================
" Bind data to context
"==========================================================
lo_nd_attachment_list->bind_table(
exporting
new_items = lt_attachment_list ).
endmethod.
To Achieve Below Portion :
We need to Define UI Elements as Below:
Define Transparent Container TC2 -Ã Create a Table Element-Ã Choose Create Binding
And set Attachment
Fields as below :
File Name Properties :
File Type Properties:
Created On Properties:
Time Properties:
Is File Saved or not
Properties:
Create Toolbar from Table Option:
With Save and Delete Buttons
Source Code:
On Action Save :
method onactionsave .
data lo_nd_attachment_list type ref to if_wd_context_node.
data lt_attachment_list type wd_this->elements_attachments.
data lo_msg_manager type ref to if_wd_message_manager.
field-symbols <fs_attachment_list> like line of lt_attachment_list.
lo_nd_attachment_list =
wd_context->get_child_node( name = wd_this->wdctx_attachments ).
lo_nd_attachment_list->get_static_attributes_table(
importing table = lt_attachment_list ).
data lt_data type table of zattachments.
data ls_data like line of lt_data.
"==========================================================
"Loop over attachment list for only new attachments
"==========================================================
loop at lt_attachment_list assigning <fs_attachment_list>
where status = abap_false." new attachment
" set status
<fs_attachment_list>-status = abap_true.
clear ls_data.
move-corresponding <fs_attachment_list> to ls_data.
append ls_data to lt_data.
endloop.
"get message manager
lo_msg_manager =
wd_comp_controller->wd_get_api( )->get_message_manager( ).
if lt_data[] is initial.
" Attachement list is already saved
lo_msg_manager->report_error_message(
message_text = 'Attachement list is already saved' ).
return.
endif.
"==========================================================
" Save attachments
"==========================================================
modify zattachments from table lt_data.
if sy-subrc is initial.
" after successful save, update the status
lo_nd_attachment_list->bind_table(
exporting
new_items = lt_attachment_list
).
" Show success message
lo_msg_manager->report_success(
message_text = 'Attachement list saved successfully !!!' ).
endif.
endmethod.
data lo_nd_attachment_list type ref to if_wd_context_node.
data lt_attachment_list type wd_this->elements_attachments.
data lo_msg_manager type ref to if_wd_message_manager.
field-symbols <fs_attachment_list> like line of lt_attachment_list.
lo_nd_attachment_list =
wd_context->get_child_node( name = wd_this->wdctx_attachments ).
lo_nd_attachment_list->get_static_attributes_table(
importing table = lt_attachment_list ).
data lt_data type table of zattachments.
data ls_data like line of lt_data.
"==========================================================
"Loop over attachment list for only new attachments
"==========================================================
loop at lt_attachment_list assigning <fs_attachment_list>
where status = abap_false." new attachment
" set status
<fs_attachment_list>-status = abap_true.
clear ls_data.
move-corresponding <fs_attachment_list> to ls_data.
append ls_data to lt_data.
endloop.
"get message manager
lo_msg_manager =
wd_comp_controller->wd_get_api( )->get_message_manager( ).
if lt_data[] is initial.
" Attachement list is already saved
lo_msg_manager->report_error_message(
message_text = 'Attachement list is already saved' ).
return.
endif.
"==========================================================
" Save attachments
"==========================================================
modify zattachments from table lt_data.
if sy-subrc is initial.
" after successful save, update the status
lo_nd_attachment_list->bind_table(
exporting
new_items = lt_attachment_list
).
" Show success message
lo_msg_manager->report_success(
message_text = 'Attachement list saved successfully !!!' ).
endif.
endmethod.
On Action Delete :
method onactiondelete_attachment .
data lo_node type ref to if_wd_context_node.
data lt_elements type wdr_context_element_set.
data lo_element type ref to if_wd_context_element.
data ls_attach_list type wd_this->element_attachments.
data lo_msg_manager type ref to if_wd_message_manager.
"==========================================================
" Get the selected elements from the context
"==========================================================
lo_node =
wd_context->get_child_node( name = wd_this->wdctx_attachments ).
lt_elements =
lo_node->get_selected_elements( ).
if lt_elements[] is initial.
"report error message, Please choose a file to delete
lo_msg_manager =
wd_comp_controller->wd_get_api( )->get_message_manager( ).
lo_msg_manager->report_error_message(
message_text = 'Please select atleast 1 row' ).
return.
endif.
data lt_data type table of zattachments.
data ls_data like line of lt_data.
clear lt_data.
"==========================================================
" Delete the record from attachment list
"==========================================================
loop at lt_elements into lo_element.
lo_element->get_static_attributes(
importing
static_attributes = ls_attach_list
).
" delete from the context node
lo_node->remove_element( element = lo_element ).
"Check if record exists in table
if ls_attach_list-status = abap_true.
clear ls_data.
move-corresponding ls_attach_list to ls_data.
append ls_data to lt_data.
endif.
endloop.
"==========================================================
" Delete the record(s) from data base table
"==========================================================
delete zattachments from table lt_data.
endmethod.
data lo_node type ref to if_wd_context_node.
data lt_elements type wdr_context_element_set.
data lo_element type ref to if_wd_context_element.
data ls_attach_list type wd_this->element_attachments.
data lo_msg_manager type ref to if_wd_message_manager.
"==========================================================
" Get the selected elements from the context
"==========================================================
lo_node =
wd_context->get_child_node( name = wd_this->wdctx_attachments ).
lt_elements =
lo_node->get_selected_elements( ).
if lt_elements[] is initial.
"report error message, Please choose a file to delete
lo_msg_manager =
wd_comp_controller->wd_get_api( )->get_message_manager( ).
lo_msg_manager->report_error_message(
message_text = 'Please select atleast 1 row' ).
return.
endif.
data lt_data type table of zattachments.
data ls_data like line of lt_data.
clear lt_data.
"==========================================================
" Delete the record from attachment list
"==========================================================
loop at lt_elements into lo_element.
lo_element->get_static_attributes(
importing
static_attributes = ls_attach_list
).
" delete from the context node
lo_node->remove_element( element = lo_element ).
"Check if record exists in table
if ls_attach_list-status = abap_true.
clear ls_data.
move-corresponding ls_attach_list to ls_data.
append ls_data to lt_data.
endif.
endloop.
"==========================================================
" Delete the record(s) from data base table
"==========================================================
delete zattachments from table lt_data.
endmethod.
Create a application name for Webdynpro component and test
it.
Hi
ReplyDeleteThis is wonderful document, which helped me a lot and thanks for provide with screenshots..thanks a lot Naga