抓取用户列表存在于 dynamics crm 中的关系中

Grab list of users exists in a relation in dynamics crm

我在 dynamics crm 表单订单到系统用户中有多对多关系,从下面的代码片段我试图获取退出此关系的用户列表。但这会引发异常,即 附加信息:'SystemUser' 实体不包含 Name = 'salesorderid'..

的属性

知道吗,我在其中遗漏了什么?

private void grabRelatedUsers(IOrganizationService service){
 QueryExpression sysUsersQuery = new QueryExpression()
            {
                EntityName = "systemuser",
                ColumnSet = new ColumnSet("systemuserid"),
                LinkEntities = {
                    new LinkEntity()  {
                    LinkFromEntityName = "systemuser",
                    LinkToEntityName = "new_salesorder_systemuser",
                    LinkFromAttributeName = "systemuserid",
                    LinkToAttributeName = "systemuserid",
                    JoinOperator = JoinOperator.Inner
                    },
                    new LinkEntity() {
                        LinkFromEntityName = "new_salesorder_systemuser",
                        LinkToEntityName = "salesorder",
                        LinkFromAttributeName = "salesorderid",
                        LinkToAttributeName = "salesorderid",
                        JoinOperator = JoinOperator.Inner,
                        LinkCriteria = new FilterExpression() {
                            Conditions = {
                                new ConditionExpression("salesorderid",ConditionOperator.Equal,orderId)
                            }
                        }
                    },
                    new LinkEntity().AddLink("systemuser","systemuserid","systemuserid")
                    },

            };

            EntityCollection subGridUsers = service.RetrieveMultiple(sysUsersQuery);
}

您没有正确嵌套链接实体。查看为您的方案生成的 fetchXML:

<fetch version="1.0" output-format="xml-platform" mapping="logical">
  <entity name="systemuser">
    <attribute name="systemuserid" />
    <link-entity name="new_salesorder_systemuser" from="systemuserid" to="systemuserid" visible="false" intersect="true">
      <link-entity name="salesorder" from="salesorderid" to="salesorderid" alias="ab">
        <filter type="and">
          <condition attribute="salesorderid" operator="eq" value="XXXXXXXXXXXXXXX" />
        </filter>
      </link-entity>
    </link-entity>
  </entity>
</fetch>

如您所见,linked 实体必须是嵌套的,而且,因为这是开箱即用的 N:N 关系,您不能 link 来自 new_salesorder_systemuser 的销售订单, 但来自系统用户。所以 QueryExpression 应该是这样的:

QueryExpression sysUsersQuery = new QueryExpression()
{
    EntityName = "systemuser",
    ColumnSet = new ColumnSet("systemuserid"),
    LinkEntities = {
        new LinkEntity
        {
            LinkFromEntityName = "systemuser",
            LinkToEntityName = "new_salesorder_systemuser",
            LinkFromAttributeName = "systemuserid",
            LinkToAttributeName = "systemuserid",
            JoinOperator = JoinOperator.Inner,
            LinkEntities =
            {
                new LinkEntity()
                {
                    EntityAlias = "ab",
                    LinkFromEntityName = "systemuser",
                    LinkToEntityName = "salesorder",
                    LinkFromAttributeName = "salesorderid",
                    LinkToAttributeName = "salesorderid",
                    JoinOperator = JoinOperator.Inner,
                    LinkCriteria = new FilterExpression()
                    {
                        Conditions = {
                            new ConditionExpression("salesorderid",ConditionOperator.Equal, "sfsdf")
                        }
                    }
                }
            }
        }
    }
};

如果您在生成 QueryExpressions 时遇到问题,只需使用 Advanced Find 进行查询,下载 fetchXml 并使用 SDK FetchXmlToQueryExpressionRequest 消息将其转换为 QueryExpression。这里有一些样本: https://msdn.microsoft.com/en-us/library/hh547457.aspx