You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure AD中使用startswith过滤businessPhones属性的问题求助

解决Azure AD User中businessPhones字段的$filter适配问题

问题核心

Azure AD User的businessPhones是集合类型属性,不能直接套用字符串类型的startswith语法(如startswith(businessPhones, 'avi')),同时错误的集合any写法(如any(t: businessPhones : startswith(t, 'avi')))也会触发格式错误。

正确的过滤语法

针对businessPhones这类集合属性,符合Graph API规范的startswith过滤语法为:

businessPhones/any(t: startswith(t, 'avi'))

代码修改方案

假设你的动态查询生成逻辑是遍历searchFields数组生成过滤条件,只需添加对集合类型字段的判断分支,即可适配businessPhones:

问题代码片段(示例)

# 伪代码,以Python为例,需根据实际语言调整
def build_filter(search_fields, keyword):
    clauses = []
    for field in search_fields:
        # 统一使用字符串字段的startswith语法,不适用于集合字段
        clause = f"startswith({field}, '{keyword}')"
        clauses.append(clause)
    return " or ".join(clauses)

修改后的代码片段

# 伪代码,以Python为例,需根据实际语言调整
# 定义需要特殊处理的集合类型字段列表
COLLECTION_FIELDS = {'businessPhones', 'otherMails'}  # 可扩展其他集合字段

def build_filter(search_fields, keyword):
    clauses = []
    for field in search_fields:
        if field in COLLECTION_FIELDS:
            # 集合字段使用any语法拼接过滤条件
            clause = f"{field}/any(t: startswith(t, '{keyword}'))"
        else:
            # 普通字符串字段保留原有逻辑
            clause = f"startswith({field}, '{keyword}')"
        clauses.append(clause)
    return " or ".join(clauses)

验证正确的查询URL

修改后生成的合法查询示例:

https://graph.microsoft.com/v1.0/users?$filter=businessPhones/any(t: startswith(t, 'avi')) or startswith(displayName, 'avi')&$count=true

关键注意事项

  • 使用$count=true时,必须在请求头中添加ConsistencyLevel: eventual,否则会返回400错误。
  • 后续新增其他集合类型的用户属性(如otherMails),直接添加到COLLECTION_FIELDS集合即可统一处理。

内容的提问来源于stack exchange,提问作者Avinash Reddy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 07:32:39