Howto insert a menu reference into the MainMenu

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

Dynamics AX versions
Has been tested on Dynamics AX 2009 SP1 RU-6

Code
/// /// The following demonstrates how to add a reference to the basic menu in the bank menu. /// 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);
 * AOT
 * 1) define.MenuBank('@SYS7439') //Bank
 * 2) define.MenuBasic('@SYS5921') //Basic

// Saves the changes menuBank.AOTsave;

Previous Versions
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: 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'); }