如何获得与给定实体关联的CRM角色

时间:2015-10-26 03:39:06

标签: c# dynamics-crm-2013

我正在尝试编写一些C#,因此我为一个函数提供了一个实体名称,它返回与该实体关联的所有CRM角色。

或者即使我可以为给定的安全角色获取prvReadRole的所有实体。

2 个答案:

答案 0 :(得分:0)

我正在使用T-SQL解决此问题。

我已经编写了一个类来获取具有特权读取的实体名称的所有角色

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace xxx.xxx.xxxx.xxx.xxx
{
    public class xxx
    {
        public static String GetRolesForEntityTSQL(string Entity) {
            return String.Format(@"
                    DECLARE
                           @EntityName VARCHAR(200) = '{0}',
                           @Action VARCHAR(200) = 'Read'

                    DECLARE @PrivName VARCHAR(500) = 'prv' + @Action + @EntityName

                    SELECT  rb.Name, pb.Name, rb.RoleId, rb.ParentRoleId, rb.ParentRootRoleId
                    FROM   RolePrivilegesBase p
                    INNER JOIN    PrivilegeBase pb
                           ON p.PrivilegeId = pb.PrivilegeId
                           INNER JOIN RoleBase rb
                           ON rb.RoleId = p.RoleId
                    WHERE 
                    pb.Name = @PrivName

                    ", Entity);
        }
    }
}

答案 1 :(得分:0)

我在Xml中只有源代码,因为我已经在客户端进行了检查,但我相信你可以使用Xml作为样本构建QueryExpression和C#。

var requestXml = "<s:envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'>" +
"  <s:body>" +
"    <retrievemultiple xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.microsoft.com/xrm/2011/Contracts/Services'>" +
"      <query i:type='a:QueryExpression' xmlns:a='http://schemas.microsoft.com/xrm/2011/Contracts'>" +
"        <a:columnset>" +
"          <a:allcolumns>false</a:allcolumns>" +
"          <a:columns xmlns:b='http://schemas.microsoft.com/2003/10/Serialization/Arrays'>" +
"        </a:columns></a:columnset>" +
"        <a:criteria>" +
"          <a:conditions>" +
"            <a:conditionexpression>" +
"              <a:attributename>name</a:attributename>" +
"              <a:operator>Equal</a:operator>" +
"              <a:values xmlns:b='http://schemas.microsoft.com/2003/10/Serialization/Arrays'>" +
"                <b:anytype i:type='c:string' xmlns:c='http://www.w3.org/2001/XMLSchema'>" + privilegeName + "</b:anytype>" +
"              </a:values>" +
"            </a:conditionexpression>" +
"          </a:conditions>" +
"          <a:filteroperator>And</a:filteroperator>" +
"          <a:filters>" +
"          <a:isquickfindfilter>false</a:isquickfindfilter>" +
"        </a:filters></a:criteria>" +
"        <a:distinct>false</a:distinct>" +
"        <a:entityname>privilege</a:entityname>" +
"        <a:linkentities>" +
"          <a:linkentity>" +
"            <a:columns>" +
"              <a:allcolumns>false</a:allcolumns>" +
"              <a:columns xmlns:b='http://schemas.microsoft.com/2003/10/Serialization/Arrays'>" +
"            </a:columns>" +
"            <a:entityalias i:nil='true'>" +
"            <a:joinoperator>Inner</a:joinoperator>" +
"            <a:linkcriteria>" +
"              <a:conditions>" +
"              <a:filteroperator>And</a:filteroperator>" +
"              <a:filters>" +
"              <a:isquickfindfilter>false</a:isquickfindfilter>" +
"            </a:filters></a:conditions></a:linkcriteria>" +
"            <a:linkentities>" +
"              <a:linkentity>" +
"                <a:columns>" +
"                  <a:allcolumns>false</a:allcolumns>" +
"                  <a:columns xmlns:b='http://schemas.microsoft.com/2003/10/Serialization/Arrays'>" +
"                </a:columns>" +
"                <a:entityalias i:nil="true">" +
"                <a:joinoperator>Inner</a:joinoperator>" +
"                <a:linkcriteria>" +
"                  <a:conditions>" +
"                  <a:filteroperator>And</a:filteroperator>" +
"                  <a:filters>" +
"                  <a:isquickfindfilter>false</a:isquickfindfilter>" +
"                </a:filters></a:conditions></a:linkcriteria>" +
"                <a:linkentities>" +
"                  <a:linkentity>" +
"                    <a:columns>" +
"                      <a:allcolumns>false</a:allcolumns>" +
"                      <a:columns xmlns:b='http://schemas.microsoft.com/2003/10/Serialization/Arrays'>" +
"                    </a:columns>" +
"                    <a:entityalias i:nil="true">" +
"                    <a:joinoperator>Inner</a:joinoperator>" +
"                    <a:linkcriteria>" +
"                      <a:conditions>" +
"                        <a:conditionexpression>" +
"                          <a:attributename>systemuserid</a:attributename>" +
"                          <a:operator>EqualUserId</a:operator>" +
"                          <a:values xmlns:b='http://schemas.microsoft.com/2003/10/Serialization/Arrays'>" +
"                        </a:values></a:conditionexpression>" +
"                      </a:conditions>" +
"                      <a:filteroperator>And</a:filteroperator>" +
"                      <a:filters>" +
"                      <a:isquickfindfilter>false</a:isquickfindfilter>" +
"                    </a:filters></a:linkcriteria>" +
"                    <a:linkentities>" +
"                    <a:linkfromattributename>roleid</a:linkfromattributename>" +
"                    <a:linkfromentityname>privilege</a:linkfromentityname>" +
"                    <a:linktoattributename>roleid</a:linktoattributename>" +
"                    <a:linktoentityname>systemuserroles</a:linktoentityname>" +
"                  </a:linkentities></a:entityalias></a:columns></a:linkentity>" +
"                </a:linkentities>" +
"                <a:linkfromattributename>roleid</a:linkfromattributename>" +
"                <a:linkfromentityname>privilege</a:linkfromentityname>" +
"                <a:linktoattributename>parentrootroleid</a:linktoattributename>" +
"                <a:linktoentityname>role</a:linktoentityname>" +
"              </a:entityalias></a:columns></a:linkentity>" +
"            </a:linkentities>" +
"            <a:linkfromattributename>privilegeid</a:linkfromattributename>" +
"            <a:linkfromentityname>privilege</a:linkfromentityname>" +
"            <a:linktoattributename>privilegeid</a:linktoattributename>" +
"            <a:linktoentityname>roleprivileges</a:linktoentityname>" +
"          </a:entityalias></a:columns></a:linkentity>" +
"        </a:linkentities>" +
"        <a:orders>" +
"        <a:pageinfo>" +
"          <a:count>0</a:count>" +
"          <a:pagenumber>0</a:pagenumber>" +
"          <a:pagingcookie i:nil="true">" +
"          <a:returntotalrecordcount>false</a:returntotalrecordcount>" +
"        </a:pagingcookie></a:pageinfo>" +
"        <a:nolock>false</a:nolock>" +
"      </a:orders></query>" +
"    </retrievemultiple>" +
"  </s:body>" +
"</s:envelope>";