2

我正在尝试在一个查询中获取当前用户的角色以及有关所述角色的数据(而不是使用 Xrm.Page.context.getUserRoles 然后遍历所有结果并使用其 ID 查询每个包含角色的端点)。

我正在使用 Microsoft XRM 工具(在 CRM 2016 中),它生成的查询看起来合法:

xrmservices/2011/OrganizationData.svc/SystemUserRolesSet?$select=systemuserroles_association/Name&$expand=systemuserroles_association&$filter=SystemUserId%20eq%20guid%27436CD5A4-FD15-E611-80C1-00155D7B5806%27

如果我查看由 CrmSvcUtil 生成的 SystemUserRole 类,似乎有这样一个属性(systemuserroles_association),但看起来 SVC 端点没有使用相同的类定义。

Type 'Microsoft.Crm.Sdk.Data.Services.SystemUserRoles' does not have a property named 'systemuserroles_association'.

有什么方法可以解决这个问题吗?我希望它能够工作,以减少客户端对端点的调用。

4

1 回答 1

1

这里有两个简单的选择:

一次调用查询所有角色ID

获取结果Xrm.Page.context.getUserRoles并生成一个 OR 查询,如下所示:RoleSet?$select=Name,BusinessUnitId,RoleId&$orderby=Name&$filter=RoleId%20eq%20%28guid%27cb269f84-e19d-e011-b66c-00155db528b6%27%29%20or%20RoleId%20eq%20%28guid%27fcbd7757-bfa5-e511-80d4-6c3be5a8fa14%27%29

未编码以便于阅读:XRMServices/2011/OrganizationData.svc/RoleSet?$select=Name,BusinessUnitId,RoleId&$orderby=Name&$filter=RoleId eq (guid'cb269f84-e19d-e011-b66c-00155db528b6') or RoleId eq (guid'fcbd7757-bfa5-e511-80d4-6c3be5a8fa14')

通过获取

您可以将此 fetch 查询传递给 web api:

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true">
  <entity name="role">
    <attribute name="name" />
    <attribute name="businessunitid" />
    <attribute name="roleid" />
    <order attribute="name" descending="false" />
    <link-entity name="systemuserroles" from="roleid" to="roleid" visible="false" intersect="true">
      <link-entity name="systemuser" from="systemuserid" to="systemuserid" alias="ac">
        <filter type="and">
          <condition attribute="systemuserid" operator="eq-userid" />
        </filter>
      </link-entity>
    </link-entity>
  </entity>
</fetch>

这是它的样子:

var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/roles?fetchXml=%3Cfetch%20version%3D%221.0%22%20output-format%3D%22xml-platform%22%20mapping%3D%22logical%22%20distinct%3D%22true%22%3E%3Centity%20name%3D%22role%22%3E%3Cattribute%20name%3D%22name%22%20%2F%3E%3Cattribute%20name%3D%22businessunitid%22%20%2F%3E%3Cattribute%20name%3D%22roleid%22%20%2F%3E%3Corder%20attribute%3D%22name%22%20descending%3D%22false%22%20%2F%3E%3Clink-entity%20name%3D%22systemuserroles%22%20from%3D%22roleid%22%20to%3D%22roleid%22%20visible%3D%22false%22%20intersect%3D%22true%22%3E%3Clink-entity%20name%3D%22systemuser%22%20from%3D%22systemuserid%22%20to%3D%22systemuserid%22%20alias%3D%22ac%22%3E%3Cfilter%20type%3D%22and%22%3E%3Ccondition%20attribute%3D%22systemuserid%22%20operator%3D%22eq-userid%22%20%2F%3E%3C%2Ffilter%3E%3C%2Flink-entity%3E%3C%2Flink-entity%3E%3C%2Fentity%3E%3C%2Ffetch%3E", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
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);
        }
        else {
            alert(this.statusText);
        }
    }
};
req.send();

我使用CRM Rest Builder来生成那个 JS。

于 2016-05-16T20:46:05.160 回答