Microsoft Graph Subscription call

Hattikoti, Roopa S 20 Reputation points

Hello All,

Mail conversion : EWS to exchange online using Microsoft Graph.

Old Code :

StreamingSubscription subscription = service.SubscribeToStreamingNotifications(
                    new FolderId[] { SharedMailbox },
                StreamingSubscriptionConnection connection = new StreamingSubscriptionConnection(service, 30);
                connection.OnNotificationEvent += OnNotificationEvent;
                connection.OnDisconnect += OnDisconnect;
                Console.WriteLine("(" + System.DateTime.Now + ") Syncing Mailbox...");

New conversion : Using Microsoft Graph need same functionality

                var results = graphClient.Users[""].MailFolders["Inbox"].Messages;

                var requestBody = new Subscription
                    ChangeType = "updated",
                    //NotificationUrl = "",
                    Resource = results.ToString(),
                    ExpirationDateTime = DateTimeOffset.Parse("2800-11-20T18:23:45.9356913Z"),
                    ClientState = clientSecret,
                    //LatestSupportedTlsVersion = "v1_2",
                var result = await graphClient.Subscriptions.PostAsync(requestBody);

                if (result)
                    Console.WriteLine("Process the files");
                    Console.WriteLine("Check the notification again");

Is notification URL is mandatory in microsoft graph subscription? I just want the result if there is any new mail.

Once after executing the new code I am getting Exception of type 'Microsoft.Graph.Models.ODataErrors.ODataError' was thrown.

Please suggest me on this.

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,448 questions
A set of technologies in the .NET Framework for building web applications and XML web services.
3,234 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,164 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jalpa Panchal-MSFT 480 Reputation points Microsoft Vendor

    Hi @Hattikoti, Roopa S,

    The NotificationUrl is mandatory when creating a subscription using Microsoft Graph. Microsoft Graph will send a POST request with the change notification to this URL. This endpoint should be accessible publicly. The error message you provided is generic. You will need to catch this exception and inspect its details to get a more specific idea of what's wrong.

    Here is the sample code:

    Dim subscription As New Subscription()
        subscription.ChangeType = "created"
        subscription.NotificationUrl = ""
        subscription.Resource = "users/"
        subscription.ExpirationDateTime = DateTimeOffset.UtcNow.AddHours(24) 'Note: Subscriptions cannot be eternal, they have a max lifespan
        subscription.ClientState = clientSecret
        Dim result = Await graphClient.Subscriptions.Request().AddAsync(subscription)

    Here is the document you can refer.

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

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,