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

Announcements

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Get Security role of user using JScript

(0) ShareShare
ReportReport
Posted on by 277

Hi Experts

I used a JavaScript to get logged in users security role using a JavaScript on CRM2015, but now we upgraded to CRM2016. And the form is throwing and error. Below is the code I'm using, please advice.

function FormOnLoad()
{
    var optCtrl = Xrm.Page.ui.controls.get("statuscode");
    var CMRole = CheckUserRole("System Administrator");

    if (Xrm.Page.ui.getFormType() == 6 && !CMRole) {
        optCtrl.removeOption(100000000);
      
    }
}


function GetServerUrl() {
    return Xrm.Page.context.getClientUrl() + "/xrmservices/2011/organizationdata.svc/";
}


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

function GetRoleName(userRoleId) {
    var selectQuery = "RoleSet?$top=1&$filter=RoleId eq guid'" + userRoleId + "'&$select=Name";
    var odataSelect = GetServerUrl() + selectQuery;
    //alert(odataSelect);
    var roleName = null;
    $.ajax({
        type: "GET",
        async: false,
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: odataSelect,
        beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
        success: function (data, textStatus, XmlHttpRequest) {
            var result = data.d;
            if (!!result) {
                roleName = result.results[0].Name;
            }
        },
        error: function (XmlHttpRequest, textStatus, errorThrown) {
            //alert('OData Select Failed: ' + odataSelect);
        }
    });
    return roleName;
}


Error Thrown is:

jscript.jpg

*This post is locked for comments

I have the same question (0)
  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    Just curious - have you tried to check issue you experience in search engine? I was able to find an answer less than in 1 minute - community.dynamics.com/.../168698

  • Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    Are you using XRMServiceToolkit?

    If you are, your please check the version of the toolkit you are using. Starting with 2015 Update 1, you will need to use at least version 2.2.1 of the XRMServiceToolkit.

    Hope this helps.

  • Suggested answer
    Jeevarajan Kumar Profile Picture
    781 Most Valuable Professional on at

    Hi, 

    As suggested by Ben, in the attached link by Andrew, we usually use the below code

    if (typeof($) === 'undefined') {
    	$ = parent.$;
    	jQuery = parent.jQuery;
    }

    Hope it helps.

  • Turbo Forms Profile Picture
    277 on at

    Thanks a lot Andrew. it seems to solve the issue.

  • Community Member Profile Picture
    on at

    //you can user this script

    function CheckUserRole(roleName) {
    var currentUserRoles = Xrm.Page.context.getUserRoles();
    for (var i = 0; i < currentUserRoles.length; i++) {
    var userRoleId = currentUserRoles[i];
    var theUserRoleName = GetTheRoleName(userRoleId);
    if (theUserRoleName == roleName) {
    return true;
    }
    //debugger;
    }
    return false;
    }

    function GetRoleName (userRoleId) {

    var roleName = "";
    var columns = ["Name"];
    var fileObj = CrmRestKit.Retrieve("Role", userRoleId, columns, false);
    if (fileObj.responseJSON.d != null) {
    var data = fileObj.responseJSON.d;
    roleName = data.Name;
    }
    return roleName;
    }

  • Suggested answer
    Ben Fishin Profile Picture
    on at

    For better performance, try this pattern instead of a service call to the API...

    community.dynamics.com/.../get-user-security-role-name-from-context-in-dynamics-365-ce-crm

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Ben,

    Cool approach and thanks for heads up but back in 2017 when I answered this question - method you used was not available and was introduced around 2 years ago.

  • Suggested answer
    Forrest Zhang Profile Picture
    20 on at
    This article may be helpful for you.
     

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
Varsha deshpande Profile Picture

Varsha deshpande 5

#2
Ciprian  P Profile Picture

Ciprian P 2

#3
ZS-27011425-0 Profile Picture

ZS-27011425-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans