Hello
We are doing mass Hide/ unhide of ribbon buttons using JavaScript. Which in turn is effecting our Form load performance, Onload the we don't see any ribbon buttons and the form would be frozen, after a while we see the ribbon buttons then the form is functional. We got a reply to the Microsoft ticket saying, As we are calling javascript on each buttons visibility, The script is taking too long to check between users roles going back and fro. They recommended to declare variables for all security roles and compare later to reduce the retrieval time.
Below is the code that I'm using right now.
function showhideoperation() { debugger; var button = GetOperationUser(); return button; } function GetOperationUser() { debugger; var isUserRole = true; var roles = Xrm.Page.context.getUserRoles(); for (var i = 0; i < roles.length; i++) { var role = GetOperationRoles(roles[i].replace("{", "").replace("}", "")); if (role) return false; } return isUserRole; } function GetOperationRoles(userRoleId) { debugger; var isUserRole = false; var zipcodeFetchXML = ['<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">' + '<entity name="role">' + '<attribute name="name" />' + '<attribute name="businessunitid" />' + '<attribute name="roleid" />' + '<order attribute="name" descending="false" />' + '<filter type="and">' + '<condition attribute="roleid" operator="eq" value="' + userRoleId + '" />' + '</filter>' + '</entity>' + '</fetch>'].join(''); var encodedFetchXML = encodeURIComponent(zipcodeFetchXML); var dcs_zipcodeid var req = new XMLHttpRequest(); req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/roles?fetchXml=" + encodedFetchXML, 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=OData.Community.Display.V1.FormattedValue"); req.onreadystatechange = function() { if (this.readyState === 4) { req.onreadystatechange = null; if (this.status === 200) { var results = JSON.parse(this.response); for (var i = 0; i < results.value.length; i++) { var roles = results.value[i]; //Single Line Text //UniqueIdentifier if (roles['name'].toLowerCase() == "system administrator") { return false; } if (roles['name'].toLowerCase() == "operation 2") { isUserRole = true; } } } else { alert(this.statusText); } } }; req.send(); return isUserRole; }
I'm using the code on multiple buttons of different entities.
I added Entity privilege rule for majority of buttons, but the remaining buttons the only way is to hide using security roles.
*This post is locked for comments