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 :
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

I have the same question (0)
  • Suggested answer
    Dynamics_Alok Profile Picture
    1,746 on at

    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 

  • Kishor Kumar Profile Picture
    3,710 on at

    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

  • Kishor Kumar Profile Picture
    3,710 on at

    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?

  • Community Member Profile Picture
    on at

    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

  • Verified answer
    Community Member Profile Picture
    on at

    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();
    }


  • Verified answer
    Kishor Kumar Profile Picture
    3,710 on at

    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);
    
    }


  • Kishor Kumar Profile Picture
    3,710 on at

    Thanks for the replies.

  • Kishor Kumar Profile Picture
    3,710 on at
    
    

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!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
JS-09031509-0 Profile Picture

JS-09031509-0 3

#2
AS-17030037-0 Profile Picture

AS-17030037-0 2

#2
Mark Eckert Profile Picture

Mark Eckert 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans