Microsoft Entra ID 对象的高级查询功能

随着Microsoft Entra在稳定性、可用性和性能方面不断提供更多的功能和改进,Microsoft Graph 也会不断发展和缩放,以有效地访问数据。 一种方法是通过 Microsoft Graph 对各种Microsoft Entra ID对象(也称为目录对象及其属性)的高级查询功能的支持不断增加。 例如,在 $filter 查询参数上添加 not (not),不等于 (ne), (endsWith) 运算符结尾。

Microsoft Graph 查询引擎使用索引存储来满足查询请求。 为了添加对某些属性的其他查询功能的支持,这些属性现在在单独的存储中编制索引。 这种单独的索引使Microsoft Entra ID能够增加对查询请求的支持并提高性能。 但是,默认情况下,这些高级查询功能不可用,但请求者还必须将 ConsistencyLevel 标头设置为 eventual并且(除了 $search)使用 $count 查询参数。 ConsistencyLevel标头和$count被称为高级查询参数

例如,要仅检索非活动用户帐户,你可以运行使用 $filter 查询参数的查询之一。

选项 1:$filter 查询参数与 运算符一起使用 eq 。 此请求默认有效,即请求不需要高级查询参数。

GET https://graph.microsoft.com/v1.0/users?$filter=accountEnabled eq false

选项 2:$filter 查询参数与 运算符一起使用 ne 。 默认情况下不支持此请求, ne 因为运算符仅在高级查询中受支持。 因此,必须将 ConsistencyLevel 标头设置为 eventual并使用$count=true查询字符串。

GET https://graph.microsoft.com/v1.0/users?$filter=accountEnabled ne true&$count=true
ConsistencyLevel: eventual

Microsoft Entra ID (目录) 支持高级查询功能的对象

仅目录对象及其关系支持这些高级查询功能,包括以下常用对象:

Object 关系
administrativeUnit
  • members
  • application
  • 所有者
  • appRoleAssignment -
    设备
  • memberOf
  • transitiveMemberOf
  • registeredUsers
  • registeredOwners
  • group
  • members
  • transitiveMembers
  • memberOf
  • transitiveMemberOf
  • 所有者
  • appRoleAssignments
  • oAuth2PermissionGrant(委派的权限授予) -
    orgContact
  • memberOf
  • transitiveMemberOf
  • servicePrincipal
  • memberOf
  • transitiveMemberOf
  • appRoleAssignments
  • appRoleAssignmentsTo
  • oAuth2PermissionGrant
  • user
  • memberOf
  • transitiveMemberOf
  • ownedObjects
  • registeredDevices
  • ownedDevices
  • transitiveManagers
  • directReports
  • transitiveReports
  • appRoleAssignments
  • oAuth2PermissionGrant
  • 需要高级查询功能的查询方案

    下表列出了仅在高级查询中支持的目录对象的查询方案:

    说明 示例
    使用 $count 作为 URL 段 GET~/groups/$count
    使用 $count 作为查询字符串参数 GET~/servicePrincipals?$count=true
    $filter 表达式中使用 $count GET~/users?$filter=assignedLicenses/$count eq 0&$count=true
    使用 $search GET~/applications?$search="displayName:Browser"
    对选择属性使用 $orderby GET~/applications?$orderby=displayName&$count=true
    $filterendsWith 运算符结合使用 GET~/users?$count=true&$filter=endsWith(mail,'@outlook.com')
    在同一查询中使用$filter$orderby GET../applications?$orderby=displayName&$filter=startsWith( displayName, 'Box')&$count=true
    对特定属性将 $filterstartsWith 运算符结合使用. GET~/users?$filter=startsWith(mobilePhone, '25478') OR startsWith(mobilePhone, '25473')&$count=true
    $filternenot 运算符结合使用 GET~/users?$filter=companyName ne null and NOT(companyName eq 'Microsoft')&$count=true
    $filternotstartsWith 运算符结合使用 GET~/users?$filter=NOT startsWith(displayName, 'Conf')&$count=true
    在带有 endsWith 运算符的集合上使用 $filter GET~/users?$count=true&$filter=proxyAddresses/any (p:endsWith(p, 'contoso.com'))&$select=id,displayName,proxyaddresses
    将 OData 强制转换与可传递成员列表配合使用 GET~/me/transitiveMemberOf/microsoft.graph.group?$count=true

    注意

    • 仅高级查询支持将 $filter$orderby 结合使用。
    • 高级查询当前不支持$expand
    • 高级查询功能目前不适用于Azure AD B2C租户。
    • 若要在 批处理请求 中使用高级查询功能,请在 POST 请求的 JSON 正文中指定 ConsistencyLevel 标头。

    支持按Microsoft Entra ID (目录) 对象的属性进行筛选

    目录对象的属性对查询参数的支持行为各不相同。 以下是目录对象的常见应用场景:

    • 默认情况下支持的查询也适用于高级查询参数,但响应最终会一致。
    • 默认情况下,只要默认支持eq运算符,则默认支持in运算符。
    • endsWith仅通过邮件otherMailsuserPrincipalNameproxyAddresses 属性的高级查询参数支持运算符。
    • 获取空集合 (/$count eq 0/$count ne 0) 和对象少于 (/$count eq 1的集合, /$count ne 1) 仅支持高级查询参数。
    • notne 求反运算符仅支持高级查询参数。
      • 支持 eq 运算符的所有属性也支持 nenot 运算符。
      • 对于使用 any lambda 运算符的查询,请使用 not 运算符。 请参阅使用 lambda 运算符的筛选器

    下表按目录对象的属性汇总了对 $filter 运算符的支持,并指示通过高级查询功能支持查询的位置。

    Legend

    • 默认情况下有效。不需要高级查询参数。 运算符 $filter 默认适用于该属性。
    • 需要高级查询参数。运算符$filter需要高级查询参数,这些参数包括:
      • ConsistencyLevel=eventual 标头
      • $count=true 查询字符串
    • 不支持。$filter 属性不支持 运算符。 向我们发送反馈 ,请求此属性支持 $filter 方案。
    • 空白单元格指示查询对该属性无效。
    • null 值 指示该属性可为 null 且可使用 null筛选。
    • 此处未列出的属性根本不支持 $filter

    管理单元属性

    属性 eq startsWith eq Null
    说明 先进 高级 先进
    displayName 默认 默认值 高级
    isMemberManagementRestricted 默认值 NotSupported
    membershipRule 默认 默认值 NotSupported
    membershipRuleProcessingState 默认值 NotSupported
    scopedRoleMembers/any (s:s/id) 默认值

    应用程序属性

    属性 eq startsWith ge/le eq Null
    appId 默认值
    createdDateTime 默认 默认值 高级
    createdOnBehalfOf/id 默认值
    说明 先进 高级 先进
    disabledByMicrosoftStatus 默认 NotSupported
    displayName 默认 默认值 高级
    federatedIdentityCredentials/any (f:f/issuer) 高级 高级 NotSupported
    federatedIdentityCredentials/any (f:f/name) 高级 高级 NotSupported
    federatedIdentityCredentials/any (f:f/subject) 高级 高级 NotSupported
    identifierUris/any(p:p) 默认值 默认值
    info/logoUrl NotSupported NotSupported 高级
    info/termsOfServiceUrl 高级 高级 NotSupported
    notes 高级 高级 高级
    publicClient/redirectUris/any(p:p) 高级 高级
    publisherDomain 默认 默认值 NotSupported
    requiredResourceAccess/any(r:r/resourceAppId) 高级
    serviceManagementReference 高级 高级 高级
    signInAudience 默认 NotSupported
    spa/redirectUris/any(p:p) 高级 高级
    tags/any(p:p) 默认值 默认值
    唯一名称 默认值 默认值 NotSupported
    verifiedPublisher/displayName 高级 高级 高级
    web/homePageUrl 高级 高级 高级
    web/redirectUris/any(p:p) 高级 高级

    应用程序实体的以下属性支持 $count 筛选器表达式中的集合。

    属性 eq 计数 0 eq 计数 1
    extensionProperties/$count 高级 NotSupported
    federatedIdentityCredentials/$count 高级 NotSupported

    协定属性

    属性 eq startsWith
    customerId 默认
    defaultDomainName 默认 默认值
    displayName 默认 默认值

    设备属性

    属性 eq startsWith ge/le eq Null
    accountEnabled 默认 NotSupported
    alternativeSecurityIds/any(a:a/identityProvider) 高级 高级 NotSupported
    alternativeSecurityIds/any(a:a/type) 默认值 高级 NotSupported
    approximateLastSignInDateTime 默认 默认值 高级
    deviceCategory 高级 高级 先进
    deviceId 默认
    deviceOwnership 高级 先进
    displayName 默认 默认值 高级
    enrollmentProfileName 高级 高级 高级
    extensionAttributes/extensionAttribute1-15 高级 高级 高级
    hostnames/any(p:p) 默认值 默认值
    isCompliant 默认 NotSupported
    isManaged 默认 NotSupported
    isRooted 高级 高级
    managementType 高级 高级
    manufacturer 高级 高级 先进
    mdmAppId 默认
    model 高级 高级 先进
    onPremisesLastSyncDateTime 默认 默认值 NotSupported
    onPremisesSecurityIdentifier 默认 先进
    onPremisesSyncEnabled 默认 高级
    operatingSystem 默认 默认值 先进
    operatingSystemVersion 默认 默认值 高级
    physicalIds/any(p:p) 默认值
    profileType 默认值 NotSupported
    registrationDateTime 高级 高级 高级
    trustType 默认值 NotSupported

    设备实体的以下属性支持$count筛选器表达式中的集合。

    属性 eq 计数 0 eq 计数 1
    physicalIds/$count 高级 NotSupported
    systemLabels/$count 高级 NotSupported

    目录角色属性

    属性 eq startsWith eq Null
    说明 先进 高级 先进
    displayName 默认 高级 先进
    roleTemplateId 默认 NotSupported

    组属性

    属性 eq startsWith ge/le eq Null
    appRoleAssignments/any (a:a/id) 默认值
    assignedLicenses/any (a:a/skuId) 默认值
    classification 默认 默认值 NotSupported
    createdByAppId 默认值
    createdOnBehalfOf/id 默认值
    说明 先进 高级 先进
    displayName 默认 默认值 先进
    expirationDateTime 先进 高级 NotSupported
    hasMembersWithLicenseErrors 默认 默认值
    infoCatalogs/any(p:p) 默认值 默认
    isAssignableToRole 默认 NotSupported
    mail 默认 默认值 先进
    mailEnabled 默认 NotSupported
    mailNickname 默认 默认值 先进
    membershipRule 默认 默认值 NotSupported
    membershipRuleProcessingState 默认值 NotSupported
    onPremisesLastSyncDateTime 默认 默认值 NotSupported
    onPremisesProvisioningErrors/any(o:o/category) 默认值 NotSupported
    onPremisesProvisioningErrors/any(o:o/propertyCausingError) 默认值 NotSupported
    onPremisesSamAccountName 先进 高级 NotSupported
    onPremisesSecurityIdentifier 默认 先进
    onPremisesSyncEnabled 默认 先进
    preferredLanguage 先进 高级 高级
    proxyAddresses/any(p:p) 默认值 默认
    renewedDateTime 默认 默认值 NotSupported
    resourceBehaviorOptions/any(p:p) 默认值
    resourceProvisioningOptions/any(p:p) 默认值
    securityEnabled 默认 NotSupported
    settings/any (s:s/displayName) 默认值 默认值 NotSupported
    settings/any (s:s/id) 默认值
    唯一名称 默认值 默认值 NotSupported

    实体的以下属性支持$count筛选器表达式中的集合。

    属性 eq 计数 0 eq 计数 1
    assignedLicenses/$count 高级 NotSupported
    onPremisesProvisioningErrors/$count 高级 NotSupported
    proxyAddresses/$count 高级 NotSupported

    组织联系人属性

    属性 eq startsWith ge/le eq Null
    CompanyName 先进 高级 先进
    department 默认 默认值 先进
    displayName 默认 默认值 先进
    givenName 默认 默认值 先进
    jobTitle 默认 默认值 先进
    mail 默认 默认值 先进
    mailNickname 默认 默认值 高级
    manager/id 默认
    onPremisesLastSyncDateTime 默认 默认值 NotSupported
    onPremisesProvisioningErrors/any(o:o/category) 默认值 NotSupported
    onPremisesProvisioningErrors/any(o:o/propertyCausingError) 默认值 NotSupported
    onPremisesSyncEnabled 默认 高级
    proxyAddresses/any(p:p) 默认值 默认值
    surname 默认 默认值 高级

    orgContact 实体的以下属性支持$count筛选器表达式中的集合。

    属性 eq 计数 0 eq 计数 1
    onPremisesProvisioningErrors/$count 高级 NotSupported
    proxyAddresses/$count 高级 NotSupported

    服务主体属性

    属性 eq startsWith ge/le eq Null
    accountEnabled 默认 NotSupported
    alternativeNames/any (p:p) 默认值 默认值
    appId 默认值
    appOwnerOrganizationId 先进
    appRoleAssignedTo/any (a:a/id) 默认值
    appRoleAssignmentRequired 先进 NotSupported
    appRoleAssignments/any (a:a/id) 默认值
    applicationTemplateId 默认
    createdObjects/any(c:c/id) 高级
    delegatedPermissionClassifications/any (d:d/id) 默认值
    说明 先进 高级 先进
    displayName 默认 默认值 高级
    federatedIdentityCredentials/any (f:f/issuer) 高级 高级 NotSupported
    federatedIdentityCredentials/any (f:f/name) 高级 高级 NotSupported
    federatedIdentityCredentials/any (f:f/subject) 高级 高级 NotSupported
    homepage 高级 高级 高级
    info/logoUrl NotSupported NotSupported 高级
    info/termsOfServiceUrl 高级 高级 NotSupported
    notes 高级 高级 高级
    oauth2PermissionGrants/任何 (o:o/id) 默认值
    preferredSingleSignOnMode 默认值 NotSupported
    preferredTokenSigningKeyEndDateTime 默认值 默认值 NotSupported
    publisherName 默认值 默认值 NotSupported
    remoteDesktopSecurityConfiguration/id 默认值
    remoteDesktopSecurityConfiguration/targetDeviceGroups/any (t:t/displayName) 默认值 默认值 NotSupported
    remoteDesktopSecurityConfiguration/targetDeviceGroups/any (t:t/id) 默认值
    servicePrincipalNames/any (p:p) 默认值 默认值
    servicePrincipalType 默认值 NotSupported
    tags/any(p:p) 默认值 默认值
    verifiedPublisher/displayName 高级 高级 高级

    servicePrincipal 实体的以下属性支持$count筛选器表达式中的集合。

    属性 eq 计数 0 eq 计数 1
    federatedIdentityCredentials/$count 高级 NotSupported
    ownedObjects/$count 高级 高级

    用户属性

    属性 eq startsWith ge/le eq Null
    accountEnabled 默认 NotSupported
    ageGroup 默认 NotSupported
    appRoleAssignments/any (a:a/id) 默认值
    assignedLicenses/any (a:a/skuId) 默认值
    assignedPlans/any(a:a/capabilityStatus) 高级 NotSupported
    assignedPlans/any(a:a/service) 高级 高级 NotSupported
    assignedPlans/any(a:a/servicePlanId) 高级
    authorizationInfo/certificateUserIds/any (p:p) 高级
    businessPhones/any (p:p) 高级 先进
    城市 默认 默认值 高级
    cloudRealtimeCommunicationInfo/isSipEnabled 默认值 NotSupported
    CompanyName 先进 高级 先进
    consentProvidedForMinor 默认 NotSupported
    country 默认 默认值 先进
    createdDateTime 默认 默认值 高级
    createdObjects/any(c:c/id) 先进
    creationType 默认 NotSupported
    department 默认 默认值 先进
    displayName 默认 默认值 先进
    employeeHireDate 先进 高级 NotSupported
    employeeId 默认 高级
    employeeOrgData/costCenter 高级 高级 NotSupported
    employeeOrgData/division 高级 高级 NotSupported
    employeeType 先进 NotSupported
    externalUserState 默认 NotSupported
    faxNumber 先进 高级 先进
    givenName 默认 默认值 高级
    identities/any(i:i/issuer) 默认值 NotSupported 默认值
    imAddresses/any (p:p) 默认值 默认值
    infoCatalogs/any(p:p) 默认值 默认值
    isLicenseReconciliationNeeded 默认值 NotSupported
    isResourceAccount 默认 NotSupported
    jobTitle 默认 默认值 高级
    licenseDetails/any (l:l/id) 默认值
    mail 默认 默认值 先进
    mailNickname 默认 默认值 高级
    manager/id 默认
    mobilePhone 先进 高级 高级
    oauth2PermissionGrants/任何 (o:o/id) 默认值
    officeLocation 先进 高级 高级
    onPremisesDistinguishedName 高级 高级 高级
    onPremisesExtensionAttributes/extensionAttribute1-15 高级 高级 先进
    onPremisesImmutableId 默认
    onPremisesLastSyncDateTime 默认 默认值 NotSupported
    onPremisesProvisioningErrors/any(o:o/category) 默认值 NotSupported
    onPremisesProvisioningErrors/any(o:o/propertyCausingError) 默认值 NotSupported
    onPremisesSamAccountName 先进 高级 NotSupported
    onPremisesSecurityIdentifier 默认 高级
    onPremisesSipInfo/isSipEnabled 高级 NotSupported
    onPremisesSyncEnabled 默认 高级
    otherMails/any (p:p) 默认值 默认
    passwordPolicies NotSupported NotSupported 高级
    passwordProfile/forceChangePasswordNextSignIn 高级 高级
    passwordProfile/forceChangePasswordNextSignInWithMfa 高级 先进
    postalCode 先进 高级 先进
    preferredLanguage 先进 高级 高级
    provisionedPlans/any(p:p/provisioningStatus) 高级 NotSupported
    provisionedPlans/any(p:p/service) 高级 高级 NotSupported
    proxyAddresses/any(p:p) 默认值 默认值
    scopedRoleMemberOf/any (s:s/id) 默认
    showInAddressList 先进 NotSupported
    state 默认 先进
    streetAddress 先进 高级 先进
    surname 默认 默认值 先进
    usageLocation 默认 默认值 先进
    userPrincipalName 默认 默认值 NotSupported
    userType 默认 高级

    以下用户实体属性支持$count筛选器表达式中的集合。

    属性 eq 计数 0 eq 计数 1
    assignedLicenses/$count 高级 NotSupported
    onPremisesProvisioningErrors/$count 高级 NotSupported
    otherMails/$count 高级 NotSupported
    ownedObjects/$count 高级 高级
    proxyAddresses/$count 高级 NotSupported

    下表显示了用户对象上其他扩展属性对 的支持$filter

    扩展类型 eq startsWith eq null
    架构扩展 高级 高级 高级
    开放扩展 NotSupported NotSupported NotSupported
    目录扩展 默认值 高级 高级

    支持按Microsoft Entra ID (目录) 对象的属性排序

    下表汇总了对 $orderby 目录对象的按属性的支持,并指示通过高级查询功能支持排序的位置。

    Legend

    • 默认情况下有效。不需要高级查询参数。 运算符 $orderby 默认适用于该属性。
    • 需要高级查询参数。运算符$orderby需要高级查询参数,这些参数包括:
      • ConsistencyLevel=eventual 标头
      • $count=true 查询字符串
    • 在同一 $filter 查询中对目录对象使用 和 $orderby 始终需要高级查询参数。 有关详细信息,请参阅 需要高级查询功能的查询方案
    目录对象 属性名称 支持$orderby
    administrativeUnit createdDateTime 高级
    administrativeUnit deletedDateTime 高级
    administrativeUnit displayName 高级
    应用程序 createdDateTime 高级
    应用程序 deletedDateTime 高级
    应用程序 displayName 高级
    orgContact createdDateTime 高级
    orgContact displayName 高级
    设备 approximateLastSignInDateTime 高级
    设备 createdDateTime 高级
    设备 deletedDateTime 高级
    设备 displayName 高级
    deletedDateTime 高级
    displayName 默认
    servicePrincipal createdDateTime 高级
    servicePrincipal deletedDateTime 高级
    servicePrincipal displayName 高级
    用户 createdDateTime 高级
    用户 deletedDateTime 高级
    用户 displayName 默认
    用户 userPrincipalName 默认

    针对目录对象的高级查询的错误处理

    仅支持使用高级查询参数对目录对象进行计数。 ConsistencyLevel=eventual如果未指定标头,则使用 URL 段时$count,请求将返回错误,或者以无提示方式忽略$count查询参数 (?$count=true) (如果使用)。

    GET https://graph.microsoft.com/v1.0/users/$count
    
    {
        "error": {
            "code": "Request_BadRequest",
            "message": "$count is not currently supported.",
            "innerError": {
                "date": "2021-05-18T19:03:10",
                "request-id": "d9bbd4d8-bb2d-44e6-99a1-71a9516da744",
                "client-request-id": "539da3bd-942f-25db-636b-27f6f6e8eae4"
            }
        }
    }
    

    对于目录对象, $search 仅适用于高级查询。 如果未指定 ConsistencyLevel 标头,请求将返回错误。

    GET https://graph.microsoft.com/v1.0/applications?$search="displayName:Browser"
    
    {
        "error": {
            "code": "Request_UnsupportedQuery",
            "message": "Request with $search query parameter only works through MSGraph with a special request header: 'ConsistencyLevel: eventual'",
            "innerError": {
                "date": "2021-05-27T14:26:47",
                "request-id": "9b600954-ba11-4899-8ce9-6abad341f299",
                "client-request-id": "7964ef27-13a3-6ca4-ed7b-73c271110867"
            }
        }
    }
    

    如果 URL 中的属性或查询参数仅在高级查询中受支持,但缺少 ConsistencyLevel 标头或 $count=true 查询字符串,则请求将返回错误。

    GET https://graph.microsoft.com/beta/users?$filter=endsWith(userPrincipalName,'%23EXT%23@contoso.com')
    
    {
        "error": {
            "code": "Request_UnsupportedQuery",
            "message": "Operator 'endsWith' is not supported because the required parameters might be missing. Try adding $count=true query parameter and ConsistencyLevel:eventual header. Refer to https://aka.ms/graph-docs/advanced-queries for more information",
            "innerError": {
                "date": "2023-07-14T08:43:39",
                "request-id": "b3731da7-5c46-4c37-a8e5-b190124d2531",
                "client-request-id": "a1556ddf-4794-929d-0105-b753a78b4c68"
            }
        }
    }
    

    如果尚未为某个属性编制索引以支持查询参数,即使指定了高级查询参数,请求也会返回错误。

    GET https://graph.microsoft.com/beta/groups?$filter=createdDateTime ge 2021-11-01&$count=true
    ConsistencyLevel: eventual
    
    {
        "error": {
            "code": "Request_UnsupportedQuery",
            "message": "Unsupported or invalid query filter clause specified for property 'createdDateTime' of resource 'Group'.",
            "innerError": {
                "date": "2023-07-14T08:42:44",
                "request-id": "b6a5f998-94c8-430d-846d-2eaae3031492",
                "client-request-id": "2be83e05-649e-2508-bcd9-62e666168fc8"
            }
        }
    }
    

    但是,包含查询参数的请求可能会以无提示方式失败。 例如,对于不支持的查询参数和不受支持的查询参数组合。 在这些情况下,请检查请求返回的数据,以确定指定的查询参数是否具有所需的效果。 例如,在下面的示例中,即使查询成功, @odata.count 参数也缺失。

    GET https://graph.microsoft.com/v1.0/users?$count=true
    
    HTTP/1.1 200 OK
    Content-type: application/json
    
    {
      "@odata.context":"https://graph.microsoft.com/v1.0/$metadata#users",
      "value":[
        {
          "displayName":"Oscar Ward",
          "mail":"oscarward@contoso.com",
          "userPrincipalName":"oscarward@contoso.com"
        }
      ]
    }