如何在Fetch XML中排除指定属性并检索实体其余所有属性
FetchXML 排除指定属性检索实体数据
首先明确:FetchXML 不支持你尝试的 !attribute! 这种排除语法,<all-attributes/> 会返回实体的所有属性,没法直接通过这种方式排除特定字段。要实现「获取所有属性但排除部分」的需求,有两种可行方案:
方案一:显式列出所有需要的属性
直接把除了 Name、Date 之外的所有属性逐个用 <attribute> 标签列出,替代 <all-attributes/>。示例代码如下:
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false" > <entity name="account" > <!-- 列出除name、date外的所有属性 --> <attribute name="accountid" /> <attribute name="statecode" /> <attribute name="location" /> <!-- 其他需要的属性... --> <order attribute="statecode" descending="false" /> <filter type="and" > <condition attribute="statecode" operator="eq" value="0" /> <condition attribute="location" operator="eq" uitype="location" value="{11c43ee8-b9d3-4e51-b73f-bd9dda66e29c}" /> </filter> </entity> </fetch>
优缺点:属性列表精确可控,但如果实体属性数量多(比如你说的42个),手动列写会很繁琐;后续实体新增属性时,需要手动更新FetchXML才能包含新属性。
方案二:使用 exclude-attributes 属性(推荐)
Dynamics 365 9.0 及以上版本支持在 <entity> 标签中添加 exclude-attributes 属性,直接指定需要排除的属性名(多个属性用逗号分隔),配合 <all-attributes/> 使用即可实现需求。示例代码:
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false" > <entity name="account" exclude-attributes="name,createdon" > <all-attributes/> <order attribute="statecode" descending="false" /> <filter type="and" > <condition attribute="statecode" operator="eq" value="0" /> <condition attribute="location" operator="eq" uitype="location" value="{11c43ee8-b9d3-4e51-b73f-bd9dda66e29c}" /> </filter> </entity> </fetch>
注:代码中的 createdon 替换为你实际要排除的Date类型属性的逻辑名称
优缺点:写法简洁,无需手动列大量属性;实体新增属性会自动被包含在结果中,无需修改FetchXML,但需要确认你的Dynamics 365环境版本支持该属性。
内容的提问来源于stack exchange,提问作者Aaditya
相关产品推荐
相关产品推荐

