web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

How get Associated Duty name for roles using SysSecurity

(1) ShareShare
ReportReport
Posted on by 2
I want to get all Duty names for Role = Data management administrator and used this code , but its not generating , duties name
 
using Microsoft.Dynamics.AX.Security.Management.Domain;internal final class TestClass{                      /// <summary>    /// Class entry point. The system will call this method when a designated menu    /// is selected or when execution starts and this class is set as the startup class.    /// </summary>    /// <param name = /_args/>The specified arguments.</param>    public static void main(Args _args)    {        var RolesRepo       = SysSecurity::GetSecurityRepository().Roles;        var RolesEnumerator = RolesRepo.LoadAll().GetEnumerator();        var DutyRepo        = SysSecurity::GetSecurityRepository().Duties;        var DutyEnumerator  = DutyRepo.LoadAll().GetEnumerator();        var PrivilegeRepo       = SysSecurity::GetSecurityRepository().Privileges;        var PrivilegeEnumerator = PrivilegeRepo.LoadAll().GetEnumerator();         while (RolesEnumerator.MoveNext())        {                         Role Role = RolesEnumerator.Current;            if(Role.Name == 'Data management administrator')                         {                info(strFmt(/Name %1/, Role.Name));                info(strFmt(/Description %1/, Role.Description));                info(strFmt(/AOT Name %1/, Role.Identifier.ToString()));                info(strFmt(/Duty %1/, Role.Duties));                info(strFmt(/Privilege %1/, Role.Privileges));                info(strFmt(/Subole %1/, Role.SubRoles));            }                       while(DutyEnumerator.MoveNext())            {                Duty Duty = DutyEnumerator.Current;                if(Duty.Roles.ToString() == Role.Identifier.ToString())                {                    info(strFmt(/Name %1/, Duty.Name));                    info(strFmt(/Description %1/, Duty.Description));                    info(strFmt(/AOT Name %1/, Duty.Identifier.ToString()));                    info(strFmt(/Privilege %1/, Duty.Privileges));                }            }        }    }}
 
I have the same question (0)
  • Verified answer
    Martin Dráb Profile Picture
    238,050 Most Valuable Professional on at
    If I understand it correctly, the relevant code is this:
    var dutyRepo = SysSecurity::GetSecurityRepository().Duties;
    var dutyEnumerator = dutyRepo.LoadAll().GetEnumerator();
    
    while (dutyEnumerator.MoveNext())
    {
        Duty duty = dutyEnumerator.Current;
        if (duty.Roles.ToString() == 'DataManagementAdministrator')
        {
            info(strFmt("Name %1", duty.Name));
        }
    }
    The condition for the role name looks wrong to me. You assume that a duty can't be included in several roles, which is not true. You also assume that ToString() method of DutyRoleReferenceCollection class will give you the role name, which may easily be wrong too. You should use Contains() method instead. Like this:
    var dutyRepo = SysSecurity::GetSecurityRepository().Duties;
    var dutyEnumerator = dutyRepo.LoadAll().GetEnumerator();
    
    while (dutyEnumerator.MoveNext())
    {
        Duty duty = dutyEnumerator.Current;
        if (duty.Roles.Contains('DataManagementAdministrator'))
        {
            info(strFmt("Name %1", duty.Name));
        }
    }
    I tried to run it and it looked good to me.
  • Sachin Mittal Profile Picture
    2 on at
    Hi, thanks martin , I have managed to create role , duty , privilege and menu items relation with help of your code only i.e. from https://dev.goshoom.net/2020/02/security-api/  and for now , my code is , I want to add licensees for these menu items too, can you help how can we get those
    using Microsoft.Dynamics.AX.Security.Management.Domain;
    internal final class TestClass
    {
        /// <summary>
        /// Class entry point. The system will call this method when a designated menu
        /// is selected or when execution starts and this class is set as the startup class.
        /// </summary>
        /// <param name = "_args">The specified arguments.</param>
        public static void main(Args _args)
        {
            var RolesRepo       = SysSecurity::GetSecurityRepository().Roles;
            var RolesEnumerator = RolesRepo.LoadAll().GetEnumerator();
    
            var DutyRepo        = SysSecurity::GetSecurityRepository().Duties;
            var DutyEnumerator  = DutyRepo.LoadAll().GetEnumerator();
    
            var PrivilegeRepo       = SysSecurity::GetSecurityRepository().Privileges;
            var PrivilegeEnumerator = PrivilegeRepo.LoadAll().GetEnumerator();
    
            while (RolesEnumerator.MoveNext())
            {
                Role Role = RolesEnumerator.Current;
                if(Role.Name == 'Data management administrator')
                {
                    info(strFmt("Role Name %1", Role.Name));
    
                    var Duty1Enumerator = Role.Duties.GetEnumerator();
    
                    while(Duty1Enumerator.MoveNext())
                    {
                        Duty DutyRole =    Duty1Enumerator.Current;
                        info(strFmt("Duty Name %1", DutyRole.Name));
    
                        var PrivelegeEnumerator = DutyRole.Privileges.GetEnumerator();
    
                        while(PrivelegeEnumerator.MoveNext() )
                        {
                            Privilege privilege1 =   PrivelegeEnumerator.Current ;
                            info(strFmt("Privileges Name %1", privilege1.Name));
                            setPrefix(strFmt("Privileges Name %1", privilege1.Name));
    
                            var ActionEnumerator = privilege1.ActionMenuItemGrants.GetEnumerator();
                            while (ActionEnumerator.MoveNext())
                            {
                                ActionMenuItemGrant grant = ActionEnumerator.Current;
                                info(strFmt("Action Menu item name%1", grant.Name));
                             
                            }
    
                            var DisplayEnumerator = privilege1.DisplayMenuItemGrants.GetEnumerator();
                            while (DisplayEnumerator.MoveNext())
                            {
                                DisplayMenuItemGrant grant= DisplayEnumerator.Current;
                                info(strFmt("Display Menu item name%1", grant.Name));
                            }       
    
                            var OutputEnumerator = privilege1.OutputMenuItemGrants.GetEnumerator();
                            while (OutputEnumerator.MoveNext())
                            {
                                OutputMenuItemGrant grant = OutputEnumerator.Current;
                                info(strFmt("Output Menu item name%1", grant.Name));
                            }
                        }
                    }
                }
               
            }
        }
    }
     
  • Martin Dráb Profile Picture
    238,050 Most Valuable Professional on at
    This thread is about How get Associated Duty name for roles using SysSecurity, therefore the new question about licenses doesn't belong here. Please create a new thread for that.
     
    If your question has been answered, please verify it by Does this answer your question?. If it doesn't work, I can try it on your behalf.
  • Sachin Mittal Profile Picture
    2 on at
    @Martin solution, answers this question, Thanks

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

News and Announcements

Season of Giving Solutions is Here!

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Abhilash Warrier Profile Picture

Abhilash Warrier 678 Super User 2025 Season 2

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 407 Super User 2025 Season 2

#3
Martin Dráb Profile Picture

Martin Dráb 283 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans