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)

Retrieving login users security roles

(0) ShareShare
ReportReport
Posted on by

Hello everyone,

I have a requirement wherein i have to retrieve the logged in users security roles. Some of the users have multiple roles. 

The code i have used is,

function test() {

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

    alert(UserRole);

   

    for (var i = 0; i < UserRole.length; i++)

        alert("Hi");

    {

        var userRoleId = UserRole[i];

        alert("Hi");

 

        var req = new XMLHttpRequest();

        req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/roles?$select=name&$filter=roleid eq " + userRoleId, true);

But I am not able to get the desired output. I am getting as "Bad Request"error and "undefined" error.

If you could please help!

 

*This post is locked for comments

I have the same question (0)
  • Alessandro Graps Profile Picture
    2,664 on at

    Hi,

    did you try this way?

    blogs.infinite-x.net/.../retreiving-user-roles-in-crm-2011

    thanks

    A.G.

  • Community Member Profile Picture
    on at

    Hi,

    this function will get all the user's security role Id, passing security role id to get the security role name. If it match, it is true otherwise false.

    function CheckUserRole(roleName) {

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

       for (var i = 0; i < currentUserRoles.length; i++) {

           var userRoleId = currentUserRoles[i];

            var userRoleName = GetRoleName(userRoleId);

            if (userRoleName == roleName) {

               return true;

            }

       }

       return false;

    }

    Please mark verified if this is answer for your query.

    Regards

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi,

    Please check whether your are getting the guid in  userRoleId  by putting alert(userRoleId ), I think that will provide you the object if yes then you will get the role id like userRoleId.id or userRoleId.Id

  • Community Member Profile Picture
    on at

    Hi Mohit,

    I am getting the guid id in userRoleID if I hardcore the value like 0,1 but when I pass i as a value it is showing me undefined error and then the bad request error comes at the end.

    If you could please help me!!!

  • Community Member Profile Picture
    on at

    I have used this code. But this code is not serving my objective. It is showing bad request error if I pass i as a value to userRoleId variable.

  • Community Member Profile Picture
    on at

    Hi,

    function onLoad() {

    // ** Shows all the roles for current user

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

    for (var i = 0; i < Roles.length; i++) {

    var RoleId = Roles[i];

    var selectQuery = "/RoleSet?$top=1&$filter=RoleId eq guid'" + RoleId + "'&$select=Name";

    var role = null;

    role = makeRequest(selectQuery);

    var RoleName = role[0].Name;

    alert("Role ID=" + RoleId + " Role Name=" + RoleName);

    }

    }

  • Verified answer
    Rushikesh Bhujbal Profile Picture
    285 on at

    Hi Darshani

    Try changing

    req.open("GET", window.parent.Xrm.Page.context.getClientUrl() + "/api/data/v8.0/roles?$select=name&$filter=roleid eq " + userRoleId, true);

    to

    req.open("GET", window.parent.Xrm.Page.context.getClientUrl() + "/api/data/v8.0/roles?$select=name&$filter=roleid eq " + userRoleId, false);

    change "async" from "true" to "false".

    No such errors in your code. Try to replcae your function with below code.

    function test() {

       var UserRole = window.parent.Xrm.Page.context.getUserRoles();

       for (var i = 0; i < UserRole.length; i++) {

            var req = new XMLHttpRequest();

           var userRoleId = UserRole[i];

           req.open("GET", window.parent.Xrm.Page.context.getClientUrl() + "/api/data/v8.0/roles?$select=name&$filter=roleid eq " + userRoleId, false);

           req.setRequestHeader("Accept", "application/json");

           req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

           req.onreadystatechange = function () {

               if (req.readyState == 4 && req.status == 200) {

                   console.log("Hello "+i);

               }

           };

           req.send();

       }

    }

  • Community Member Profile Picture
    on at

    Thank you so much!!! My code is working now.

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