cancel
Showing results for 
Search instead for 
Did you mean: 

How to Get Sap menu by RFC or Bapi or Programe

new163wang
Explorer
0 Kudos
183

Dear all,help me:

    How to obtain the menu and transaction code of Sap based on the login name, including the Favorites created by this account?

   QQ20240910-085510.png

new163wang
Explorer
0 Kudos

With the help of Sandra_Rossi :

new163wang_0-1726665040310.png

*&---------------------------------------------------------------------*
*& Report Name: My menu
*& Create Date:2024-09-11
*& Created By :KlaunWang S2303
*& Description:树形显示菜单(收藏夹)双击菜单执行Tcode
*& VERSION:v1.0
*& 参考:https://cloud.tencent.com/developer/article/2340940
*& Tree Demo by Sap:
*& NAVP_DEMO_TREE                 使用 CL_GUI_ALV_TREE 中的导航参数文件
*& NAVP_DEMO_TREE_COMPLEX         使用 CL_GUI_ALV_TREE(复杂示例)中到的导航参数文件
*& NAVP_DEMO_TREE_FORM_ROUTINES
*& NAVP_DEMO_TREE_TOOLBAR_EVT_RCV
*& SALV_DEMO_TREE_DATA_UPDATE     Demonstration Program for ALV OM Tree: Changed Data
*& SALV_DEMO_TREE_EVENTS          Demonstration Program for ALV OM Tree: Events
*& SALV_DEMO_TREE_FORM_EVENTS     Demonstration of Use of ALV Design Object
*& SALV_DEMO_TREE_FUNCTIONS       Demonstration Program for ALV OM Tree: Functions
*& SALV_DEMO_TREE_METADATA        Demonstration Program for ALV OM Tree: Metadata
*& SALV_DEMO_TREE_SELECTIONS      Demonstration Program for ALV OM Tree: Selections
*& SALV_DEMO_TREE_SETTINGS        Demonstration Program for ALV OM Tree: Settings
*& SALV_DEMO_TREE_SIMPLE          Demonstration Program for ALV OM Tree
*&---------------------------------------------------------------------*
*& records:
*&---------------------------------------------------------------------*
report zzh_private_menu.
typesbegin of ty_tree_node,
         id   type char18,
         text type char40,
       end of ty_tree_node.
types:begin of ty_data,
        menu_level type  menu_num_2"菜单中的级别
        parent_id  type  menu_num10"菜单 ID 的计数器
        object_id  type  menu_num10"菜单 ID 的计数器
        node_type  type  bxntype"接点名 ( U = URL, F = 文件夹, T = 事务)
        tcode      type  sytcode"当前事务代码
        text       type ssm_title,
        nkey       type lvc_nkey,
      end of ty_data.

data:lt_data        type table of ty_data,ls_data type ty_data,wa_data type ty_data,
     lt_tree        type table of ty_data,ls_tree type ty_data,
     lt_bxmnodes    type table of bxmnodes,ls_bxmnodes type bxmnodes,
     l_node_text    type lvc_value,
     ls_sflight     type sflight,
     ls_node        type lvc_s_layn,
     lt_item_layout type lvc_t_layi,
     ls_item_layout type lvc_s_layi
     .

field-symbols:<fs_data> type  ty_data
              .

datag_alv_tree         type ref to cl_gui_alv_tree,
      g_custom_container type ref to cl_gui_custom_container.

datagt_fcat type  lvc_t_fcat,
      gs_fcat type  lvc_s_fcat.

datags_sflight type sflight ,      "Output-Table
      gt_sflight type  table of sflight,
      gt_scala   type standard table of ty_tree_node,
      ok_code    like sy-ucomm,
      save_ok    like sy-ucomm.           "OK-Code

data:g_docking   type ref to cl_gui_docking_container,
     g_spliter   type ref to cl_gui_splitter_container,
     g_container type ref to cl_gui_container,
     dynnr       type dynnr.

constants:g_repid     like sy-repid value sy-repid "当前系统
.

class lcl_tree_event_receiver definition.

  public section.
    methods handle_node_double_click
      for event node_double_click of cl_gui_alv_tree
      importing node_key sender.
    methods handle_item_double_click
      for event item_double_click of cl_gui_alv_tree
      importing fieldname node_key.

endclass.

class lcl_tree_event_receiver implementation.

  method handle_node_double_click.

    datalt_children type lvc_t_nkey,
          ls_children type lvc_nkey.

    "根据点击的结点找出所有的下一个结点
    call method sender->get_children
      exporting
        i_node_key  node_key
      importing
        et_children lt_children.

    if not lt_children is initial.

      "展开结点
      call method sender->expand_node
        exporting
          i_node_key    node_key
          i_level_count 2.

    endif.

  endmethod.

  method handle_item_double_click.
    datal_value type lvc_value,
          l_layi  type lvc_t_layi,
          l_layn  type lvc_s_layn.

    call method g_alv_tree->get_outtab_line
      exporting
        i_node_key     node_key
      importing
        e_outtab_line  ls_tree
        e_node_text    l_value
        et_item_layout l_layi
        es_node_layout l_layn
      exceptions
        node_not_found 1
        others         2.
    if sy-subrc and ls_tree-tcode is not initial.
*      data:mseg type string .
*      mseg = ls_tree-text.
*      message mseg type 'I' .
      call transaction ls_tree-tcode.
    endif.

  endmethod.

endclass.

initialization.

at selection-screen.

*at selection-screen on value-request for p_zztime.
*  perform frm_get_datatime.

start-of-selection.
*  perform set_quanx.  "权限
*  perform frm_lock.   "锁

end-of-selection.
*调用DYPLNR
  call screen 1000.

*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*       状态栏
*----------------------------------------------------------------------*
module status_1000 output.
  set pf-status 'STATUS_1000'.
  set titlebar  'TITLE_1000'.
endmodule" STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*       命令区域
*----------------------------------------------------------------------*
module user_command_1000 input.
  data:action like sy-ucomm,subrc like sy-subrc.
  action ok_code.
  clearok_code.

  case action.
    when 'BACK'.
      leave to screen 0.
    when 'EXIT'.
      leave program.

    when 'SLT'.
      leave to screen 0.
    when others.
      call method cl_gui_cfw=>dispatch.
  endcase.

  call method cl_gui_cfw=>flush.
endmodule" USER_COMMAND_0100 INPUT
module pbo_include output.
  if g_docking is initial .
    create object g_docking
      exporting
        repid                       g_repid
        dynnr                       '1000'
        side                        cl_gui_docking_container=>dock_at_top
        extension                   '440'
      exceptions
        cntl_error                  1
        cntl_system_error           2
        create_error                3
        lifetime_error              4
        lifetime_dynpro_dynpro_link 5.

    create object g_spliter "布局之分割容器
      exporting
        parent  g_docking
        rows    1
        columns 2.

    call method g_spliter->set_column_width "设置分割单元格的大小
      exporting
        id    1
        width 30.

    call method g_spliter->get_container
      exporting
        row       1
        column    1
      receiving
        container g_container.
  endif.


  create object g_alv_tree
    exporting
      parent                      g_container
*     node_selection_mode         = cl_gui_column_tree=>node_sel_mode_single   "单选节点
      node_selection_mode         cl_gui_column_tree=>node_sel_mode_single "多选节点
      item_selection              'X'  "能否选中节点上的单个元素
      no_html_header              'X'
      no_toolbar                  ''
    exceptions
      cntl_error                  1
      cntl_system_error           2
      create_error                3
      lifetime_error              4
      illegal_node_selection_mode 5
      failed                      6
      illegal_column_name         7.

* 树形hierarchy
  data l_hierarchy_header type treev_hhdr.
  perform build_hierarchy_header changing l_hierarchy_header.
  perform frm_tree_fieldcat.

  call method g_alv_tree->set_table_for_first_display
    exporting
*     i_structure_name    = 'SFLIGHT'
      is_hierarchy_header l_hierarchy_header
      i_background_id     'ALV_BACKGROUND'
    changing
      it_outtab           lt_tree "table must be empty !,gt_sflight
      it_fieldcatalog     gt_fcat"fieldcat

  perform create_tree.

  call method g_alv_tree->update_calculations.
  perform register_events.

  call method g_alv_tree->frontend_update.

endmodule" PBO_INCLUDE OUTPUT

form create_tree.
  datap_relat_key type lvc_nkey,
        p_node_key1 type lvc_nkey,
        p_node_key2 type lvc_nkey,
        p_node_key3 type lvc_nkey,
        l_node_text type lvc_value,
        lv_line     type i.

  datal_parent_id type lvc_nkey,
        l_next_id   type lvc_nkey,
        l_last_key  type lvc_nkey
        .

  perform frm_getdata.

  loop at lt_data assigning <fs_data> where parent_id '0000000001'."一级文件夹

    perform add_parentid_line using <fs_data>
                                    ''
                              changing l_parent_id.

    <fs_data>-nkey l_parent_id.
  endloop.

  sort lt_data by menu_level.
  loop at lt_data assigning <fs_data> where menu_level > 1."二级及以下文件夹
*    at new menu_level.
    clear:wa_data.
    read table lt_data into wa_data with key object_id <fs_data>-parent_id.
    if sy-subrc and wa_data-nkey is not initial.
*      动态添加节点
      perform add_parentid_line using <fs_data>
                                  wa_data-nkey
                            changing l_parent_id.

      <fs_data>-nkey l_parent_id.
    endif.
*    endat.
  endloop.
endform.

form build_hierarchy_header changing p_hierarchy_header type treev_hhdr.

  p_hierarchy_header-heading '我的菜单'."名称
  p_hierarchy_header-tooltip 'Tcode 事务代码'."停留时显示
  p_hierarchy_header-width 30.
  p_hierarchy_header-width_pix ' '.

endform.

form exit_program.

  call method g_custom_container->free.
  leave program.

endform.

form register_events .
  datalt_events        type cntl_simple_events,
        l_event          type cntl_simple_event,
        l_event_receiver type ref to lcl_tree_event_receiver.

  call method g_alv_tree->get_registered_events
    importing
      events lt_events.

  l_event-eventid cl_gui_column_tree=>eventid_node_double_click.
  append l_event to lt_events.
  l_event-eventid cl_gui_column_tree=>eventid_item_double_click.
  append l_event to lt_events.

  call method g_alv_tree->set_registered_events
    exporting
      events                    lt_events
    exceptions
      cntl_error                1
      cntl_system_error         2
      illegal_event_combination 3.

  create object l_event_receiver.
  set handler l_event_receiver->handle_node_double_click for g_alv_tree.
  set handler l_event_receiver->handle_item_double_click for g_alv_tree.

endform.                    " register_events
form add_fieldcat using value(p_fieldnametype c
                            value(p_outputlentype c
                            p_text   type c
                            value(p_key)  type c
                            value(p_just)  type c
                            value(p_txt_fieldtype c
                            value(p_field)  type c
                            value(p_tabletype c
                            value(p_edittype c
                            value(p_qfieldnametype c
                            value(p_sumtype c
                            value(p_no_zerotype c
                            value(datatypetype c
                            value(intlentype c
                            value(decimalstype c
                            value(drdn_hndltype c
                            value(drdn_fieldtype c
  value(inttypetype c
                   changing p_gr_fieldcat_lvc type lvc_t_fcat.
  datals_fcat type lvc_s_fcat.
  if p_fieldname 'CKBOX'.
    ls_fcat-checkbox 'X'.
  endif.
  ls_fcat-fieldname p_fieldname.                 "字段名
  ls_fcat-outputlen p_outputlen.                 "字段长度
  ls_fcat-coltext  p_text.                       "字段显示内容
  ls_fcat-key p_key.                             "关键值
  ls_fcat-just p_just.                           "对
  ls_fcat-ref_table p_table.                     "表名
  ls_fcat-ref_field p_field.                     "引用字段名
  ls_fcat-coltext p_txt_field.                   "自定义字段名
  ls_fcat-edit p_edit.                           "是否可编辑
  ls_fcat-qfieldname p_qfieldname.               "单位引用
  ls_fcat-do_sum p_sum.
  ls_fcat-no_zero p_no_zero.

  ls_fcat-datatype    datatype.
  ls_fcat-intlen      intlen.
  ls_fcat-decimals    decimals.

  ls_fcat-drdn_hndl drdn_hndl."下拉列表
  ls_fcat-drdn_field drdn_field."下拉列表字段
  ls_fcat-inttype    inttype .
  append ls_fcat to p_gr_fieldcat_lvc.
*  CLEAR:P_GR_FIELDCAT_LVC.
endform" ADD_FIELDCAT
form frm_tree_fieldcat .
  refresh gt_fcat.

  perform add_fieldcat using 'TCODE'  '30' ''  ''  ''  '事务代码' '' '' '' '' '' '' '' '' '' '' '' '' changing gt_fcat[].

endform.
form frm_getdata .
  call function 'BX_FAVOS_READ_ALL_NODES'
    exporting
      user_name              sy-uname
      language               sy-langu
      sort_nodes             'X'
*     GENERATE_URL           = ' '
*     REPLACE_RFC_VARIABLES  = ' '
*     GET_RFC_DESTINATION_TYPE       = ' '
*     USAGE_MODE             = ' '
*     DEVICE                 = ' '
    tables
      output_nodes_and_texts lt_bxmnodes
*     GENERATED_URLS         =
    .

  refresh:lt_data.
  clear:ls_data.
  loop at lt_bxmnodes into ls_bxmnodes.
    move-corresponding ls_bxmnodes to ls_data.
    append ls_data to lt_data.
  endloop.
endform" FRM_GETDATA

form add_parentid_line  using    ls_data type ty_data
                                 p_relat_key type lvc_nkey
                        changing  p_node_key  type lvc_nkey
                        .

  clear:l_node_text,ls_sflight,ls_node,lt_item_layout,ls_item_layout
  .

* set item-layout
*  ls_item_layout-t_image = '@3P@'."@3P@
  ls_item_layout-fieldname g_alv_tree->c_hierarchy_column_name.
  ls_item_layout-style cl_gui_column_tree=>style_intensifd_critical.
  append ls_item_layout to lt_item_layout.

  clear:ls_tree.
  move-corresponding ls_data to ls_tree.
* add node
  if ls_data-node_type 'T'.
    l_node_text |{ ls_data-tcode }-{ ls_data-text }|.
  else.
    l_node_text ls_data-text.
  endif.

  ls_node-n_image   space.
  ls_node-exp_image space.

  call method g_alv_tree->add_node
    exporting
      i_relat_node_key p_relat_key "p_relat_key TYPE lvc_nkey
      i_relationship   cl_gui_column_tree=>relat_last_child
      i_node_text      l_node_text
      is_outtab_line   ls_tree
      is_node_layout   ls_node
      it_item_layout   lt_item_layout
    importing
      e_new_node_key   p_node_key.

endform.

 

 

View Entire Topic
Sandra_Rossi
Active Contributor

By program? (Manually, there's a download for favorites.)

For favorites. search the Web around SMEN_BUFFC or BX_FAVOS_READ_ALL_NODES.

For "SAP Menu", it's the same for all users. The users can also display another menu which is role-based (varies for every user). For "SAP Menu", it's a menu named S000, use STREE_STRUCTURE_READ_N_TOP_LEV. As I don't find answers in the Web, I post the code here:

  DATA structure_id    TYPE ttree-id.
  DATA read_all_levels TYPE hier_types-char1.
  DATA read_n_levels   TYPE hier_types-numc2.
  DATA structure_table TYPE STANDARD TABLE OF hier_iface.

  structure_id = 'S000'.
  read_all_levels = 'X'.
  CALL FUNCTION 'STREE_STRUCTURE_READ_N_TOP_LEV'
    EXPORTING
      structure_id          = structure_id
      read_all_levels       = read_all_levels
      read_n_levels         = read_n_levels
    TABLES
      structure_table       = structure_table.
new163wang
Explorer
0 Kudos
Thank you for your help!