Microsoft Graph API - filtering on categories.

Ward Horsfall 101 Reputation points
2021-12-16T14:38:51.147+00:00

Hi,

For a specified mail folder how can I do the following?

  1. Display all mail messages with have either of a few categories set and only see the Subject, DateRecieved and Categories?
  2. Display all mail messages with have any categories set (not blank) and only see the Subject, DateRecieved?

Thanks,

Ward

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,038 questions
0 comments No comments
{count} votes

Accepted answer
  1. AtulThorat-MSFT 391 Reputation points
    2022-01-28T16:00:05.977+00:00

    Hi @Ward Horsfall ,

    Please use below graph API endpoint for getting messages from specified mail folder using Graph API.

    I have outlook mailbox and my inbox folder looks like below with mail with different categories.

    169338-mailbox-inbox-folder.png

    1.Display all mail messages which have either of a few categories set and only see the Subject, DateRecieved and Categories?

    Use Graph API like

    GET
    https://graph.microsoft.com/v1.0/me/mailFolders/{mailFolders id}/messages?$select=subject,receivedDateTime,categories&$filter=categories/any(a:a+eq+'Red+Category') or categories/any(a:a+eq+'Orange+Category')

    Among different outlook mail categories outlookcategory, here I am filtering inbox folder mail with "Red" or "Orange" category

    GET https://graph.microsoft.com/v1.0/me/mailFolders/AAMkADRlNzY0M2NmLTRmMGItNDUyYS1hYThhLThlNzE5NTVmMDZiOQAuAAAAAABR21-6ABv8TagD21M49AjtAQBgYaQ-pmuUR4IFYCzhSxPIAAAAAAEMAAA=/messages?$select=subject,receivedDateTime,categories&$filter=categories/any(a:a+eq+'Red+Category') or categories/any(a:a+eq+'Orange+Category')

    We will get response where only mail with either "Red" or "Orange" category will filtered out.

    {  
        "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('6ab5cc33-a9d6-40b1-a84f-993e06d170f3')/mailFolders('AAMkADRlNzY0M2NmLTRmMGItNDUyYS1hYThhLThlNzE5NTVmMDZiOQAuAAAAAABR21-6ABv8TagD21M49AjtAQBgYaQ-pmuUR4IFYCzhSxPIAAAAAAEMAAA%3D')/messages(subject,receivedDateTime,categories)",  
        "value": [  
            {  
                "@odata.etag": "W/\"CQAAABYAAABgYaQ/pmuUR4IFYCzhSxPIAAANcoPn\"",  
                "id": "AAMkADRlNzY0M2NmLTRmMGItNDUyYS1hYThhLThlNzE5NTVmMDZiOQBGAAAAAABR21-6ABv8TagD21M49AjtBwBgYaQ-pmuUR4IFYCzhSxPIAAAAAAEMAABgYaQ-pmuUR4IFYCzhSxPIAAAMRBDRAAA=",  
                "categories": [  
                    "Red category"  
                ],  
                "receivedDateTime": "2022-01-25T01:14:13Z",  
                "subject": "Major update from Message center"  
            },  
            {  
                "@odata.etag": "W/\"CQAAABYAAABgYaQ/pmuUR4IFYCzhSxPIAAANcoPx\"",  
                "id": "AAMkADRlNzY0M2NmLTRmMGItNDUyYS1hYThhLThlNzE5NTVmMDZiOQBGAAAAAABR21-6ABv8TagD21M49AjtBwBgYaQ-pmuUR4IFYCzhSxPIAAAAAAEMAABgYaQ-pmuUR4IFYCzhSxPIAAAK7462AAA=",  
                "categories": [  
                    "Red category"  
                ],  
                "receivedDateTime": "2022-01-24T05:09:02Z",  
                "subject": "Weekly digest: Microsoft service updates"  
            },  
            {  
                "@odata.etag": "W/\"CQAAABYAAABgYaQ/pmuUR4IFYCzhSxPIAAANcoP6\"",  
                "id": "AAMkADRlNzY0M2NmLTRmMGItNDUyYS1hYThhLThlNzE5NTVmMDZiOQBGAAAAAABR21-6ABv8TagD21M49AjtBwBgYaQ-pmuUR4IFYCzhSxPIAAAAAAEMAABgYaQ-pmuUR4IFYCzhSxPIAAAJZLYnAAA=",  
                "categories": [  
                    "Orange category"  
                ],  
                "receivedDateTime": "2022-01-21T02:49:31Z",  
                "subject": "Major update from Message center"  
            },  
            {  
                "@odata.etag": "W/\"CQAAABYAAABgYaQ/pmuUR4IFYCzhSxPIAAANcoQD\"",  
                "id": "AAMkADRlNzY0M2NmLTRmMGItNDUyYS1hYThhLThlNzE5NTVmMDZiOQBGAAAAAABR21-6ABv8TagD21M49AjtBwBgYaQ-pmuUR4IFYCzhSxPIAAAAAAEMAABgYaQ-pmuUR4IFYCzhSxPIAAAGStDNAAA=",  
                "categories": [  
                    "Orange category"  
                ],  
                "receivedDateTime": "2022-01-20T12:48:32Z",  
                "subject": "Reminder: Initial consult"  
            },  
            {  
                "@odata.etag": "W/\"CQAAABYAAABgYaQ/pmuUR4IFYCzhSxPIAAANcoQN\"",  
                "id": "AAMkADRlNzY0M2NmLTRmMGItNDUyYS1hYThhLThlNzE5NTVmMDZiOQBGAAAAAABR21-6ABv8TagD21M49AjtBwBgYaQ-pmuUR4IFYCzhSxPIAAAAAAEMAABgYaQ-pmuUR4IFYCzhSxPIAAAGStDMAAA=",  
                "categories": [  
                    "Orange category"  
                ],  
                "receivedDateTime": "2022-01-20T11:13:32Z",  
                "subject": "Reminder: Initial consult"  
            },  
            {  
                "@odata.etag": "W/\"CQAAABYAAABgYaQ/pmuUR4IFYCzhSxPIAAANcoRN\"",  
                "id": "AAMkADRlNzY0M2NmLTRmMGItNDUyYS1hYThhLThlNzE5NTVmMDZiOQBGAAAAAABR21-6ABv8TagD21M49AjtBwBgYaQ-pmuUR4IFYCzhSxPIAAAAAAEMAABgYaQ-pmuUR4IFYCzhSxPIAAAGStDFAAA=",  
                "categories": [  
                    "Red category"  
                ],  
                "receivedDateTime": "2022-01-17T05:05:20Z",  
                "subject": "Weekly digest: Microsoft service updates"  
            },  
            {  
                "@odata.etag": "W/\"CQAAABYAAABgYaQ/pmuUR4IFYCzhSxPIAAANcoRW\"",  
                "id": "AAMkADRlNzY0M2NmLTRmMGItNDUyYS1hYThhLThlNzE5NTVmMDZiOQBGAAAAAABR21-6ABv8TagD21M49AjtBwBgYaQ-pmuUR4IFYCzhSxPIAAAAAAEMAABgYaQ-pmuUR4IFYCzhSxPIAAAE8SyhAAA=",  
                "categories": [  
                    "Red category"  
                ],  
                "receivedDateTime": "2022-01-14T10:38:49Z",  
                "subject": "New booking: Shahana S for Initial consult"  
            },  
            {  
                "@odata.etag": "W/\"CQAAABYAAABgYaQ/pmuUR4IFYCzhSxPIAAANcoRf\"",  
                "id": "AAMkADRlNzY0M2NmLTRmMGItNDUyYS1hYThhLThlNzE5NTVmMDZiOQBGAAAAAABR21-6ABv8TagD21M49AjtBwBgYaQ-pmuUR4IFYCzhSxPIAAAAAAEMAABgYaQ-pmuUR4IFYCzhSxPIAAACwlDKAAA=",  
                "categories": [  
                    "Red category"  
                ],  
                "receivedDateTime": "2022-01-13T16:54:09Z",  
                "subject": "New booking: sdsd for Initial consult"  
            }  
        ]  
    }  
    

    169501-red-or-orange-category.png

    2.Display all mail messages which have any categories set (not blank) and only see the Subject, DateRecieved?

    Use Graph API like

    GET
    https://graph.microsoft.com/v1.0/me/mailFolders/{mailFolders id}/messages?$select=subject,receivedDateTime,categories&$filter=categories/any(a:a+eq+'Red+Category')

    Among different outlook mail categories outlookcategory, here I am filtering inbox folder mail with "Red" category.

    GET

    https://graph.microsoft.com/v1.0/me/mailFolders/AAMkADRlNzY0M2NmLTRmMGItNDUyYS1hYThhLThlNzE5NTVmMDZiOQAuAAAAAABR21-6ABv8TagD21M49AjtAQBgYaQ-pmuUR4IFYCzhSxPIAAAAAAEMAAA=/messages?$select=subject,receivedDateTime,categories&$filter=categories/any(a:a+eq+'Red+Category')

    We will get response where only mail with "Red" category will filtered out.

    {  
        "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('6ab5cc33-a9d6-40b1-a84f-993e06d170f3')/mailFolders('AAMkADRlNzY0M2NmLTRmMGItNDUyYS1hYThhLThlNzE5NTVmMDZiOQAuAAAAAABR21-6ABv8TagD21M49AjtAQBgYaQ-pmuUR4IFYCzhSxPIAAAAAAEMAAA%3D')/messages(subject,receivedDateTime,categories)",  
        "value": [  
            {  
                "@odata.etag": "W/\"CQAAABYAAABgYaQ/pmuUR4IFYCzhSxPIAAANcoPn\"",  
                "id": "AAMkADRlNzY0M2NmLTRmMGItNDUyYS1hYThhLThlNzE5NTVmMDZiOQBGAAAAAABR21-6ABv8TagD21M49AjtBwBgYaQ-pmuUR4IFYCzhSxPIAAAAAAEMAABgYaQ-pmuUR4IFYCzhSxPIAAAMRBDRAAA=",  
                "categories": [  
                    "Red category"  
                ],  
                "receivedDateTime": "2022-01-25T01:14:13Z",  
                "subject": "Major update from Message center"  
            },  
            {  
                "@odata.etag": "W/\"CQAAABYAAABgYaQ/pmuUR4IFYCzhSxPIAAANcoPx\"",  
                "id": "AAMkADRlNzY0M2NmLTRmMGItNDUyYS1hYThhLThlNzE5NTVmMDZiOQBGAAAAAABR21-6ABv8TagD21M49AjtBwBgYaQ-pmuUR4IFYCzhSxPIAAAAAAEMAABgYaQ-pmuUR4IFYCzhSxPIAAAK7462AAA=",  
                "categories": [  
                    "Red category"  
                ],  
                "receivedDateTime": "2022-01-24T05:09:02Z",  
                "subject": "Weekly digest: Microsoft service updates"  
            },  
            {  
                "@odata.etag": "W/\"CQAAABYAAABgYaQ/pmuUR4IFYCzhSxPIAAANcoRN\"",  
                "id": "AAMkADRlNzY0M2NmLTRmMGItNDUyYS1hYThhLThlNzE5NTVmMDZiOQBGAAAAAABR21-6ABv8TagD21M49AjtBwBgYaQ-pmuUR4IFYCzhSxPIAAAAAAEMAABgYaQ-pmuUR4IFYCzhSxPIAAAGStDFAAA=",  
                "categories": [  
                    "Red category"  
                ],  
                "receivedDateTime": "2022-01-17T05:05:20Z",  
                "subject": "Weekly digest: Microsoft service updates"  
            },  
            {  
                "@odata.etag": "W/\"CQAAABYAAABgYaQ/pmuUR4IFYCzhSxPIAAANcoRW\"",  
                "id": "AAMkADRlNzY0M2NmLTRmMGItNDUyYS1hYThhLThlNzE5NTVmMDZiOQBGAAAAAABR21-6ABv8TagD21M49AjtBwBgYaQ-pmuUR4IFYCzhSxPIAAAAAAEMAABgYaQ-pmuUR4IFYCzhSxPIAAAE8SyhAAA=",  
                "categories": [  
                    "Red category"  
                ],  
                "receivedDateTime": "2022-01-14T10:38:49Z",  
                "subject": "New booking: Shahana S for Initial consult"  
            },  
            {  
                "@odata.etag": "W/\"CQAAABYAAABgYaQ/pmuUR4IFYCzhSxPIAAANcoRf\"",  
                "id": "AAMkADRlNzY0M2NmLTRmMGItNDUyYS1hYThhLThlNzE5NTVmMDZiOQBGAAAAAABR21-6ABv8TagD21M49AjtBwBgYaQ-pmuUR4IFYCzhSxPIAAAAAAEMAABgYaQ-pmuUR4IFYCzhSxPIAAACwlDKAAA=",  
                "categories": [  
                    "Red category"  
                ],  
                "receivedDateTime": "2022-01-13T16:54:09Z",  
                "subject": "New booking: sdsd for Initial consult"  
            }  
        ]  
    }  
    

    169397-red-category.png

    Hope this helps.

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".


0 additional answers

Sort by: Most helpful