Search code examples
azure-active-directorymicrosoft-graph-api

graph api query with $filter operator (STARTS WITH and NOT)


I'm working on a Graph API query where I am trying to get Groups based on certain conditions. Here is the query below

with 'ConsistencyLevel': 'eventual' header

https://graph.microsoft.com/v1.0/users?$count=true&$filter=startswith(displayName , 'xyz') and NOT(groupTypes/any(c:c+eq+'Unified')) and securityEnabled eq false and mailEnabled eq true&$select=displayName,mail

Property 'groupTypes' does not exist as a declared property or extension property.

How would I combine STARTS WITH and NOT operator and pass it in $filter?


Solution

  • Note that: To query groups based on certain conditions, you need to make use of 'groups' endpoint instead of 'users'.

    • The groupTypes property is property is applicable to groups, not users.

    When I executed the same query as you, got the same error:

    GET https://graph.microsoft.com/v1.0/users?$count=true&$filter=startswith(displayName , 'test') and NOT(groupTypes/any(c:c+eq+'Unified')) and securityEnabled eq false and mailEnabled eq true&$select=displayName,mail
    
    ConsistencyLevel: Eventual
    

    enter image description here

    Hence, to resolve the error make use of groups endpoint:

    GET https://graph.microsoft.com/v1.0/groups?$count=true&$filter=startswith(displayName, 'test') and not groupTypes/any(c:c eq 'Unified') and securityEnabled eq false and mailEnabled eq true&$select=displayName,mail
    
    ConsistencyLevel: Eventual
    

    enter image description here

    Output:

    {
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#groups(displayName,mail)",
    "@odata.count": 1,
    "value": [
    {
    "displayName": "Testgroup",
    "mail": "samplegroup@xxx.onmicrosoft.com"
    }
    ]
    }