Howto insert a menu reference into the MainMenu

From Axaptapedia
Jump to: navigation, search

Description[edit]

The following demonstrates how to insert a menu reference into a menu via code.

Dynamics AX versions[edit]

Has been tested on Dynamics AX 2009 SP1 RU-6

Code[edit]

<xpp> /// <summary> /// The following demonstrates how to add a reference to the basic menu in the bank menu. /// </summary>

  1. AOT
  2. define.MenuBank('@SYS7439') //Bank
  3. define.MenuBasic('@SYS5921') //Basic

Menu menuBank, menuBasic;

// Gets menu on which the menu reference will be added menuBank = TreeNode::findNode(#MenusPath + #AOTDelimiter + #MenuBank);

// Gets the menu reference menuBasic = TreeNode::findNode(#MenusPath + #AOTDelimiter + #MenuBasic);

// Adds the menu reference to a menu menuBank.addMenuReference(menuBasic);

// Saves the changes menuBank.AOTsave(); </xpp>

Before After
Before After

Previous Versions[edit]

I don't know how to do it via API (if you call AOTAdd it just inserts given menu itself). The only way I've foun is to export the MainMenu to XPO, change it, and import again.

Here is example code: <xpp> static void Edictum_InstallMainMenu(Args _args) {

   Menu m=new Menu('MainMenu');
   TextBuffer b=new TextBuffer();
   str tempFile=WinApi::getTempPath()+'tmp_main_menu.xpo';
   int toInsert;
   /// find the last occurence of the string _s
   int findLast(str _s)
   {
       int ret;
       int pos = 1;
       while(b.find(_s, pos))
       {
           ret = b.matchPos();
           pos = b.matchPos() + b.matchLen();
       }
       return ret;
   }
   /// import file by full path
   void import(str _fileName)
   {
       SysImportElements import=new SysImportElements();
       boolean examinedFile=false;
   ;
       import.newFile(_fileName);
       import.parmAddToProject( false);
       import.parmDeleteSubNodes(true);
       import.parmImportWithIds(false);
       import.parmOverrideLocks( false);
       import.parmImportLabels(false);
       import.parmImportAot(true);
       import.import();
   }
   // export main menu to the file in the temporary directory
   m.treeNodeExport(tempFile);
   b.fromFile(tempFile);
   // finding the last occurence of menu end
   toInsert = findLast('\n: *ENDMENU\n');
   if (toInsert)
   {
       // insert reference code
       b.insert(@'
   MENUREFERENCE
     PROPERTIES
       Name                #DOC_Main
     ENDPROPERTIES
   ENDMENUREFERENCE

', toInsert);

       // save the code back to the file
       b.toFile(tempFile);
       // import changed xpo
       import(tempFile);
       info('ok');
   }
   else
       info('bad');

} </xpp>