Search code examples
microsoft-graph-apimicrosoft-graph-mail

How to count unread messages in Microsoft Graph


I'm trying to get a simple count of unread messages in a folder, but I'd like to just get a count without having to retrieve any of the message detail.

The closest I've got is something like this:

https://graph.microsoft.com/v1.0/users/{user}/mailFolders/Inbox/messages?$filter=isRead ne true&$count=true&$select=id&top=1

Notice that I'm including $select=id here, but I don't really want that - I'm just looking for a simple count.


Solution

  • Something like this is closer to what I need:

    https://graph.microsoft.com/v1.0/users/{user}/mailFolders/Inbox
    

    This gives some basic folder information for that folder, including the unreadItemCount.

    {
        "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('{user}')/mailFolders/$entity",
        "id": "AAMkADQwNDkzY2MwLWRkODMtNDdkYS05MjNmLWI0YTA4OTNlN2U1ZgAuAAAAAABpELO9F64CS6YLKzHVeiwoAQDXOQANV1mvRZBdzyuZRD-5AAAAAAEMAAA=",
        "displayName": "Inbox",
        "parentFolderId": "AAMkADQwNDkzY2MwLWRkODMtNDdkYS05MjNmLWI0YTA4OTNlN2U1ZgAuAAAAAABpELO9F64CS6YLKzHVeiwoAQDXOQANV1mvRZBdzyuZRG-5AAAAAAEIAAA=",
        "childFolderCount": 0,
        "unreadItemCount": 1,
        "totalItemCount": 2
    }