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

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)
Answered

Retrieving Login user security Roles

(0) ShareShare
ReportReport
Posted on by 7

Hi,

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 the user 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) only. Using "Xrm.Page.context.getUserRoles()" i am getting all the roles the user have.

How to achieve it, any suggestions?

*This post is locked for comments

I have the same question (0)
  • Verified answer
    PavanKumarGarlapati Profile Picture
    100 on at
    RE: Retrieving Login user security Roles

    Hi,

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

     

    Note: In case my answer helped you. Kindly mark it as verified.

    Warm Regards,

    Pavan Kumar Garlapati

    Follow my Blog: https://pavankumargarlapati.wordpress.com




  • Jini Francis Profile Picture
    7 on at
    RE: Retrieving Login user security Roles

    Thank you Pavan for your prompt response. The above code is working as expected.

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

Responsible AI policies

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

Pallavi Phade – Community Spotlight

We are honored to recognize Pallavi Phade as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Community Member Profile Picture

Community Member 2

#1
UllrSki Profile Picture

UllrSki 2

#3
SC-08081331-0 Profile Picture

SC-08081331-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans