我正在尝试在一个查询中获取当前用户的角色以及有关所述角色的数据(而不是使用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'.
有什么方法可以解决这个问题?我希望它可以工作,以减少客户端对端点的调用。
答案 0 :(得分:1)
这里有两个简单的选择:
获取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')
您可以将此获取查询传递给网络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。