Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Get Security role of user using JScript

(0) ShareShare
ReportReport
Posted on by

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
    Get Security role of user using JScript
    This article may be helpful for you.
     
  • a33ik Profile Picture
    a33ik 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 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 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 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 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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey Pt 2

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans