如何通过OData API获取CRM实体的完整时间线?
获取Dynamics 365联系人完整时间线(与UI一致)的开箱即用方案
你之前调用的$expand=Contact_ActivityPointers只能获取直接关联到联系人的活动(比如直接选了该联系人的电话、邮件、约会),但UI时间线里包含的通过incident(案例)间接关联的活动,这个方法是拿不到的。下面给你几个开箱即用的方案,不用遍历incident就能拿到和UI完全一致的时间线数据:
方案1:使用FetchXML查询ActivityPointer实体
FetchXML支持多层级关联查询,能一次性覆盖直接关联和间接关联的活动,这也是Dynamics 365 UI渲染时间线背后用的方式之一。
示例FetchXML(替换你的联系人ID):
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false" count="50"> <entity name="activitypointer"> <attribute name="activityid" /> <attribute name="subject" /> <attribute name="activitytypecode" /> <attribute name="createdon" /> <order attribute="createdon" descending="true" /> <!-- 直接关联联系人的活动 --> <link-entity name="contact" from="contactid" to="regardingobjectid" link-type="inner" alias="directContact"> <filter type="and"> <condition attribute="contactid" operator="eq" value="390eb71f-1ef2-f011-a141-c68c7256aea3" /> </filter> </link-entity> <!-- 通过案例(incident)间接关联的活动 --> <link-entity name="incident" from="incidentid" to="regardingobjectid" link-type="inner" alias="incidentLink"> <link-entity name="contact" from="contactid" to="customerid" link-type="inner" alias="contactThroughIncident"> <filter type="and"> <condition attribute="contactid" operator="eq" value="390eb71f-1ef2-f011-a141-c68c7256aea3" /> </filter> </link-entity> </link-entity> </entity> </fetch>
把这个FetchXML URL编码后,通过Web API调用:
https://myOrg.com/MyEnv/api/data/v9.0/activitypointers?fetchXml=【编码后的FetchXML内容】
方案2:使用Web API的ActivityPointer端点直接过滤
通过$filter组合多个关联条件,覆盖直接和间接关联的场景:
https://myOrg.com/MyEnv/api/data/v9.0/activitypointers?$filter=(regardingobjectid/contactid eq '390eb71f-1ef2-f011-a141-c68c7256aea3') or (regardingobjectid/incidentid in (incidents?$select=incidentid&$filter=customerid/contactid eq '390eb71f-1ef2-f011-a141-c68c7256aea3'))&$orderby=createdon desc&$select=activityid,subject,activitytypecode,createdon
这个请求会同时返回:
- 直接把联系人作为关联对象的活动
- 关联到该联系人所属案例的所有活动
方案3:使用官方Timeline API(推荐)
如果你的Dynamics 365版本是v9.1及以上,官方提供了专门的Timeline API,直接返回和UI完全一致的时间线数据,包括所有关联路径(直接、间接、甚至自定义关联),无需自己拼接关联条件:
请求示例:
POST https://myOrg.com/MyEnv/api/data/v9.1/GetTimelineRecords Content-Type: application/json { "EntityLogicalName": "contact", "EntityId": "390eb71f-1ef2-f011-a141-c68c7256aea3", "PageSize": 50, "PageNumber": 1, "SortDirection": "Descending", "SortByAttribute": "createdon" }
这个API会自动处理所有关联关系,返回的结构和UI时间线完全匹配,还支持分页、排序,是最省心的方案。
内容的提问来源于stack exchange,提问作者Hasan
相关产品推荐
相关产品推荐

