private void createSecurityElement_FromDataEntity(IDataEntityView selectedDataEntity, string suffix)
        {
            //Create Security privilege
            AxSecurityPrivilege axSecurityPriv = new AxSecurityPrivilege()
            {
                Name = selectedDataEntity.Name + suffix
            };

            axSecurityPriv.DataEntityPermissions.Add(
                new AxSecurityDataEntityPermission()
            {
                Grant = suffix == "Maintain"
                            ? Microsoft.Dynamics.AX.Metadata.Core.MetaModel.AccessGrant.ConstructGrantDelete()
                            : Microsoft.Dynamics.AX.Metadata.Core.MetaModel.AccessGrant.ConstructGrantRead(),
                Name = selectedDataEntity.Name
            });

            // Find current model
            var modelSaveInfo = Common.CommonUtil.GetCurrentModelSaveInfo();

            var metaModelService = Common.CommonUtil.GetModelSaveService();

            metaModelService.CreateSecurityPrivilege(axSecurityPriv, modelSaveInfo);

            Common.CommonUtil.AddElementToProject(axSecurityPriv);
        }
        private void createSecurityElement_FromDataEntity(IDataEntityView selectedDataEntity, string suffix)
        {
            //Create Security privilege
            AxSecurityPrivilege axSecurityPriv = new AxSecurityPrivilege()
            {
                Name = selectedDataEntity.Name + suffix
            };

            axSecurityPriv.DataEntityPermissions.Add(
                new AxSecurityDataEntityPermission()
            {
                Grant = suffix == "Maintain"
                            ? Microsoft.Dynamics.AX.Metadata.Core.MetaModel.AccessGrant.ConstructGrantDelete()
                            : Microsoft.Dynamics.AX.Metadata.Core.MetaModel.AccessGrant.ConstructGrantRead(),
                Name = selectedDataEntity.Name
            });

            // Assign the correct label on this by addint maintain or view in the label, copy the base label from the menu item
            string label = selectedDataEntity.Label;

            if (label.StartsWith("@"))
            {
                //label = Labels.LabelHelper.FindLabel(label).LabelText;
                label = Labels.LabelHelper.FindLabelGlobally(label).LabelText;
            }

            if (suffix.Equals("Maintain"))
            {
                label = "Maintain " + label;
            }
            else if (suffix.Equals("View"))
            {
                label = "View " + label;
            }
            // Convert to camel case
            if (String.IsNullOrEmpty(label) == false)
            {
                char[] a = label.ToLowerInvariant().ToCharArray();
                a[0]  = char.ToUpperInvariant(a[0]);
                label = new String(a);
            }
            axSecurityPriv.Label = label;

            // Find current model
            var modelSaveInfo = Common.CommonUtil.GetCurrentModelSaveInfo();

            var metaModelService = Common.CommonUtil.GetModelSaveService();

            metaModelService.CreateSecurityPrivilege(axSecurityPriv, modelSaveInfo);

            Common.CommonUtil.AddElementToProject(axSecurityPriv);
        }
        /// <summary>
        /// Called when user clicks on the add-in menu
        /// </summary>
        /// <param name="e">The context of the VS tools and metadata</param>
        public override void OnClick(AddinDesignerEventArgs e)
        {
            Microsoft.Dynamics.AX.Metadata.Core.MetaModel.EntryPointType entryPointType;
            try
            {
                // we will create 2 security privileges for the menu item with the same name + Maintain,  +View
                var selectedMenuItem = e.SelectedElement as IMenuItem;
                if (selectedMenuItem != null)
                {
                    var metadataType = selectedMenuItem.GetMetadataType();

                    if (selectedMenuItem is IMenuItemAction)
                    {
                        entryPointType = Microsoft.Dynamics.AX.Metadata.Core.MetaModel.EntryPointType.MenuItemAction;
                    }
                    else if (selectedMenuItem is IMenuItemDisplay)
                    {
                        entryPointType = Microsoft.Dynamics.AX.Metadata.Core.MetaModel.EntryPointType.MenuItemDisplay;
                    }
                    else if (selectedMenuItem is IMenuItemOutput)
                    {
                        entryPointType = Microsoft.Dynamics.AX.Metadata.Core.MetaModel.EntryPointType.MenuItemOutput;
                    }
                    else
                    {
                        return;
                    }

                    this.createSecurityElement_FromMenuItem(selectedMenuItem, entryPointType, "Maintain");
                    this.createSecurityElement_FromMenuItem(selectedMenuItem, entryPointType, "View");
                }
                else if (e.SelectedElement is IDataEntityView)
                {
                    IDataEntityView selectedElementDataEntity = e.SelectedElement as IDataEntityView;
                    this.createSecurityElement_FromDataEntity(selectedElementDataEntity, "Maintain");
                    this.createSecurityElement_FromDataEntity(selectedElementDataEntity, "View");
                }
            }
            catch (Exception ex)
            {
                CoreUtility.HandleExceptionWithErrorMessage(ex);
            }
        }