locked
How to hide a particular field some user based on the roles they have RRS feed

  • Question

  • I wanna hide a field for some users based on their roles  in crm 2011.I have written some code to hide the field (approvalstatus) for the user admin.

    It fetches the logged user role but hiding is not working.Pls find the code snippet.pls give some suggestion

    var command = Xrm.Page.context.getUserId();
    alert(command);

    var role = Xrm.Page.context.getUserRoles();
    alert(role);


    if((role =="f4d7c915-003e-e111-bbe6-005056a17301")
    {
    Xrm.Page.ui.controls.get("new_approvalstatus").setVisible(true);

    else
    Xrm.Page.getControl("new_approvalstatus").setVisible(false);

    }

    Thanks in advance,

    Syed ibrahim Rowthar


    Friday, February 24, 2012 8:36 PM

Answers

  • Hi 

    Field level security is available in CRM 2011 now. Go through the followin link:

    http://www.powerobjects.com/blog/2010/10/08/field-level-security-out-of-box-in-microsoft-dynamics-crm-2011/ 

    If you want to achieve this through javascript. Here is the link. It retrieves the user role. Based on the user role all u need is to hide the field

    http://jianwang.blogspot.com/2008/01/crm-40-check-current-users-security.html

    Reply if you need more details

    Friday, February 24, 2012 8:42 PM
  • Hello,

    Actually getUserRoles method returns array of roles. That's why it doesn't work for you. Check following article - it provides you possibility to compare roles by name and not by id's - http://blogs.infinite-x.net/2010/11/16/retreiving-user-roles-in-crm-2011/

    One more thing that I've noticed you have an error in your code - it is in bold:

    if((role =="f4d7c915-003e-e111-bbe6-005056a17301")

    2 brackets that are opened and only one that is closed.


    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
    Follow Andriy on Twitter


    Friday, February 24, 2012 9:05 PM
    Moderator
  • TRY THIS.

    var hasRequiredRole = false;

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

    for (var i = 0; i < roles.length; i++)
    {
      var userRole = roles.[i];
      if (userRole.replace(/[{}]/g, "").toLowerCase() == "f4d7c915-003e-e111-bbe6-005056a17301" )
      {
          hasRequiredRole = true;
      }
    }

    if (hasRequiredRole == true)

    {

    Xrm.Page.ui.controls.get("new_approvalstatus").setVisible(true);

    }

    else

    {

    Xrm.Page.getControl("new_approvalstatus").setVisible(false);

    }


    Regards, Kyaw Kyaw Tun

    Friday, March 2, 2012 2:47 AM
  • Hi Syed,

    I've modified your code a bit. Also I've turned it into a function so you can copy this into a web resource and then call the function on your form:

    //hides a field if the user has a role (entered as the GUID)
    function showFieldIfUserHasRole(fieldName, roleId) {
        var hasRequiredRole = userHasRole(roleId);
        Xrm.Page.getControl(fieldName).setVisible(hasRequiredRole);
    }
    
    //returns true if the user has the role (entered as the GUID)
    function userHasRole(roleId) {
        var userRoles = Xrm.Page.context.getUserRoles();
        for (var i = 0; i < userRoles.length; i++) {
            var currentRole = userRoles[i];
            if (currentRole.toLowerCase() == roleId.toLowerCase()) {
                return true;
            }
        }
        return false;
    }

    Just call the showFieldIfUserHasRole function on your form, and pass in the field name and role id as follows:

    "name", "B00E2A05-0F1E-E111-B9DA-00155D04DC01"

    Note that this can be used for other fields or roles by editing the parameters. 

    I also made the second part a separate function so you can use that for performing other tasks other than just showing a field.

    Hope this helps!

    -Paul

    Friday, March 2, 2012 10:52 AM
  • var hasRequiredRole = false;

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

    for (var i = 0; i < roles.length; i++)
    {
      var userRole = roles[i];
      if (userRole.replace(/[{}]/g, "").toLowerCase() == "f4d7c915-003e-e111-bbe6-005056a17301" )
      {
          hasRequiredRole = true;
      }
    }

    if (hasRequiredRole == true)

    {

    Xrm.Page.ui.controls.get("new_approvalstatus").setVisible(true);

    }

    else

    {

    Xrm.Page.getControl("new_approvalstatus").setVisible(false);

    }

    But the above method is not a good one because we hardcoded the role it.

    The better method is passing the Role Name and checking the user.

    FYI

    http://blogs.infinite-x.net/2010/11/16/retreiving-user-roles-in-crm-2011/



    Regards, Kyaw Kyaw Tun

    Tuesday, March 6, 2012 10:07 PM

All replies

  • Hi 

    Field level security is available in CRM 2011 now. Go through the followin link:

    http://www.powerobjects.com/blog/2010/10/08/field-level-security-out-of-box-in-microsoft-dynamics-crm-2011/ 

    If you want to achieve this through javascript. Here is the link. It retrieves the user role. Based on the user role all u need is to hide the field

    http://jianwang.blogspot.com/2008/01/crm-40-check-current-users-security.html

    Reply if you need more details

    Friday, February 24, 2012 8:42 PM
  • Hello,

    Actually getUserRoles method returns array of roles. That's why it doesn't work for you. Check following article - it provides you possibility to compare roles by name and not by id's - http://blogs.infinite-x.net/2010/11/16/retreiving-user-roles-in-crm-2011/

    One more thing that I've noticed you have an error in your code - it is in bold:

    if((role =="f4d7c915-003e-e111-bbe6-005056a17301")

    2 brackets that are opened and only one that is closed.


    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
    Follow Andriy on Twitter


    Friday, February 24, 2012 9:05 PM
    Moderator
  • Hi,

    Even after the correction also its not working.Can u pls give me exact code snippet for the scenario.

    Thursday, March 1, 2012 8:57 PM
  • Hi,

    Even after the correction also its not working.Can u pls give me exact code snippet for the scenario.


    Provide the code you use.

    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
    Follow Andriy on Twitter

    Thursday, March 1, 2012 9:14 PM
    Moderator
  • TRY THIS.

    var hasRequiredRole = false;

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

    for (var i = 0; i < roles.length; i++)
    {
      var userRole = roles.[i];
      if (userRole.replace(/[{}]/g, "").toLowerCase() == "f4d7c915-003e-e111-bbe6-005056a17301" )
      {
          hasRequiredRole = true;
      }
    }

    if (hasRequiredRole == true)

    {

    Xrm.Page.ui.controls.get("new_approvalstatus").setVisible(true);

    }

    else

    {

    Xrm.Page.getControl("new_approvalstatus").setVisible(false);

    }


    Regards, Kyaw Kyaw Tun

    Friday, March 2, 2012 2:47 AM
  • Hi Syed,

    I've modified your code a bit. Also I've turned it into a function so you can copy this into a web resource and then call the function on your form:

    //hides a field if the user has a role (entered as the GUID)
    function showFieldIfUserHasRole(fieldName, roleId) {
        var hasRequiredRole = userHasRole(roleId);
        Xrm.Page.getControl(fieldName).setVisible(hasRequiredRole);
    }
    
    //returns true if the user has the role (entered as the GUID)
    function userHasRole(roleId) {
        var userRoles = Xrm.Page.context.getUserRoles();
        for (var i = 0; i < userRoles.length; i++) {
            var currentRole = userRoles[i];
            if (currentRole.toLowerCase() == roleId.toLowerCase()) {
                return true;
            }
        }
        return false;
    }

    Just call the showFieldIfUserHasRole function on your form, and pass in the field name and role id as follows:

    "name", "B00E2A05-0F1E-E111-B9DA-00155D04DC01"

    Note that this can be used for other fields or roles by editing the parameters. 

    I also made the second part a separate function so you can use that for performing other tasks other than just showing a field.

    Hope this helps!

    -Paul

    Friday, March 2, 2012 10:52 AM
  • Hi Kyaw Kyaw Tun,

    I have tried this but its not working.I copied the entire code in the form onload event and tried but its not working.Not even fetching the user id too.

    Regards,

    Syed 

    Tuesday, March 6, 2012 6:34 PM
  • var hasRequiredRole = false;

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

    for (var i = 0; i < roles.length; i++)
    {
      var userRole = roles[i];
      if (userRole.replace(/[{}]/g, "").toLowerCase() == "f4d7c915-003e-e111-bbe6-005056a17301" )
      {
          hasRequiredRole = true;
      }
    }

    if (hasRequiredRole == true)

    {

    Xrm.Page.ui.controls.get("new_approvalstatus").setVisible(true);

    }

    else

    {

    Xrm.Page.getControl("new_approvalstatus").setVisible(false);

    }

    But the above method is not a good one because we hardcoded the role it.

    The better method is passing the Role Name and checking the user.

    FYI

    http://blogs.infinite-x.net/2010/11/16/retreiving-user-roles-in-crm-2011/



    Regards, Kyaw Kyaw Tun

    Tuesday, March 6, 2012 10:07 PM