Wednesday, 23 January 2019

Use of 'Select' and 'Filter' in OData Query in Microsoft Graph API with reference to PowerShell


Overview:

    The OData provider supports server-side OData filtering, searching, and sorting. I am just referring some queries for Office 365 - Microsoft Teams, for better explanation, but the OData syntax is applicable all Microsoft Graph API wherever it is applied.

My concern over here is regarding : Use of 'Select'  and 'Filter' in OData Query in Microsoft Graph API. 

1. Normal MS Teams GET request URI

https://graph.microsoft.com/v1.0/teams/f2a918a0-0b5c-4e58-8e83-9cabd24dea78

Response from Microsoft Graph API :


{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#teams('f2a918a0-0b5c-4e58-8e83-9cabd24dea78')/channels",
    "value": [
        {
            "id": "19:b4939b320444438e801d00becdc1c894@thread.skype",
            "displayName": "General",
            "description": "Sales Team"
        },
        {
            "id": "19:429d54ef13b44d8c934c425ac4742e2b@thread.skype",
            "displayName": "Sales Status",
            "description": null
        }
    ]
}

2. Use of 'Select' in OData Query :


https://graph.microsoft.com/v1.0/teams/f2a918a0-0b5c-4e58-8e83-9cabd24dea78/channels?$select=id,displayname


Response from Microsoft Graph API :


{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#teams('f2a918a0-0b5c-4e58-8e83-9cabd24dea78')/channels(id,displayName)",
    "value": [
        {
            "id": "19:b4939b320444438e801d00becdc1c894@thread.skype",
            "displayName": "General"
        },
        {
            "id": "19:429d54ef13b44d8c934c425ac4742e2b@thread.skype",
            "displayName": "Sales Status"
        }
    ]
}

Note : please have a look on syntax : "?$select=id,displayname"


3. Use of 'filter' along with the 'select' :


https://graph.microsoft.com/v1.0/teams/f2a918a0-0b5c-4e58-8e83-9cabd24dea78/channels?$filter=displayName eq 'General'&$select=id,displayname


Response from Microsoft Graph API :


{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#teams('f2a918a0-0b5c-4e58-8e83-9cabd24dea78')/channels(id,displayName)",
    "value": [
        {
            "id": "19:b4939b320444438e801d00becdc1c894@thread.skype",
            "displayName": "General"
        }
    ]

}

Note : please have a look on syntax below : 

For Select :  "?$select=id,displayname"
For Filter :  " ?$filter=displayName eq 'General'  "

'&' symbol has been used to separate select and filter.

Below query will also work : select used before filter.

https://graph.microsoft.com/v1.0/teams/f2a918a0-0b5c-4e58-8e83-9cabd24dea78/channels?$select=id,displayname&$filter=displayName eq 'General'


PowerShell implementation of above API
---------------------------------------------------------

#Specifying URI with filter and Select


$uri = "https://graph.microsoft.com/v1.0/groups/$GroupId/drive/root/children?`$filter=name eq '$ChannelName' &`$select=id"   



$HeaderDictionaryObj = [System.Collections.IDictionary] $authHeader


$result = Invoke-RestMethod -Method Get -Uri $uri -Headers $HeaderDictionaryObj

NOTE : To use "$" inside URI which is a string, we need to use `$ instead. Since, $ is used in PowerShell for variable declaration and reference, and if we use $ directly, PowerShell consider the postfix as variable.

e.g. `$select 


Refer below link for detailed information

https://www.powershellgallery.com/packages/OData/1.0/Content/en-US%5Cabout_OData_Query.help.txt

No comments:

Post a Comment

How to Customize Dynamics 365 with Power Apps - Updated UI

In this blog I will explain How to Customize Dynamics 365 with new updated UI Have a look on new menu structure below: See ...