locked
Check multiple roles and remove optionset values RRS feed

  • Question

  • Hi

    I have written a script to check the user roles and remove an optionset value if the user is not assigned the System administrator role.

    This script works 100% however if a user has mutiple roles, the script does not appear to function correctly. If the user has 5 roles and 1 role is System Administrator, the values should be available however this is not the case.

    Please find herewith the script:

    function hideAdHocOptionset() {
        debugger;
        var Roles = Xrm.Page.context.getUserRoles();
        for (var i = 0; i < Roles.length; i++) {
            var RoleId = Roles[i];
            var req = new XMLHttpRequest();
            req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/roles(" + RoleId + ")?$select=name", false);
            req.setRequestHeader("OData-MaxVersion", "4.0");
            req.setRequestHeader("OData-Version", "4.0");
            req.setRequestHeader("Accept", "application/json");
            req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
            req.onreadystatechange = function () {
                if (this.readyState === 4) {
                    req.onreadystatechange = null;
                    if (this.status === 200) {
                        var result = JSON.parse(this.response);
                        var roleName = result["name"];
                        //alert(roleName);
                        if (roleName != "System Administrator") {
                            Xrm.Page.getControl("new_customertype").removeOption(100000012);
                            Xrm.Page.getControl("new_customertype").removeOption(100000018);
                        }
                        else if (roleName == 'System Administrator') {
                            Xrm.Page.getControl("new_customertype").addOption(100000012);
                            Xrm.Page.getControl("new_customertype").addOption(100000018);
                        }
                    }
                    else {
                        Xrm.Utility.alertDialog(this.statusText);
                    }
                }
            };
            req.send();
        }
    }

    Any suggestions would be greatly appreciated.

    Regards
    Tony


    Monday, September 18, 2017 9:36 AM

All replies

  • It would seem that I need to create an array and compare against the array which includes all of the roles that has been assigned to the user.

    I tried the following without success.

    function hideAdHocOptionset() {
        debugger;
        var Roles = Xrm.Page.context.getUserRoles();
        for (var i = 0; i < Roles.length; i++) {
            var roleId = Roles[i];
            var req = new XMLHttpRequest();
            req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/roles?$select=name&$filter=roleid eq" + roleId + "", true);
            req.setRequestHeader("OData-MaxVersion", "4.0");
            req.setRequestHeader("OData-Version", "4.0");
            req.setRequestHeader("Accept", "application/json");
            req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
            req.onreadystatechange = function () {
                if (this.readyState === 4) {
                    req.onreadystatechange = null;
                    if (this.status === 200) {
                        //var result = JSON.parse(this.response);
                        var results = JSON.parse(this.response);
                        for (var i = 0; i < results.value.length; i++) {
                            var roleName = results.value[i]["name"];
                            //var roleName = result["name"];
                           // alert(roleName);
                            if (roleName != "System Administrator") {
                                Xrm.Page.getControl("new_customertype").removeOption(100000012);
                                Xrm.Page.getControl("new_customertype").removeOption(100000018);
                            }
                            else if (roleName == 'System Administrator') {
                                Xrm.Page.getControl("new_customertype").addOption(100000012);
                                Xrm.Page.getControl("new_customertype").addOption(100000018);
                            }
                        }
                    }
                    else {
                        Xrm.Utility.alertDialog(this.statusText);
                    }
                }
            };
            req.send();
        }
    }
    I would really appreciate any suggestions. Thanks!

    Monday, September 18, 2017 10:44 AM
  • I am now trying the following script:

    function hideAdHocOptionset() {
        debugger;
        var currentUserRoles = Xrm.Page.context.getUserRoles();
        for (var i = 0; i < currentUserRoles.length; i++) {
            var userRoleId = currentUserRoles[i];
            var userRoleName = GetRoleName(userRoleId);
            if (userRoleName != "System Administrator") {
                Xrm.Page.getControl("new_customertype").removeOption(100000012);
                Xrm.Page.getControl("new_customertype").removeOption(100000018);
            }
        }
    }
    
    function GetRoleName(userRoleId) {
        var Roles = Xrm.Page.context.getUserRoles();
        for (var i = 0; i < Roles.length; i++) {
            var RoleId = Roles[i];
            var req = new XMLHttpRequest();
            req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/roles(" + RoleId + ")?$select=name", false);
            req.setRequestHeader("OData-MaxVersion", "4.0");
            req.setRequestHeader("OData-Version", "4.0");
            req.setRequestHeader("Accept", "application/json");
            req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
            req.onreadystatechange = function () {
                if (this.readyState === 4) {
                    req.onreadystatechange = null;
                    if (this.status === 200) {
                        var result = JSON.parse(this.response);
                        var roleName = result["name"];
                    }
                    else {
                        Xrm.Utility.alertDialog(this.statusText);
                    }
                }
            };
            req.send();
        }
    }

    Again this works for the situation where a user has a single role but fails if the user has multiple roles - really appreciate any suggestions.

    Thanks
    Tony

    Monday, September 18, 2017 3:18 PM
  • Hello Tony, 

    Can you try this code using the library crmfetchkit. I didn't verify but I think it works.

    // use the library crmFecthkit
    function hideAdHocoptionset(){
    	 var Roles = Xrm.Page.context.getUserRoles();
        
    	var fetchxml =  @"<fetch version='1.0'>" +
    		"  <entity name='roles'>"+
    		"    <attribute name='name' />"+
    		"   <filter type="or">";
    	for (var i = 0; i < Roles.length; i++) {
    		fetchxml += "     <condition attribute='roleId' operator='eq' value='" + roleId + "' />";
    	}
    	
    	fetchxml += "   </filter>"+
    	" </entity>"+
    	"</fetch>";
    
    	var entities = CrmFetchKit.FetchSync(fetchxml);
    
    	for(var i = 0, max = entities.length; i < max; i++) {
    		var roleName = entities["name"];
    		if (roleName != "System Administrator") {
    			Xrm.Page.getControl("new_customertype").removeOption(100000012);
    			Xrm.Page.getControl("new_customertype").removeOption(100000018);
    		}
    		else if (roleName == 'System Administrator') {
    			Xrm.Page.getControl("new_customertype").addOption(100000012);
    			Xrm.Page.getControl("new_customertype").addOption(100000018);
    		}
    	}
    }

    I hope that it can help you.

    Kind Regards, 

    Moh

    • Proposed as answer by Moh Helper Thursday, September 21, 2017 12:07 PM
    Thursday, September 21, 2017 12:07 PM