Mark all messages as Read in a Folder using the Microsoft Graph API
Being able to mark all the messages as read (or unread) on a Folder is an existing EWS feature that has made it into the Beta endpoint in the Microsoft Graph recently. Mark all as read is an important feature generally in Mail Clients but sometimes in Applications after performing particular actions. Previously in the Graph without the folder level function you would have to update each item individually or part of a 20 item batch which is both slow and will usually land you in throttling jail if you needed to update a large number of Items.
Things to be aware of when marking a lot of messages as read
Here's the main thing to watch out for when you're marking a bunch of unread messages as read: read receipts.
If you've got a lot of unread messages that requested read receipts, and you mark them as read, you could easily trigger a flood of "read" receipts being sent out.
The Graph endpoint does support suppressions of read recipients using an option in the post request.
Migrating from an EWS Application
If your migrating an EWS Application the MarkAllItemsAsRead
operation in EWS works in exactly the same way as the Graph endpoint. eg code like this is the EWS Managed APIFolderId inboxId = new FolderId(WellKnownFolderName.Inbox, "user@example.com");
Folder inboxFolder = Folder.Bind(service, inboxId);
inboxFolder.MarkAllItemsAsRead(true);
Can be replaced by this in the Graph SDK (this is beta code as this is only available in the beta endpoint at the moment)
using Microsoft.Graph.Beta.Me.MailFolders.Item.UpdateAllMessagesReadState;
var markAllAsReadRequestBody = new UpdateAllMessagesReadStatePostRequestBody
{
IsRead = true,
SuppressReadReceipts = true,
};
await graphClient.Me.MailFolders["Inbox"].UpdateAllMessagesReadState.PostAsync(markAllAsReadRequestBody);
Using it in the PowerShell Graph SDK
For the Beta SDK see https://learn.microsoft.com/en-us/graph/api/mailfolder-updateallmessagesreadstate?view=graph-rest-beta&tabs=powershell
For using in regular SDK via Invoke-GraphRequest use$uri = "https://graph.microsoft.com/beta/me/mailFolders/Inbox/updateAllMessagesReadState"
$body = @{
isRead = $true
} | ConvertTo-Json
Invoke-MgGraphRequest -Method POST -Uri $uri -Body $body -ContentType 'application/json'