Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Get Security role of user using JScript

Posted on by 275

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

  • Suggested answer
    Forrest Zhang Profile Picture
    Forrest Zhang 18 on at
    Get Security role of user using JScript
    This article may be helpful for you.
     
  • a33ik Profile Picture
    a33ik 84,321 Most Valuable Professional on at
    RE: Get Security role of user using JScript

    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
    Ben Fishin Profile Picture
    Ben Fishin on at
    RE: Get Security role of user using JScript

    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

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Get Security role of user using JScript

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

  • Turbo Forms Profile Picture
    Turbo Forms 275 on at
    RE: Get Security role of user using JScript

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

  • Verified answer
    Jeevarajan Kumar Profile Picture
    Jeevarajan Kumar 747 Most Valuable Professional on at
    RE: Get Security role of user using JScript

    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.

  • Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: Get Security role of user using JScript

    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.

  • Verified answer
    a33ik Profile Picture
    a33ik 84,321 Most Valuable Professional on at
    RE: Get Security role of user using JScript

    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

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... 290,902 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,316 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans