Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)
Answered

Retrieving Login user security Roles

Posted on by 5

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

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

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

  • Verified answer
    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




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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Complete!

🔔 Be sure to subscribe to the new forums you are interested in to stay up to date! 🔔

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,134 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,928 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans