如何通过 SOAP 从 MS Dynamics CRM 查询 n:n 关系?

How do I query an n:n relationship from MS Dynamics CRM via SOAP?

我需要将一些链接的实体添加到 Dynamics 365 CRM 上的 SOAP 查询(它报告的版本是 Version 1612 (8.2.1.176) (DB 8.2.1.176))。客户端实现在 PHP.

通过反复试验和大量示例,我已经能够将类似以下请求正文的内容组合在一起。 (我不确定这是否是构建查询的最佳方式;似乎还有其他几种方式,包括 <fetch>,但目前为止这个方式有效。)

<Execute xmlns="http://schemas.microsoft.com/xrm/2011/Contracts/Services" xmlns:si="http://www.w3.org/2001/XMLSchema-instance">
  <request xmlns:c="http://schemas.microsoft.com/xrm/2011/Contracts" si:type="c:RetrieveMultipleRequest">
    <c:Parameters xmlns:c2="http://schemas.datacontract.org/2004/07/System.Collections.Generic">
      <c:KeyValuePairOfstringanyType>
        <c2:key>Query</c2:key>
        <c2:value si:type="c:QueryExpression">
          <c:ColumnSet>
            <c:AllColumns>true</c:AllColumns>
            <c:Columns/>
          </c:ColumnSet>
          <c:EntityName>entity1</c:EntityName>
          <c:LinkEntities>
            <c:LinkEntity>
              <c:LinkFromAttributeName>entity2id</c:LinkFromAttributeName>
              <c:LinkFromEntityName>entity1</c:LinkFromEntityName>
              <c:LinkToAttributeName>entity2id</c:LinkToAttributeName>
              <c:LinkToEntityName>entity2</c:LinkToEntityName>
              <c:JoinOperator>Inner</c:JoinOperator>
              <c:LinkEntities>
                <c:LinkEntity>
                  <c:LinkFromAttributeName>entity3id</c:LinkFromAttributeName>
                  <c:LinkFromEntityName>entity2</c:LinkFromEntityName>
                  <c:LinkToAttributeName>entity3id</c:LinkToAttributeName>
                  <c:LinkToEntityName>entity3</c:LinkToEntityName>
                  <c:JoinOperator>Inner</c:JoinOperator>
                </c:LinkEntity>
              </c:LinkEntities>
            </c:LinkEntity>
          </c:LinkEntities>
          <c:Orders>
            <c:OrderExpression>
              <c:AttributeName>name</c:AttributeName>
              <c:OrderType>Ascending</c:OrderType>
            </c:OrderExpression>
          </c:Orders>
          <c:PageInfo>
            <c:Count>1</c:Count>
            <c:PageNumber>1</c:PageNumber>
          </c:PageInfo>
        </c2:value>
      </c:KeyValuePairOfstringanyType>
    </c:Parameters>
    <c:RequestName>RetrieveMultiple</c:RequestName>
  </request>
</Execute>

(在这种情况下,实体1到实体2的关系似乎是1:n,实体2到实体3的关系是n:1。实际上,实体2只包含连接实体1和实体3的外键,还有一些额外的属性。)

这个查询显然执行正确(它运行没有错误,并且根据内部连接限制了结果集),但我只从第一个实体类型中取回列。

我如何实际获取连接实体的属性?

编辑:

我尝试将以下各项添加到 <c:LinkEntity> 元素中:

<c:ColumnSet>
  <c:AllColumns>true</c:AllColumns>
</c:ColumnSet>

<c:Columns>
  <c:AllColumns>true</c:AllColumns>
</c:Columns>

<c:Columns>
  <a:string xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
    attribute1
  </a:string>
</c:Columns>

<c:ColumnSet>
  <c:Columns>
    <a:string xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
      attribute1
    </a:string>
  </c:Columns>
</c:ColumnSet>

不幸的是,虽然其中 none 个导致了错误,但它们也没有改变输出。

像下面这样放置 Columns 元素。

<Execute xmlns="http://schemas.microsoft.com/xrm/2011/Contracts/Services" xmlns:si="http://www.w3.org/2001/XMLSchema-instance">
  <request xmlns:c="http://schemas.microsoft.com/xrm/2011/Contracts" si:type="c:RetrieveMultipleRequest">
    <c:Parameters xmlns:c2="http://schemas.datacontract.org/2004/07/System.Collections.Generic">
      <c:KeyValuePairOfstringanyType>
        <c2:key>Query</c2:key>
        <c2:value si:type="c:QueryExpression">
          <c:ColumnSet>
            <c:AllColumns>true</c:AllColumns>
            <c:Columns/>
          </c:ColumnSet>
          <c:EntityName>entity1</c:EntityName>
          <c:LinkEntities>
             <c:LinkEntity>

              <c:Columns>
                <c:AllColumns>true</c:AllColumns>
              </c:Columns>

              <c:LinkFromAttributeName>entity2id</c:LinkFromAttributeName>
              <c:LinkFromEntityName>entity1</c:LinkFromEntityName>
              <c:LinkToAttributeName>entity2id</c:LinkToAttributeName>
              <c:LinkToEntityName>entity2</c:LinkToEntityName>
              <c:JoinOperator>Inner</c:JoinOperator>
              <c:LinkEntities>
                <c:LinkEntity>
                  <c:LinkFromAttributeName>entity3id</c:LinkFromAttributeName>
                  <c:LinkFromEntityName>entity2</c:LinkFromEntityName>
                  <c:LinkToAttributeName>entity3id</c:LinkToAttributeName>
                  <c:LinkToEntityName>entity3</c:LinkToEntityName>
                  <c:JoinOperator>Inner</c:JoinOperator>
                </c:LinkEntity>
              </c:LinkEntities>
            </c:LinkEntity>
          </c:LinkEntities>
          <c:Orders>
            <c:OrderExpression>
              <c:AttributeName>name</c:AttributeName>
              <c:OrderType>Ascending</c:OrderType>
            </c:OrderExpression>
          </c:Orders>
          <c:PageInfo>
            <c:Count>1</c:Count>
            <c:PageNumber>1</c:PageNumber>
          </c:PageInfo>
        </c2:value>
      </c:KeyValuePairOfstringanyType>
    </c:Parameters>
    <c:RequestName>RetrieveMultiple</c:RequestName>
  </request>
</Execute>