如何使用OData过滤SharePoint API返回结果中的集合类型子元素
Your initial filter expression $filter=Audience/ eq "Agent" won't work because Audience is a collection type (Collection(Edm.String)), not a single string value. You can't directly compare a collection to a single string—instead, you need to use OData's any operator to check if at least one element in the collection matches your criteria.
Correct Filter Syntax
Use this expression to retrieve items where the Audience collection contains the value "Agent":
$filter=Audience/any(a: a eq 'Agent')
Breakdown of the Expression
Audience/any(): This tells the API to iterate over each element in theAudiencecollection.a: A placeholder alias for each element in the collection (you can use any valid identifier here, likeitemorvalinstead ofa).a eq 'Agent': The condition to check—does the current element equal the string "Agent"? If even one element in the collection meets this condition, the item is included in the results.
Example Full API Request
Here's how the full request would look when fetching items from a SharePoint list:
GET https://your-sharepoint-site-url/_api/web/lists/getbytitle('YourTargetList')/items?$filter=Audience/any(a: a eq 'Agent')
Validation with Your Sample Data
Looking at your provided XML response, the Audience collection includes <d:element>Agent</d:element>, so this item would be correctly returned by the filter above.
If you ever need to check for multiple values (e.g., items that include both "Agent" and "Internal"), you can chain multiple any conditions with and:
$filter=Audience/any(a: a eq 'Agent') and Audience/any(a: a eq 'Internal')
内容的提问来源于stack exchange,提问作者Stanislav Mozolevskiy

