Microsoft Graph Subscription call

Hattikoti, Roopa S 20 Reputation points
2023-09-15T05:45:37.7433333+00:00

Hello All,

Mail conversion : EWS to exchange online using Microsoft Graph.

Old Code :

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

New conversion : Using Microsoft Graph need same functionality

                var results = graphClient.Users["xxx.xxxx@dell.com"].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");
                }
                else
                {
                    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
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,234 questions
C#
C#
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
    2023-09-15T08:49:52.1266667+00:00

    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 = "https://your-endpoint.com/notificationListener"
        subscription.Resource = "users/xxx.xxxx@dell.com/mailFolders/Inbox/messages"
        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,
    Jalpa