Tuesday, 12 January 2021

Find the list of menuitems assigned to particular privilege in D365 F&O

 Today, I will be discussing out about how to fetch the list of menu items assigned to particular privilege using X++. In AX 2012 this data was stored was on table level. However,  in D365 F&O we will be using metadata API to get this information.

Below is the code snippet of it. The code will extract the list of all menu items which are covered under privilege 'CurrencyView' and it will export data in excel sheet. The excel sheet will contain following information : 

- Privilege

- Entry point (Menu item)

- System name of  menu item

- Menu item type


using Microsoft.Dynamics.ApplicationPlatform.Environment;

using Microsoft.Dynamics.AX.Metadata.Storage;

using Microsoft.Dynamics.AX.Metadata.Storage.Runtime;

using Microsoft.Dynamics.AX.Metadata.MetaModel;

using System.IO;

using OfficeOpenXml;

using OfficeOpenXml.Style;

using OfficeOpenXml.Table;

class Test_GetMenusForPrivilege

{

    public static void main(Args _args)

    {

        str packageDir = EnvironmentFactory::GetApplicationEnvironment().Aos.PackageDirectory;

        var providerConfig = new RuntimeProviderConfiguration(packageDir);

        var provider = new MetadataProviderFactory().CreateRuntimeProvider(providerConfig);

        SecurityPrivilege   secPrivilege;

        MemoryStream memoryStream = new MemoryStream();


        using (var package = new ExcelPackage(memoryStream))

        {

            var currentRow = 1;

            var worksheets = package.get_Workbook().get_Worksheets();

            var CustTableWorksheet = worksheets.Add("Export");

            var cells = CustTableWorksheet.get_Cells();

            OfficeOpenXml.ExcelRange cell = cells.get_Item(currentRow, 1);

            System.String value = "Privilege";

            cell.set_Value(value);

            cell = null;

            value = "Entrypoint or menuitem name";

            cell = cells.get_Item(currentRow, 2);

            cell.set_Value(value);

            cell = null;

            value = "System name of menuitem";

            cell = cells.get_Item(currentRow, 3);

            cell.set_Value(value);

            cell = null;

            value = "Menu item type";

            cell = cells.get_Item(currentRow, 4);

            cell.set_Value(value);


            while select Identifier from secPrivilege

            where secPrivilege.Identifier == "CurrencyView"

            {

                AxSecurityPrivilege privilege = provider.SecurityPrivileges.Read(secPrivilege.Identifier);

                var enumerator = privilege.EntryPoints.GetEnumerator();


                while (enumerator.MoveNext())

                {

                    currentRow ++;

                    cell = null;


                    cell = cells.get_Item(currentRow, 1);

                    cell.set_Value(secPrivilege.Identifier);


                    AxSecurityEntryPointReference entryPoint = enumerator.Current;

                    cell = null;

                    cell = cells.get_Item(currentRow, 2);

                    cell.set_Value(entryPoint.Name);


                    cell = null;

                    cell = cells.get_Item(currentRow, 3);

                    cell.set_Value(entryPoint.ObjectName);


                    cell = null;

                    cell = cells.get_Item(currentRow, 4);

                    cell.set_Value(entryPoint.ObjectType);

                }

            }

            package.Save();

            file::SendFileToUser(memoryStream, "PrivilegeWithMenuItems");


        }   

    }

}


Output file



Custom Business events Part 3 - (Activate custom business event) in D365 F&O

 In this blog we will discuss about the steps to activate a custom business in D365 F&O. As we know that business event catalog does not...