CRM 2011 查找并判断用户是否有该角色
注意:用户需要安全角色读的权限,
function onLoad() { CurrentUserHasRole('系统管理员'); //有则是TRUE } //功能说明: 获取当前用户是否具有该角色 //备注: //参数说明: // roleName: 角色名称 //返回值说明(return): // bool: function CurrentUserHasRole(roleName) { //为了避免OData中文检索不出这种问题,使用encodeURIComponent来encode; var serverUrl = Xrm.Page.context.getClientUrl(); var curUserId = Xrm.Page.context.getUserId(); var retrieveEntityReq = RetrieveEntityRecordByGuid("SystemUser", curUserId); retrieveEntityReq.send(); if (retrieveEntityReq.status == 200) { var retrieved = JSON.parse(retrieveEntityReq.responseText).d; var oDataEndpointUrl = serverUrl + "/XRMServices/2011/OrganizationData.svc/"; oDataEndpointUrl += "RoleSet?$filter=Name eq " + "'" + encodeURIComponent(roleName) + "'" + " and BusinessUnitId/Id eq (guid'" + retrieved.BusinessUnitId.Id + "')"; var service = new XMLHttpRequest(); if (service != null) { service.open("GET", oDataEndpointUrl, false); service.setRequestHeader("Accept", "application/json"); service.setRequestHeader("Content-Type", "application/json; charset=utf-8"); service.send(null); var requestResults = eval('(' + service.responseText + ')').d; if (requestResults != null && requestResults.results.length == 1) { var role = requestResults.results[0]; var id = role.RoleId; var currentUserRoles = Xrm.Page.context.getUserRoles(); for (var i = 0; i < currentUserRoles.length; i++) { var userRole = currentUserRoles[i]; if (GuidsAreEqual(userRole, id)) { return true; } } } } return false; } } //功能说明: 比较两个Guid //备注: //参数说明(Parameters): // guid1:Guid // guid2: Guid //返回值说明(return): // bool: 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; }
添加OnLoad方法时需选择 传递参数