locked
Enabling a Ribbon button only for System Administrator. RRS feed

  • Question

  • Hi,

    I am trying to Enable  the activate and deactivate button in CRM only for System Amin role and not for other roles. I have written the javascript and also made the necessary cganges in the XML also. This works fine in our environment  but is giving an error on our production environment( which is a different server) while saving any record. The record gets saved but gives an error as Access is denied.

    I have pasted the code below. I get the alert A but not Alet B. What could be the problem? Any help would be useful.

    function callMain()
    {
    //debugger;
    //alert("requestResults.results.length")

     var objrole = "System Administrator";
      var rolestatus = UserHasRole(objrole);
      if (rolestatus) {
            return true;
        }
        else {
            return false;
        }

    }

    function UserHasRole(roleName) {

        var serverUrl = Xrm.Page.context.getServerUrl();
        var oDataEndpointUrl = serverUrl + "/XRMServices/2011/OrganizationData.svc/";     
    //    oDataEndpointUrl += "RoleSet?$top=1&$filter=Name eq '" + roleName + "'";
        oDataEndpointUrl += "RoleSet?$filter=Name eq '" + roleName + "'";

        var service = GetRequestObject();

        if (service != null) {

           alert("A");

            service.open("GET", oDataEndpointUrl, false);

           alert("B");

            service.setRequestHeader("X-Requested-Width", "XMLHttpRequest");

            service.setRequestHeader("Accept", "application/json, text/javascript, */*");
            service.send(null);

           var requestResults = eval('(' + service.responseText + ')').d;

    //alert(requestResults.results.length)
          if (requestResults != null) {
                for (var s = 0; s < requestResults.results.length; s++) {
                var role = requestResults.results[s];
                var id = role.RoleId;
     // var name= role.Name;
    //alert("Executive role name" + id)

                var currentUserRoles = Xrm.Page.context.getUserRoles();
    //alert("Current user roles role id " + currentUserRoles)
              for (var m = 0; m < currentUserRoles.length; m++) {
                     var userRole = currentUserRoles[m];
    //alert("Current user roles userRole " + userRole )

                     if (GuidsAreEqual(userRole, id)) {
                              return true;
                     }
                  } //for loop of current user roles end here
              }//for loop of executive roles end here
            }
        }

        return false;
    }

    function GetRequestObject() {
        if (window.XMLHttpRequest) {
            return new window.XMLHttpRequest;
        }
        else {
            try {
                return new ActiveXObject("MSXML2.XMLHTTP.3.0");
            }
            catch (ex) {
                return null;
            }
        }
    }
    function GuidsAreEqual(guid1, guid2) {
        var isEqual = false;
        if (guid1 == null || guid2 == null) {
            isEqual = false;
        }
        else {
            isEqual = guid1.replace(/[{}]/g, "").toLowerCase() == guid2.replace(/[{}]/g, "").toLowerCase();
        }

        return isEqual;
    }

    Thanks in Advance,

    Friday, January 13, 2012 6:33 AM

Answers

All replies