Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

JavaScript User Roles

(0) ShareShare
ReportReport
Posted on by 3,710

Hey,

I want to get the user roles of the current logged user!

Say User has two roles A and B assigned directly via Manage roles options and have some indirect roles say C assigned via that team that he belongs too.

Now the User will have three Roles AB(Directly Assigned )C(Indirectly assigned via- Team).

Now I want to get the roles AB(Directly assigned) and not the C. How to achieve it any suggestions?

*This post is locked for comments

  • Kishor Kumar Profile Picture
    3,710 on at
    RE: JavaScript User Roles
    
    
  • Kishor Kumar Profile Picture
    3,710 on at
    RE: JavaScript User Roles

    Thanks for the replies.

  • Verified answer
    Kishor Kumar Profile Picture
    3,710 on at
    RE: JavaScript User Roles

    Hi Guys, got the solutions for this, so if we want check the Kishor has "Sales Person" in user roles not in his team, you can use this function

    Var KishorRole = XrmServiceToolkit.Soap.IsCurrentUserRole("Sales Person");
    if(KishorRole == true)
    {
    alert(Role assigned via Manage Role is Sales Person);
    
    }
    else{
    alert(Role assigned via Manage Role is not Sales Person);
    
    }


  • Verified answer
    Community Member Profile Picture
    on at
    RE: JavaScript User Roles

    Use this code:

    function CheckUserRole() {
    	var currentUserId = Xrm.Page.context.getUserId();
    	var currentUserRoles = getCurrentUserRoles(currentUserId);
    }
    
    // Get Roles of User only
    function getCurrentUserRoles(currentUserId){
    	var userId = currentUserId.slice(1, -1);
    	var req = new XMLHttpRequest();
    	req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/systemuserrolescollection?$select=roleid&$filter=systemuserid eq "+userId+"", true);
    	req.setRequestHeader("OData-MaxVersion", "4.0");
    	req.setRequestHeader("OData-Version", "4.0");
    	req.setRequestHeader("Accept", "application/json");
    	req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    	req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    	req.onreadystatechange = function() {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 200) {
                var results = JSON.parse(this.response);
    			for (var i = 0; i < results.value.length; i++) {
                    var userRoleId = results.value[i].roleid;
    				var userRoleName = GetRoleName(userRoleId);
                }
            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send();
    }
    
    //Get Rolename based on RoleId
    function GetRoleName(roleId) {
        var req = new XMLHttpRequest();
    	req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/roles("+roleId+")?$select=name", false);
    	req.setRequestHeader("OData-MaxVersion", "4.0");
    	req.setRequestHeader("OData-Version", "4.0");
    	req.setRequestHeader("Accept", "application/json");
    	req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    	req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    	req.onreadystatechange = function() {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 200) {
                var result = JSON.parse(this.response);
                var roleName = result["name"];
    			alert(roleName);
            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send();
    }


  • Community Member Profile Picture
    on at
    RE: JavaScript User Roles

    Hello Kishor,

    you can achieve this using fetchXML,

    something like:

    <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true" >

       <entity name="systemuser" >

           <attribute name="fullname" />

           <order attribute="fullname" descending="false" />

           <filter type="and" >

               <condition attribute="isdisabled" operator="eq" value="0" />

                       </filter>

           <link-entity name="systemuserroles" alias="userrole" from="systemuserid" to="systemuserid" link-type="outer" >

           <attribute name="roleid" />

           </link-entity>

       </entity>

    </fetch>

    Regards,

      David Levins

  • Kishor Kumar Profile Picture
    3,710 on at
    JavaScript User Roles

    Hey, 

    I want to get the user roles of the current logged user! 

    Say User has two roles A and B assigned directly via Manage roles options and have some indirect roles say C assigned via that team that he belongs too.

    Now the User will have three Roles AB(Directly Assigned )C(Indirectly assigned via- Team).

    Now I want to get the roles AB(Directly assigned) and not the C. How to achieve it any suggestions?

  • Kishor Kumar Profile Picture
    3,710 on at
    RE: JavaScript User Roles

    Hi Alok, Thanks for your reply,

    Yeah I want to remove the security roles that assigned via team to user only

    If Kishor was assigned as "Sales Person" &"Marketing Manager" via Manage Roles options and he too have another roles as "Content Manager" via the team he belongs

    too

    So When we get, it will have something like this,

    var roleid = Xrm.Page.context.getUserRoles();

    The Role Id  will be retruning three roles Id, so I want only the role id of "Sales Person" &"Marketing Manager" and not the "Content Manager"

    Suggestions Please

  • Suggested answer
    Dynamics_Alok Profile Picture
    1,746 on at
    RE: JavaScript User Roles

    You can get user roles(A+B) by simply calling  Xrm.Page.context.getUserRoles();

    To get Role C ,you need to fetch (user)associated Team and then fetch Team assigned Roles.

    Ref : https://community.dynamics.com/crm/b/microsoftdynamicscrmxrm/archive/2014/05/15/ms-dynamics-crm-login-user-39-s-security-role-using-javascript 

    https://social.microsoft.com/Forums/en-US/41a96336-e126-413d-84df-db2d91fb3487/get-security-role-name-using-javascript?forum=crmdevelopment 

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

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 54

#3
dkrishna Profile Picture

dkrishna 6

Featured topics

Product updates

Dynamics 365 release plans