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

Notifications

Announcements

No record found.

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

    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

    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…

Neeraj Kumar – Community Spotlight

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

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans