How to retrieve message from a change notification using the latest Graph sdk.

Erik Smit 35 Reputation points
2023-05-04T10:27:46.5266667+00:00

I am using the Graph api to receive change notifications about received messages.

When a notification is received i want to retrieve the message that was received.

I used to use this snipped to achieve this.

var request = new MessageRequest($"{client.BaseUrl}/{notification.Resource}?$expand=microsoft.graph.eventMessage/event", client, null);
var message = await request.GetAsync();

However after updating the sdk to the latest version (5.8.0) the MessageRequest class no longer exists and i cannot find the proper way to generate the request using the RequestAdapter.

What is the (now) intended way to get the Message from a ChangeNotification?

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

Accepted answer
  1. Emin Osmanbegovic 75 Reputation points
    2023-05-16T13:59:13.5+00:00

    Hello @Erik Smit

    After some tinkering and trying to get my own listener working, I've managed to make it work. So here are some snippets which I hope will help you!

    So first of all my Notification class looks the same as yours. When I receive a notification this is how I parse it in my GraphListenerController:
    User's image

    Looking at line 66, it gets the message from _graphService.GetMessage(current), and here is the part of the method you need:
    User's image

    Line 156 splits notification.Resource by the '/' character (Resource is User/{userId}/Messages{messageId}), and from there I get the message and user Id. With that information I can just make a request in line 160 to get the exact message.
    Also, using notification.ClientState I am able to get the mailbox in which it is located. From there I just map the values to an object which is sent to the frontend via signalR (line 71 in the 1st screenshot).

    Hope this solution helps you!
    If you have any questions feel free to ask.

    Best regards,
    Emin


2 additional answers

Sort by: Most helpful
  1. TH-4749-MSFT 3,295 Reputation points
    2023-05-04T19:37:58.3433333+00:00

    Hi Erik Smit,

    Thanks for responding. Please try using EventMessageRequest instead of MessageRequest with the same parameters. https://learn.microsoft.com/en-us/graph/api/resources/eventmessagerequest?view=graph-rest-1.0. Please let us know if this worked for you. If not please provide the information on thenotification.Resource variable used in the call.

    Thanks.


  2. Erik Smit 35 Reputation points
    2023-05-11T08:46:12.34+00:00

    On an unrelated note there is also no longer an obvious way to deserialize the change notifications, using the default JsonSerializer returns an object with all values set to null. In the older version of the SDK it was possible to access the Jsonserializer in the GraphServiceClient as seen here https://github.com/microsoftgraph/aspnetcore-webhooks-sample/blob/c4a44ca5790ccf767f4fce4f6672fb12a5c29e62/src/GraphWebhooks/Controllers/ListenController.cs#L76.

    But the httpProvider of the client seems to be removed now and i cannot seem to recreate the same serializer, leaving me unable to deserialize the notifications. As you can see in the image the json content is present, but attempting to deserialize it results in an empty object, but no exception. Playing around with settings like IgnoreCase has not changed the result.

    Untitled

    The notifications content looks like this, it should be valid

    {
        "value": [
            {
                "subscriptionId": "0f04acfb-32a0-4245-be2c-59791e4a0b08",
                "subscriptionExpirationDateTime": "2023-05-14T06:47:25.1777029+00:00",
                "changeType": "created",
                "resource": "Users/3f31f65e-0c4e-4461-9a02-1ef5fe7a7491/Messages/AAMkADBkZDAyOTQwLTUzYjQtNDNmZi05Y2JiLWE1MTEzMjQwY2JmYwBGAAAAAAAOwTC6ar4tTbBP5UVqnFI_BwDJc5CKhoqFQKvCt8lPUNP-AAAAAAEMAADJc5CKhoqFQKvCt8lPUNP-AAA9qYKxAAA=",
                "resourceData": {
                    "@odata.type": "#Microsoft.Graph.Message",
                    "@odata.id": "Users/3f31f65e-0c4e-4461-9a02-1ef5fe7a7491/Messages/AAMkADBkZDAyOTQwLTUzYjQtNDNmZi05Y2JiLWE1MTEzMjQwY2JmYwBGAAAAAAAOwTC6ar4tTbBP5UVqnFI_BwDJc5CKhoqFQKvCt8lPUNP-AAAAAAEMAADJc5CKhoqFQKvCt8lPUNP-AAA9qYKxAAA=",
                    "@odata.etag": "W/\"CQAAABYAAADJc5CKhoqFQKvCt8lPUNP/AAA9n3Sx\"",
                    "id": "AAMkADBkZDAyOTQwLTUzYjQtNDNmZi05Y2JiLWE1MTEzMjQwY2JmYwBGAAAAAAAOwTC6ar4tTbBP5UVqnFI_BwDJc5CKhoqFQKvCt8lPUNP-AAAAAAEMAADJc5CKhoqFQKvCt8lPUNP-AAA9qYKxAAA="
                },
                "clientState": "zpsubQjdLCVP8yJbZhEj1mKItOvsKAdYNxTq0n5TYm4QvK61D4riXJxtP0Jogfa8iYeFE6idOYWIunkudrzjzf50Zocxi1Uv0WdNrXLWzeVGUMnw4wipBZqW",
                "tenantId": "a074e7b8-8ab3-42b7-ace4-ea0c43571ecb"
            }
        ]
    }