Skip to main content
Post a question

Notifications

Community site session details

Community site session details

Session Id : M8kTq8cHZzy1MRdVKkZ/E7
Microsoft Dynamics CRM (Archived)

Get Security role of user using JScript

Like (0) ShareShare
ReportReport
Posted on 27 Sep 2017 14:16:15 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.

  1. function FormOnLoad()
  2. {
  3. var optCtrl = Xrm.Page.ui.controls.get("statuscode");
  4. var CMRole = CheckUserRole("System Administrator");
  5.  
  6. if (Xrm.Page.ui.getFormType() == 6 && !CMRole) {
  7. optCtrl.removeOption(100000000);
  8. }
  9. }
  10.  
  11.  
  12. function GetServerUrl() {
  13. return Xrm.Page.context.getClientUrl() + "/xrmservices/2011/organizationdata.svc/";
  14. }
  15.  
  16.  
  17. function CheckUserRole(roleName) {
  18. var currentUserRoles = Xrm.Page.context.getUserRoles();
  19. for (var i = 0; i < currentUserRoles.length; i++) {
  20. var userRoleId = currentUserRoles[i];
  21. var userRoleName = GetRoleName(userRoleId);
  22. if (userRoleName == roleName) {
  23. return true;
  24. }
  25. }
  26. return false;
  27. }
  28.  
  29. function GetRoleName(userRoleId) {
  30. var selectQuery = "RoleSet?$top=1&$filter=RoleId eq guid'" + userRoleId + "'&$select=Name";
  31. var odataSelect = GetServerUrl() + selectQuery;
  32. //alert(odataSelect);
  33. var roleName = null;
  34. $.ajax({
  35. type: "GET",
  36. async: false,
  37. contentType: "application/json; charset=utf-8",
  38. datatype: "json",
  39. url: odataSelect,
  40. beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
  41. success: function (data, textStatus, XmlHttpRequest) {
  42. var result = data.d;
  43. if (!!result) {
  44. roleName = result.results[0].Name;
  45. }
  46. },
  47. error: function (XmlHttpRequest, textStatus, errorThrown) {
  48. //alert('OData Select Failed: ' + odataSelect);
  49. }
  50. });
  51. return roleName;
  52. }


Error Thrown is:

jscript.jpg

*This post is locked for comments

  • Suggested answer
    Forrest Zhang Profile Picture
    18 on 25 Oct 2024 at 04:35:07
    Get Security role of user using JScript
    This article may be helpful for you.
     
  • a33ik Profile Picture
    84,329 Most Valuable Professional on 20 Apr 2020 at 18:43:15
    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
    on 20 Apr 2020 at 18:36:10
    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
    on 28 Jun 2019 at 13:40:46
    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
    277 on 27 Sep 2017 at 15:15:18
    RE: Get Security role of user using JScript

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

  • Verified answer
    Jeevarajan Kumar Profile Picture
    747 Most Valuable Professional on 27 Sep 2017 at 15:10:51
    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

    1. if (typeof($) === 'undefined') {
    2. $ = parent.$;
    3. jQuery = parent.jQuery;
    4. }

    Hope it helps.

  • Aric Levin Profile Picture
    30,188 Moderator on 27 Sep 2017 at 14:34:18
    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
    84,329 Most Valuable Professional on 27 Sep 2017 at 14:29:55
    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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,333 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,276 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans
Loading complete