Loading... <p style="text-indent: 2em;">MSCRM 4.0 里通过客户端编程得到当前用户权限 </p> <p style="text-indent: 2em;"> </p> <table border="0"> <tbody> <tr> <td> <p> </p> <p style="text-indent: 2em;">在MSCRM新闻组里一个常见的问题是:如何通过客户端编程的方式得到当前用户的权限</p> <p style="text-indent: 2em;">下面是转刀客的解决方案:(原文地址:<a href="https://www.samool.com/go/aHR0cDovL3NvY2lhbC5taWNyb3NvZnQuY29tL0ZvcnVtcy96aC1DTi9jcm1jaGluZXNlL3RocmVhZC9mZmJkNGI5Mi03OTdhLTQ4N2MtOWQxMy00MGNjYzZkZTI4YWI=" target="_blank" ><span style="color: #34b115;">http://social.microsoft.com/Forums/zh-CN/crmchinese/thread/ffbd4b92-797a-487c-9d13-40ccc6de28ab</span></a>),此解决方案经过测试,可以达到控制用户的权限。</p> <p style="text-indent: 2em;">加在需要权限的实体ONLOAD事件中</p> <p style="text-indent: 2em;">var newName = document.getElementById("crmFormSubmitId").value;</p> <p style="text-indent: 2em;">//如果不为"",表明是编辑;否则为新建,不用验证</p> <p style="text-indent: 2em;"><!--more--></p> <p style="text-indent: 2em;">if (newName != "")</p> <p style="text-indent: 2em;">{</p> <p style="text-indent: 2em;"> var bVisible = false;</p> <p style="text-indent: 2em;"> var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");</p> <p style="text-indent: 2em;"> //1.验证当前用户是否为创建者(如果是,允许编辑)</p> <p style="text-indent: 2em;"> var xml = "" + "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +</p> <p style="text-indent: 2em;"> "<soap:Envelope xmlns:soap=\"<a href="https://www.samool.com/go/aHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvc29hcC9lbnZlbG9wZS8v" target="_blank" ><span style="color: #34b115;">http://schemas.xmlsoap.org/soap/envelope/\</span></a>" xmlns:xsi=\"<a href="https://www.samool.com/go/aHR0cDovL3d3dy53My5vcmcvMjAwMS9YTUxTY2hlbWEtaW5zdGFuY2Uv" target="_blank" ><span style="color: #34b115;">http://www.w3.org/2001/XMLSchema-instance\</span></a>" xmlns:xsd=\"<a href="https://www.samool.com/go/aHR0cDovL3d3dy53My5vcmcvMjAwMS9YTUxTY2hlbWEv" target="_blank" ><span style="color: #34b115;">http://www.w3.org/2001/XMLSchema\</span></a>">" +</p> <p style="text-indent: 2em;"> " <soap:Body>" +</p> <p style="text-indent: 2em;"> " <query xmlns:q1=\"<a href="https://www.samool.com/go/aHR0cDovL3NjaGVtYXMubWljcm9zb2Z0LmNvbS9jcm0vMjAwNi9RdWVyeS8=" target="_blank" ><span style="color: #34b115;">http://schemas.microsoft.com/crm/2006/Query\</span></a>" xsi:type=\"q1:QueryExpression\" xmlns=\"<a href="https://www.samool.com/go/aHR0cDovL3NjaGVtYXMubWljcm9zb2Z0LmNvbS9jcm0vMjAwNi9XZWJTZXJ2aWNlcy8=" target="_blank" ><span style="color: #34b115;">http://schemas.microsoft.com/crm/2006/WebServices\</span></a>">" +</p> <p style="text-indent: 2em;"> " <q1:EntityName>new_investment</q1:EntityName>" +</p> <p style="text-indent: 2em;"> " <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +</p> <p style="text-indent: 2em;"> " <q1:Attributes>" +</p> <p style="text-indent: 2em;"> " <q1:Attribute>createdby</q1:Attribute>" +</p> <p style="text-indent: 2em;"> " </q1:Attributes>" +</p> <p style="text-indent: 2em;"> " </q1:ColumnSet>" +</p> <p style="text-indent: 2em;"> " <q1:Distinct>false</q1:Distinct>" +</p> <p style="text-indent: 2em;"> " <q1:Criteria>" +</p> <p style="text-indent: 2em;"> " <q1:FilterOperator>And</q1:FilterOperator>" +</p> <p style="text-indent: 2em;"> " <q1:Conditions>" +</p> <p style="text-indent: 2em;"> " <q1:Condition>" +</p> <p style="text-indent: 2em;"> " <q1:AttributeName>new_investmentid</q1:AttributeName>" +</p> <p style="text-indent: 2em;"> " <q1:Operator>Equal</q1:Operator>" +</p> <p style="text-indent: 2em;"> " <q1:Values>" +</p> <p style="text-indent: 2em;"> " <q1:Value xmlns:q2=\"<a href="https://www.samool.com/go/aHR0cDovL21pY3Jvc29mdC5jb20vd3NkbC90eXBlcy8v" target="_blank" ><span style="color: #34b115;">http://microsoft.com/wsdl/types/\</span></a>" xsi:type=\"q2:guid\">" + newName + " </q1:Value>" +</p> <p style="text-indent: 2em;"> " </q1:Values>" +</p> <p style="text-indent: 2em;"> " </q1:Condition>" +</p> <p style="text-indent: 2em;"> " <q1:Condition>" +</p> <p style="text-indent: 2em;"> " <q1:AttributeName>createdby</q1:AttributeName>" +</p> <p style="text-indent: 2em;"> " <q1:Operator>EqualUserId</q1:Operator>" +</p> <p style="text-indent: 2em;"> " </q1:Condition>" +</p> <p style="text-indent: 2em;"> " </q1:Conditions>" +</p> <p style="text-indent: 2em;"> " </q1:Criteria>" +</p> <p style="text-indent: 2em;"> " </query>" +</p> <p style="text-indent: 2em;"> " </soap:Body>" +</p> <p style="text-indent: 2em;"> "</soap:Envelope>" +"";</p> <p style="text-indent: 2em;"> xmlHttpRequest.Open("POST", "/mscrmservices/2006/CrmService.asmx", false);</p> <p style="text-indent: 2em;"> xmlHttpRequest.setRequestHeader("SOAPAction","<a href="https://www.samool.com/go/aHR0cDovL3NjaGVtYXMubWljcm9zb2Z0LmNvbS9jcm0vMjAwNi9XZWJTZXJ2aWNlcy9SZXRyaWV2ZU11bHRpcGxl" target="_blank" ><span style="color: #34b115;">http://schemas.microsoft.com/crm/2006/WebServices/RetrieveMultiple</span></a>");</p> <p style="text-indent: 2em;"> xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");</p> <p style="text-indent: 2em;"> xmlHttpRequest.setRequestHeader("Content-Length", xml.length);</p> <p style="text-indent: 2em;"> xmlHttpRequest.send(xml);</p> <p style="text-indent: 2em;"> var resultXml = xmlHttpRequest.responseXML;</p> <p style="text-indent: 2em;"> var createdby = resultXml.selectNodes("//BusinessEntity/q1:createdby");</p> <p style="text-indent: 2em;"> </p> <p style="text-indent: 2em;"> //如果createdbys.length等于0,表明不是创建者,需要继续验证角色</p> <p style="text-indent: 2em;"> if (createdby.length == 0)</p> <p style="text-indent: 2em;"> {</p> <p style="text-indent: 2em;"> //2.验证当前用户是否包含指定的角色</p> <p style="text-indent: 2em;"> //此段内容用于查找当前用户的角色。角色存放在数组roles里。</p> <p style="text-indent: 2em;"> xml = "" + "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +</p> <p style="text-indent: 2em;"> "<soap:Envelope xmlns:soap=\"<a href="https://www.samool.com/go/aHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvc29hcC9lbnZlbG9wZS8v" target="_blank" ><span style="color: #34b115;">http://schemas.xmlsoap.org/soap/envelope/\</span></a>" xmlns:xsi=\"<a href="https://www.samool.com/go/aHR0cDovL3d3dy53My5vcmcvMjAwMS9YTUxTY2hlbWEtaW5zdGFuY2Uv" target="_blank" ><span style="color: #34b115;">http://www.w3.org/2001/XMLSchema-instance\</span></a>" xmlns:xsd=\"<a href="https://www.samool.com/go/aHR0cDovL3d3dy53My5vcmcvMjAwMS9YTUxTY2hlbWEv" target="_blank" ><span style="color: #34b115;">http://www.w3.org/2001/XMLSchema\</span></a>">" +</p> <p style="text-indent: 2em;"> " <soap:Body>" + GenerateAuthenticationHeader() +</p> <p style="text-indent: 2em;"> " <query xmlns:q1=\"<a href="https://www.samool.com/go/aHR0cDovL3NjaGVtYXMubWljcm9zb2Z0LmNvbS9jcm0vMjAwNi9RdWVyeS8=" target="_blank" ><span style="color: #34b115;">http://schemas.microsoft.com/crm/2006/Query\</span></a>" xsi:type=\"q1:QueryExpression\" xmlns=\"<a href="https://www.samool.com/go/aHR0cDovL3NjaGVtYXMubWljcm9zb2Z0LmNvbS9jcm0vMjAwNi9XZWJTZXJ2aWNlcy8=" target="_blank" ><span style="color: #34b115;">http://schemas.microsoft.com/crm/2006/WebServices\</span></a>">" +</p> <p style="text-indent: 2em;"> " <q1:EntityName>role</q1:EntityName>" +</p> <p style="text-indent: 2em;"> " <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +</p> <p style="text-indent: 2em;"> " <q1:Attributes>" +</p> <p style="text-indent: 2em;"> " <q1:Attribute>name</q1:Attribute>" +</p> <p style="text-indent: 2em;"> " </q1:Attributes>" +</p> <p style="text-indent: 2em;"> " </q1:ColumnSet>" +</p> <p style="text-indent: 2em;"> " <q1:Distinct>false</q1:Distinct>" +</p> <p style="text-indent: 2em;"> " <q1:LinkEntities>" +</p> <p style="text-indent: 2em;"> " <q1:LinkEntity>" +</p> <p style="text-indent: 2em;"> " <q1:LinkFromAttributeName>roleid</q1:LinkFromAttributeName>" +</p> <p style="text-indent: 2em;"> " <q1:LinkFromEntityName>role</q1:LinkFromEntityName>" +</p> <p style="text-indent: 2em;"> " <q1:LinkToEntityName>systemuserroles</q1:LinkToEntityName>" +</p> <p style="text-indent: 2em;"> " <q1:LinkToAttributeName>roleid</q1:LinkToAttributeName>" +</p> <p style="text-indent: 2em;"> " <q1:JoinOperator>Inner</q1:JoinOperator>" +</p> <p style="text-indent: 2em;"> " <q1:LinkEntities>" +</p> <p style="text-indent: 2em;"> " <q1:LinkEntity>" +</p> <p style="text-indent: 2em;"> " <q1:LinkFromAttributeName>systemuserid</q1:LinkFromAttributeName>" +</p> <p style="text-indent: 2em;"> " <q1:LinkFromEntityName>systemuserroles</q1:LinkFromEntityName>" +</p> <p style="text-indent: 2em;"> " <q1:LinkToEntityName>systemuser</q1:LinkToEntityName>" +</p> <p style="text-indent: 2em;"> " <q1:LinkToAttributeName>systemuserid</q1:LinkToAttributeName>" +</p> <p style="text-indent: 2em;"> " <q1:JoinOperator>Inner</q1:JoinOperator>" +</p> <p style="text-indent: 2em;"> " <q1:LinkCriteria>" +</p> <p style="text-indent: 2em;"> " <q1:FilterOperator>And</q1:FilterOperator>" +</p> <p style="text-indent: 2em;"> " <q1:Conditions>" +</p> <p style="text-indent: 2em;"> " <q1:Condition>" +</p> <p style="text-indent: 2em;"> " <q1:AttributeName>systemuserid</q1:AttributeName>" +</p> <p style="text-indent: 2em;"> " <q1:Operator>EqualUserId</q1:Operator>" +</p> <p style="text-indent: 2em;"> " </q1:Condition>" +</p> <p style="text-indent: 2em;"> " </q1:Conditions>" +</p> <p style="text-indent: 2em;"> " </q1:LinkCriteria>" +</p> <p style="text-indent: 2em;"> " </q1:LinkEntity>" +</p> <p style="text-indent: 2em;"> " </q1:LinkEntities>" +</p> <p style="text-indent: 2em;"> " </q1:LinkEntity>" +</p> <p style="text-indent: 2em;"> " </q1:LinkEntities>" +</p> <p style="text-indent: 2em;"> " </query>"+</p> <p style="text-indent: 2em;"> " </soap:Body>" +</p> <p style="text-indent: 2em;"> "</soap:Envelope>" +"";</p> <p style="text-indent: 2em;"> xmlHttpRequest.Open("POST", "/mscrmservices/2006/CrmService.asmx", false);</p> <p style="text-indent: 2em;"> xmlHttpRequest.setRequestHeader("SOAPAction","<a href="https://www.samool.com/go/aHR0cDovL3NjaGVtYXMubWljcm9zb2Z0LmNvbS9jcm0vMjAwNi9XZWJTZXJ2aWNlcy9SZXRyaWV2ZU11bHRpcGxl" target="_blank" ><span style="color: #34b115;">http://schemas.microsoft.com/crm/2006/WebServices/RetrieveMultiple</span></a>");</p> <p style="text-indent: 2em;"> xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");</p> <p style="text-indent: 2em;"> xmlHttpRequest.setRequestHeader("Content-Length", xml.length);</p> <p style="text-indent: 2em;"> xmlHttpRequest.send(xml);</p> <p style="text-indent: 2em;"> resultXml = xmlHttpRequest.responseXML;</p> <p style="text-indent: 2em;"> var roles = resultXml.selectNodes("//BusinessEntity/q1:name");</p> <p style="text-indent: 2em;"> </p> <p style="text-indent: 2em;"> //允许使用的角色列表</p> <p style="text-indent: 2em;"> var roleAllowList = [</p> <p style="text-indent: 2em;"> '系统管理员','市场营销副总裁','销售经理'</p> <p style="text-indent: 2em;"> ];</p> <p style="text-indent: 2em;"> //比较角色</p> <p style="text-indent: 2em;"> if(roles != null)</p> <p style="text-indent: 2em;"> {</p> <p style="text-indent: 2em;"> for(i = 0; i<roles.length; i++)</p> <p style="text-indent: 2em;"> {</p> <p style="text-indent: 2em;"> for(var j=0; j<roleAllowList.length; j++)</p> <p style="text-indent: 2em;"> {</p> <p style="text-indent: 2em;"> if(roles[i].text == roleAllowList[j])</p> <p style="text-indent: 2em;"> {</p> <p style="text-indent: 2em;"> bVisible = true; </p> <p style="text-indent: 2em;"> break;</p> <p style="text-indent: 2em;"> }</p> <p style="text-indent: 2em;"> }</p> <p style="text-indent: 2em;"> </p> <p style="text-indent: 2em;"> if(bVisible)</p> <p style="text-indent: 2em;"> break;</p> <p style="text-indent: 2em;"> }</p> <p style="text-indent: 2em;"> }</p> <p style="text-indent: 2em;"> }</p> <p style="text-indent: 2em;"> else</p> <p style="text-indent: 2em;"> bVisible = true; //是创建者,允许显示</p> <p style="text-indent: 2em;"> </p> <p style="text-indent: 2em;"> //判断当前文档是否为当前用户创建</p> <p style="text-indent: 2em;"> </p> <p style="text-indent: 2em;"> //禁止显示</p> <p style="text-indent: 2em;"> if (!bVisible)</p> <p style="text-indent: 2em;"> {</p> <p style="text-indent: 2em;"> document.getElementById("new_tel_mobile").style.display = "none";//需要隐藏的字段</p> <p style="text-indent: 2em;"> //document.getElementById("new_otherpreference").value = "你没有'" + roleName + "'的角色,不能显示'某些内容'!";</p> <p style="text-indent: 2em;"> //document.getElementById("new_otherpreference").style.color = "red";</p> <p style="text-indent: 2em;"> }</p> <p style="text-indent: 2em;">}</p> </td> </tr> </tbody> </table> 相关文章 MSCRM禁用标签页内所有组件 mscrm4屏幕ctrl+s热键 mscrm4隐藏grid页上的按钮 mscrm 将自定义的视图在sitemap中显示 mscrm4 用js阻止表单保存 MSCRM4 ISV.Config 控制MSCRM4菜单栏 MSCRM4 隐藏按钮方法 CRM中代码自动共享 MSCRM4获取选择记录的guid Last modification:December 24th, 2010 at 02:45 pm © 允许规范转载 Support 如果觉得我的文章对你有用,请随意赞赏 ×Close Appreciate the author Sweeping payments Pay by AliPay Pay by WeChat