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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

Creating Security roles and assign for user by using code

(0) ShareShare
ReportReport
Posted on by 456

Hi,

 I need to create a security role and i have to assign the role for a user . I have to achieve this by x++ code. 

I am new to AX .

Thanks in advance,

Ram. 

I have the same question (0)
  • André Arnaud de Calavon Profile Picture
    304,182 Super User 2026 Season 1 on at

    Hi Ram,

    Can you elaborate? What is the full the business requirement? What should be the role contents? Where does that information come from? How to know to assign to which user? Do you need to solve both using x++?

  • Ram Kumar Profile Picture
    456 on at

    Hi Andre,

    I need a simple code to create a new role and their duties and privileges and to assign that role to a user. The role name will be anything ,for example : "Project Manager", and the duties and privileges will be some standard duties and privileges like "Approve project timesheets", "Approve project transactions"... and i have to assign this Project Manager role to some user.....that's enough.

    Thanks in advance,

    Ram.

  • Suggested answer
    Ludwig Reinhard Profile Picture
    Microsoft Employee on at

    Good Morning Ram,

    I would recommend that you have a look at the gentlemen's blog who replied you first before doing some coding ;-)

    dynamicspedia.com/

    Andre published a couple of great posts about automatic role assignment.

    Best regards,

    Ludwig

  • Suggested answer
    Mansour Yahya Mohamad Profile Picture
    Microsoft Employee on at

    You may start from the below suggestion:

    public static server void createNewRole(Args _args)

    {

       #AOT

       TreeNode                     treeNode;

       TreeNode                     roleTreeNode;

       TreeNode                     privilegeTreeNode;

       TreeNode                     subPrivilegeTreeNode;

       str         roleName;

       str         privilegeName;

       ;

       roleName = "TestRole";

       privilegeName = "InventItemGroupView";

       treeNode = TreeNode::findNode(#SecRolesPath + '\\' + roleName);

       if(!treeNode)

       {    

           treeNode = TreeNode::findNode(#SecRolesPath);

           if (treeNode != null)

           {

               roleTreeNode = treeNode.AOTadd(roleName);

               if(roleTreeNode != null)

               {

                   privilegeTreeNode = roleTreeNode.AOTfindChild('Privileges');

                   subPrivilegeTreeNode = privilegeTreeNode.AOTfindChild(privilegeName);

                   if(subPrivilegeTreeNode == null)

                   {

                       subPrivilegeTreeNode = privilegeTreeNode.AOTAdd(privilegeName);

                       subPrivilegeTreeNode.AOTcompile();

                       subPrivilegeTreeNode.AOTsave();

                   }  

                   roleTreeNode.AOTcompile();

                   roleTreeNode.AOTsave();

               }

           }

       }

    }

  • André Arnaud de Calavon Profile Picture
    304,182 Super User 2026 Season 1 on at

    Hi Ram,

    You got a code example, but I'm not able to understand the business case here. What is the source requirement for a new role? Who decides to have a new one or maybe change an existing? What is the exact process? I'm asking this as in the current version there is a split between development and the client. In your current AX2012 environment you can actually do thing with direct x++, but not in Dynamics 365 anymore. You also have to be prepared for the future. If you create something now, how to guarantee it will also work in a future version?

    In addition, you also have to take care of several regulations. Who is managing the process? What will happen if you get an EDP audit? Would your solution be prepared to answer questions like: "Does it support a change request process?

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

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

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

#1
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 681

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 446 Super User 2026 Season 1

#3
Syed Haris Shah Profile Picture

Syed Haris Shah 202 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans