locked
Hide field to non-Administrators RRS feed

  • Question

  • Dear all,

    I tried to google the answer to my question but seems it is rather difficult to find a "simple" the js code to do what I need: I want to hide my "new_field" field to any non-Administrator user.

    Seems the way is to get the current user role and - if it is not "Administrator" - hide the field.

    However I found lots of js codes which I am not able to customize duly...

    Can you pls help?!

     

    Thanks a lot,

    Gian Paolo

    Friday, May 28, 2010 12:14 PM

Answers

  • FOUND THE SOLUTION, HOPE IT HELPS

    function getUserId()
    {
    try
    {
    var command = new RemoteCommand("SystemUser", "WhoAmI", "/MSCRMServices/");
    var oResult = command.Execute();

    if (oResult.Success)
    {
    return oResult.ReturnValue.UserId;
    }
    }
    catch(e)
    {
    alert("Error while retrieving userid.");
    }
    return null;
    }

    function getUserRoles(userId)
    {
    try
    {
    var command = new RemoteCommand("UserManager", "GetUserRoles");
    command.SetParameter("userIds", "<guid>" + userId + "</guid>");

    var oResult = command.Execute();

    if (oResult.Success)
    {
    return oResult.ReturnValue;
    }
    }
    catch(e)
    {
    alert("Error while retrieving roles.");
    }
    return null;
    }

    function getUserId()
    {
    try
    {
    var command = new RemoteCommand("SystemUser", "WhoAmI", "/MSCRMServices/");
    var oResult = command.Execute();

    if (oResult.Success)
    {
    return oResult.ReturnValue.UserId;
    }
    }
    catch(e)
    {
    alert("Error while retrieving userid.");
    }
    return null;
    }

    function getUserRoles(userId)
    {
    try
    {
    var command = new RemoteCommand("UserManager", "GetUserRoles");
    command.SetParameter("userIds", "<guid>" + userId + "</guid>");

    var oResult = command.Execute();

    if (oResult.Success)
    {
    return oResult.ReturnValue;
    }
    }
    catch(e)
    {
    alert("Error while retrieving roles.");
    }
    return null;
    }

    function userHasRole(userId, roleName)
    {
    result = getUserRoles(userId);
    if (result != null)
    {
    var oXml = new ActiveXObject("Microsoft.XMLDOM");
    oXml.resolveExternals = false;
    oXml.async = false;
    oXml.loadXML(result);

    roleNode = oXml.selectSingleNode("/roles/role[name='" + roleName + "']");
    if (roleNode != null)
    {
    if (roleNode.selectSingleNode("roleid[@checked='true']") != null)
    return true;
    }
    }

    return false;
    }

    function currentUserHasRole(roleName)
    {
    userId = getUserId();
    return userHasRole(userId, roleName);
    }

    if(currentUserHasRole('NAME OF THE ROLE ')){
    crmForm.all.new_field .parentElement.parentElement.parentElement.style.display = '';
    }else{
    crmForm.all.new_field .parentElement.parentElement.parentElement.style.display = 'none';
    }
    Friday, May 28, 2010 12:26 PM

All replies

  • FOUND THE SOLUTION, HOPE IT HELPS

    function getUserId()
    {
    try
    {
    var command = new RemoteCommand("SystemUser", "WhoAmI", "/MSCRMServices/");
    var oResult = command.Execute();

    if (oResult.Success)
    {
    return oResult.ReturnValue.UserId;
    }
    }
    catch(e)
    {
    alert("Error while retrieving userid.");
    }
    return null;
    }

    function getUserRoles(userId)
    {
    try
    {
    var command = new RemoteCommand("UserManager", "GetUserRoles");
    command.SetParameter("userIds", "<guid>" + userId + "</guid>");

    var oResult = command.Execute();

    if (oResult.Success)
    {
    return oResult.ReturnValue;
    }
    }
    catch(e)
    {
    alert("Error while retrieving roles.");
    }
    return null;
    }

    function getUserId()
    {
    try
    {
    var command = new RemoteCommand("SystemUser", "WhoAmI", "/MSCRMServices/");
    var oResult = command.Execute();

    if (oResult.Success)
    {
    return oResult.ReturnValue.UserId;
    }
    }
    catch(e)
    {
    alert("Error while retrieving userid.");
    }
    return null;
    }

    function getUserRoles(userId)
    {
    try
    {
    var command = new RemoteCommand("UserManager", "GetUserRoles");
    command.SetParameter("userIds", "<guid>" + userId + "</guid>");

    var oResult = command.Execute();

    if (oResult.Success)
    {
    return oResult.ReturnValue;
    }
    }
    catch(e)
    {
    alert("Error while retrieving roles.");
    }
    return null;
    }

    function userHasRole(userId, roleName)
    {
    result = getUserRoles(userId);
    if (result != null)
    {
    var oXml = new ActiveXObject("Microsoft.XMLDOM");
    oXml.resolveExternals = false;
    oXml.async = false;
    oXml.loadXML(result);

    roleNode = oXml.selectSingleNode("/roles/role[name='" + roleName + "']");
    if (roleNode != null)
    {
    if (roleNode.selectSingleNode("roleid[@checked='true']") != null)
    return true;
    }
    }

    return false;
    }

    function currentUserHasRole(roleName)
    {
    userId = getUserId();
    return userHasRole(userId, roleName);
    }

    if(currentUserHasRole('NAME OF THE ROLE ')){
    crmForm.all.new_field .parentElement.parentElement.parentElement.style.display = '';
    }else{
    crmForm.all.new_field .parentElement.parentElement.parentElement.style.display = 'none';
    }
    Friday, May 28, 2010 12:26 PM
  • You could get use of Field Level Security product of Avanade to secure fields of any system or custom entity.
    Monday, May 31, 2010 10:41 AM