Skip to main content

Notifications

Microsoft.Dynamics.AX.Metadata from .NET to query all menu items from Menus and display configuration keys and related privileges / duties (D365FO)

Required reference links in solution:
2021_2D00_07_2D00_29-14_5F00_36_5F00_31_2D00_Notes-_2D00_-Evernote.png
C# code:
using System; 
using System.Linq; 
using System.Text; 
using Microsoft.Dynamics.AX.Metadata.MetaModel; 
using Microsoft.Dynamics.AX.Metadata.Storage; 
using Microsoft.Dynamics.AX.Metadata.Storage.Runtime; 
using System.IO; 
using System.Collections.Generic; 
namespace DummyAppForD365FoMetadata 
{ 
    class Program 
    { 
        static void Main(string[] args) 
        { 
            Program program = new Program(); 
            program.Run(); 
        } 
        Microsoft.Dynamics.AX.Metadata.Providers.IMetadataProvider  metadataProviderGlobal; 
        StringBuilder                                               strBuildGlobal = new StringBuilder(); 
        Dictionary>                            menuItemPrivilegesMap = new Dictionary>(); 
        Dictionary>                            privilegesDutiesMap = new Dictionary>(); 
        void Run() 
        { 
            //string packagesDir = @"K:\AosService\PackagesLocalDirectory"; 
            //var runtimeConfiguration = new RuntimeProviderConfiguration(packagesDir); 
            //var metadataProvider = new MetadataProviderFactory().CreateRuntimeProviderWithExtensions(runtimeConfiguration); 
            var environment = Microsoft.Dynamics.ApplicationPlatform.Environment.EnvironmentFactory.GetApplicationEnvironment(); 
            var runtimeConfiguration = new RuntimeProviderConfiguration(environment.Aos.PackageDirectory); 
            metadataProviderGlobal = new MetadataProviderFactory().CreateRuntimeProviderWithExtensions(runtimeConfiguration); 
            strBuildGlobal.AppendLine("MenuItemPath" "\t"  "ConfigKey"   "\t"   "MenuItemPrivileges"   "\t"   "Duties"); 
            foreach (String securityPrivilegePkStr in metadataProviderGlobal.SecurityPrivileges.GetPrimaryKeys() ) 
            //foreach (String securityPrivilegePkStr in metadataProviderGlobal.SecurityPrivileges.GetPrimaryKeys().Where(n => n == "CustTableMaintain" 
            //|| n == "CustTableView" || n == "AuditPolicyCaseDetailMaintain")) 
            { 
                var securityPrivilege = metadataProviderGlobal.SecurityPrivileges.Read(securityPrivilegePkStr); 
                foreach(var securityPrivilegeEntryPoints in securityPrivilege.EntryPoints 
                    .Where(n => n.ObjectType == Microsoft.Dynamics.AX.Metadata.Core.MetaModel.EntryPointType.MenuItemAction  
                                || n.ObjectType == Microsoft.Dynamics.AX.Metadata.Core.MetaModel.EntryPointType.MenuItemDisplay 
                                || n.ObjectType == Microsoft.Dynamics.AX.Metadata.Core.MetaModel.EntryPointType.MenuItemOutput)) 
                { 
                    String securityPrivilegeEntryPointsMapKey; 
                    securityPrivilegeEntryPointsMapKey = securityPrivilegeEntryPoints.ObjectType   "_"   securityPrivilegeEntryPoints.ObjectName; 
                    if (!menuItemPrivilegesMap.ContainsKey(securityPrivilegeEntryPointsMapKey)) 
                    { 
                        List privileges = new List(); 
                        privileges.Add(securityPrivilege.Name); 
                        menuItemPrivilegesMap.Add(securityPrivilegeEntryPointsMapKey, privileges); 
                    } 
                    else 
                    { 
                        menuItemPrivilegesMap[securityPrivilegeEntryPointsMapKey].Add(securityPrivilege.Name); 
                    } 
                } 
            } 
            //foreach (var menuItemItem in menuItemPrivilegesMap) 
            //{ 
            //    strBuildGlobal.AppendLine(menuItemItem.Key); 
            //    foreach (var securityPrivilegeItem in menuItemItem.Value) 
            //    { 
            //        strBuildGlobal.AppendLine("\t"  securityPrivilegeItem); 
            //    } 
            //} 
            //File.WriteAllText("menuItemPrivilegesMap.txt", strBuildGlobal.ToString()); 
            //strBuildGlobal.Clear(); 
            foreach (String securityDutyPkStr in metadataProviderGlobal.SecurityDuties.GetPrimaryKeys()) 
            { 
                var securityDuty = metadataProviderGlobal.SecurityDuties.Read(securityDutyPkStr); 
                foreach (var securityPrivilegeAssignedToDuty in securityDuty.Privileges) 
                { 
                    String securityPrivilegeMapKey; 
                    securityPrivilegeMapKey = securityPrivilegeAssignedToDuty.Name; 
                    if (!privilegesDutiesMap.ContainsKey(securityPrivilegeMapKey)) 
                    { 
                        List duties = new List(); 
                        duties.Add(securityDuty.Name); 
                        privilegesDutiesMap.Add(securityPrivilegeMapKey, duties); 
                    } 
                    else 
                    { 
                        privilegesDutiesMap[securityPrivilegeMapKey].Add(securityDuty.Name); 
                    } 
                } 
            } 
            //foreach (var privilegeItem in privilegesDutiesMap) 
            //{ 
            //    strBuildGlobal.AppendLine(privilegeItem.Key); 
            //    foreach (var dutyItem in privilegeItem.Value) 
            //    { 
            //        strBuildGlobal.AppendLine("\t"   dutyItem); 
            //    } 
            //} 
            //File.WriteAllText("menuItemDutiesMap.txt", strBuildGlobal.ToString()); 
            //strBuildGlobal.Clear(); 
            //foreach (String rootMenuPkStr in metadataProviderGlobal.Menus.GetPrimaryKeys().Where(n => n == "AccountsPayable")) 
            foreach (String rootMenuPkStr in metadataProviderGlobal.Menus.GetPrimaryKeys()) 
            { 
                //strBuild.AppendLine(rootMenuPkStr); //AccountsPayable 
             
                AxMenu axMenuRoot = metadataProviderGlobal.Menus.Read(rootMenuPkStr); 
             
                foreach (var item in axMenuRoot.Elements) 
                { 
                    GetNodes(rootMenuPkStr, item); 
                } 
             
            } 
            File.WriteAllText("WriteText.txt", strBuildGlobal.ToString()); 
            //Console.Write("test"); 
            //Console.Read();             
        } 
        public void GetNodes(String _prefix, AxMenuElement _axMenuElement) 
        { 
            if (_axMenuElement == null) 
            { 
                return; 
            } 
            //if (_axMenuElement is AxMenuElementMenuItem) 
            //{ 
            //    //_strBuild.AppendLine(_prefix   ">"   ((AxMenuElementMenuItem)_axMenuElement).Name); 
            //    AppendAxMenuElementMenuItemInfo(_prefix , (AxMenuElementMenuItem)_axMenuElement);                 
            //} 
            if (_axMenuElement is AxMenuElementSubMenu) 
            { 
                foreach (var item in ((AxMenuElementSubMenu)_axMenuElement).Elements) 
                { 
                    if (item is AxMenuElementSubMenu) 
                    { 
                        //GetNodes(_prefix   ">"   item.Name, item); 
                        GetNodes(_prefix   ">"   GetLabel(item.Name, false), item); 
                    } 
                    else if (item is AxMenuElementMenuItem) 
                    { 
                        //AppendAxMenuElementMenuItemInfo(_prefix  ">"  _axMenuElement.Name, (AxMenuElementMenuItem)item); 
                        AppendAxMenuElementMenuItemInfo(_prefix   ">"   GetLabel(_axMenuElement, false), (AxMenuElementMenuItem)item); 
                    }                     
                } 
            } 
        } 
        private String GetLabel(T _object, Boolean _showSysName) 
        { 
            String labelValue = ""; 
            String nameValue = ""; 
            String retValue = ""; 
            var type = _object.GetType(); 
            if (type.GetProperty("Label") != null) 
            { 
                String label = type.GetProperty("Label").GetValue(_object).ToString(); 
                labelValue = Microsoft.Dynamics.Ax.Xpp.LabelHelper.GetLabel(label); 
            } 
            if (_showSysName && type.GetProperty("Name") != null) 
            { 
                nameValue = type.GetProperty("Name").GetValue(_object).ToString(); 
            } 
            retValue = labelValue   (nameValue.Length>0 ? " ("   nameValue   ")" : ""); 
            return retValue; 
        } 
             
        private void AppendAxMenuElementMenuItemInfo(String _prefix, AxMenuElementMenuItem _axMenuElementMenuItem) 
        { 
            AxMenuItem resolvedMenuItem = null; 
            String menuItemType = ""; 
            switch (_axMenuElementMenuItem.MenuItemType) 
            { 
                case Microsoft.Dynamics.AX.Metadata.Core.MetaModel.MenuItemType.Display: 
                    resolvedMenuItem = metadataProviderGlobal.MenuItemDisplays.Read(_axMenuElementMenuItem.MenuItemName); 
                    menuItemType = Enum.GetName(typeof(Microsoft.Dynamics.AX.Metadata.Core.MetaModel.EntryPointType), 
                        Microsoft.Dynamics.AX.Metadata.Core.MetaModel.EntryPointType.MenuItemDisplay);                         
                    break; 
                case Microsoft.Dynamics.AX.Metadata.Core.MetaModel.MenuItemType.Output: 
                    resolvedMenuItem = metadataProviderGlobal.MenuItemOutputs.Read(_axMenuElementMenuItem.MenuItemName); 
                    menuItemType = Enum.GetName(typeof(Microsoft.Dynamics.AX.Metadata.Core.MetaModel.EntryPointType), 
                        Microsoft.Dynamics.AX.Metadata.Core.MetaModel.EntryPointType.MenuItemOutput); 
                    break; 
                case Microsoft.Dynamics.AX.Metadata.Core.MetaModel.MenuItemType.Action: 
                    resolvedMenuItem = metadataProviderGlobal.MenuItemActions.Read(_axMenuElementMenuItem.MenuItemName); 
                    menuItemType = Enum.GetName(typeof(Microsoft.Dynamics.AX.Metadata.Core.MetaModel.EntryPointType), 
                        Microsoft.Dynamics.AX.Metadata.Core.MetaModel.EntryPointType.MenuItemAction); 
                    break; 
                default: 
                    break; 
            } 
            String securityPrivilegeEntryPointsMapKey = menuItemType   "_"   resolvedMenuItem.Name; 
            StringBuilder formedAppendLineStrBuilder = new StringBuilder(); 
            StringBuilder strBuildSecurityPrivileges = new StringBuilder(); 
            StringBuilder strBuildSecurityDuties = new StringBuilder(); 
            //formedAppendLineStrBuilder.Append(_prefix   ">"   _axMenuElementMenuItem.Name); 
            formedAppendLineStrBuilder.Append(_prefix   ">"   GetLabel(resolvedMenuItem, true)); 
             
            formedAppendLineStrBuilder.Append("\t");//ConfigKey 
            if (resolvedMenuItem.ConfigurationKey.Length > 0) 
            { 
                var configKey   = metadataProviderGlobal.ConfigurationKeys.Read(resolvedMenuItem.ConfigurationKey); 
                //formedAppendLineStrBuilder.Append(" (ConfigKey:"   configKey.Label   ")"); 
                formedAppendLineStrBuilder.Append(GetLabel(configKey, true)); 
            } 
            var duties = new HashSet(); 
            if (menuItemPrivilegesMap.ContainsKey(securityPrivilegeEntryPointsMapKey)) 
            { 
                 
                foreach (var securityPrivilegeItem in menuItemPrivilegesMap[securityPrivilegeEntryPointsMapKey]) 
                { 
                    //strBuildGlobal.AppendLine("\t"   securityPrivilegeItem); 
                    strBuildSecurityPrivileges.Append(strBuildSecurityPrivileges.Length > 0 ? "," : ""); 
                    //strBuildSecurityPrivileges.Append(securityPrivilegeItem); 
                    var secPrivilege = metadataProviderGlobal.SecurityPrivileges.Read(securityPrivilegeItem); 
                    strBuildSecurityPrivileges.Append(GetLabel(secPrivilege, true) ); 
                    if (privilegesDutiesMap.ContainsKey(securityPrivilegeItem)) 
                    { 
                        foreach (var dutyItem in privilegesDutiesMap[securityPrivilegeItem]) 
                        { 
                            duties.Add(dutyItem); 
                        } 
                    } 
                } 
            } 
            if (duties.Count > 0) 
            { 
                foreach (var dutyItem in duties) 
                { 
                    strBuildSecurityDuties.Append(strBuildSecurityDuties.Length > 0 ? "," : ""); 
                    //strBuildSecurityDuties.Append(dutyItem); 
                    var secDuty = metadataProviderGlobal.SecurityDuties.Read(dutyItem); 
                    strBuildSecurityDuties.Append(GetLabel(secDuty, true)); 
                } 
            } 
            formedAppendLineStrBuilder.Append("\t");//SecurityPrivileges 
            if (strBuildSecurityPrivileges.Length > 0) 
            { 
                //formedAppendLineStrBuilder.Append("(SecurityPrivileges: "   strBuildSecurityPrivileges.ToString()   ")"); 
                formedAppendLineStrBuilder.Append(strBuildSecurityPrivileges.ToString()); 
            } 
            formedAppendLineStrBuilder.Append("\t");//SecurityDuties 
            if (strBuildSecurityDuties.Length > 0) 
            { 
                //formedAppendLineStrBuilder.Append("(SecurityDuties: "   strBuildSecurityDuties.ToString()   ")"); 
                formedAppendLineStrBuilder.Append(strBuildSecurityDuties.ToString()); 
            } 
             
            strBuildGlobal.AppendLine(formedAppendLineStrBuilder.ToString()); 
        } 
    } 
}

WriteText.txt output example:
2021_2D00_07_2D00_29-14_5F00_38_5F00_10_2D00_Greenshot.png
code snippet widget

Comments