M365.FM - Modern work, security, and productivity with Microsoft 365
Welcome to another episode of Knowledge Nuggets with Mirko Peters. Today we're exploring Microsoft Graph Webhooks, one of the core building blocks for creating modern, event-driven Microsoft 365 applications. Almost every developer building with Microsoft 365 eventually faces the same challenge. How do you know when something changes? Whether you're waiting for a new user to be created in Microsoft Entra ID, a document to be uploaded to SharePoint, or an email to arrive in Outlook, the traditional solution has always been polling—repeatedly asking Microsoft Graph if anything has changed. The problem is that most of those requests return nothing, wasting API calls, increasing latency, and eventually leading to Microsoft Graph throttling. Microsoft Graph Webhooks solve this by completely changing the communication model. Instead of your application constantly asking Microsoft Graph for updates, Microsoft Graph automatically sends your application an HTTP request whenever something important happens. In this episode, we'll explore how Graph Webhooks work, why they're more efficient than polling, how subscriptions and validation fit together, and why combining webhooks with Delta Query creates one of the most reliable integration patterns available for Microsoft 365. WHY POLLING DOESN'T SCALE Before understanding webhooks, it's important to understand the limitations of polling. Imagine checking your mailbox every five minutes throughout the day. Most of the time, there's nothing inside. Yet you continue making the same trip over and over again. Traditional applications behave exactly the same way. They repeatedly send requests asking Microsoft Graph whether anything has changed. Most requests receive exactly the same response: nothing has happened. Although each request seems harmless, thousands of applications polling Microsoft Graph every few minutes quickly generate enormous amounts of unnecessary traffic. This creates several problems. Applications consume API quotas without receiving useful information, users experience delays because changes aren't detected until the next polling cycle, and Microsoft Graph eventually begins throttling applications that generate excessive traffic. Developers often refer to this as ending up in "Graph jail." Ironically, even aggressive polling still doesn't provide true real-time updates. If a user account is created one minute after the last request, the application may not discover the change for another four minutes. Microsoft Graph Webhooks eliminate this inefficiency entirely by notifying applications only when something actually changes. WHAT ARE MICROSOFT GRAPH WEBHOOKS? A webhook is simply an HTTP POST request sent automatically when an event occurs. Instead of asking Microsoft Graph for updates, your application waits for Microsoft Graph to contact it. A useful analogy is a doorbell. Without a doorbell, you repeatedly open your front door to check whether someone has arrived. With a doorbell, you simply wait until someone rings. Microsoft Graph Webhooks work exactly the same way. Your application provides Microsoft Graph with a secure HTTPS endpoint. Microsoft Graph continuously monitors the resources you've subscribed to. Whenever a relevant event occurs, Microsoft Graph immediately sends an HTTP POST request containing information about that event. Microsoft refers to these webhook events as Change Notifications, but the underlying concept remains the same. Whether monitoring Outlook mailboxes, SharePoint document libraries, Microsoft Teams messages, calendars, users, groups, or Microsoft Entra ID objects, Graph Webhooks allow applications to respond almost instantly without unnecessary polling. HOW GRAPH WEBHOOKS WORK The complete webhook process consists of three simple building blocks. Everything starts with a subscription. Your application tells Microsoft Graph exactly which resource it wants to monitor and where notifications should be delivered. Once the subscription has been created, Microsoft Graph begins monitoring the selected resource. Whenever a matching event occurs—such as a new user being created or a SharePoint document being updated—Microsoft Graph immediately generates a notification. That notification is delivered as an HTTP POST request to your application's endpoint. Your application receives the notification, acknowledges it, and performs whatever business logic is required. Although the overall architecture appears sophisticated, the actual workflow is remarkably simple. You subscribe. Microsoft Graph watches. Microsoft Graph notifies your application whenever something changes. CREATING A SUBSCRIPTION Every webhook begins with creating a subscription through Microsoft Graph. When creating the subscription, four pieces of information are required. The first is the notification URL—the secure HTTPS endpoint where Microsoft Graph should deliver notifications. The second is the resource being monitored. This could be users, groups, Outlook messages, SharePoint files, Teams chats, calendars, or many other Microsoft Graph resources. Next comes the change type, allowing applications to receive notifications only for newly created items, updates, deletions, or any combination of these events. Finally, every subscription includes an expiration date. Unlike permanent registrations, Microsoft Graph subscriptions automatically expire after a predefined period. Depending on the resource, this may range from only a few days to approximately thirty days. Applications can also provide a clientState value—a secret string included with every notification. This simple mechanism allows applications to verify that notifications genuinely originated from Microsoft Graph rather than an external source attempting to send fraudulent requests. Once the subscription is successfully created, Microsoft Graph begins monitoring the selected resource automatically. THE VALIDATION HANDSHAKE Before Microsoft Graph trusts an endpoint, it performs a verification process known as the validation handshake. Immediately after a subscription request is received, Microsoft Graph sends a validation request containing a unique validation token. Your endpoint must return that token as plain text within a short time window. A useful analogy is signing for a package delivery. Before leaving an important package, the courier confirms someone is actually available to receive it. Microsoft Graph performs exactly the same check before delivering notifications. If the endpoint successfully returns the validation token, Microsoft Graph activates the subscription. If validation fails, the subscription is never created. One detail that frequently causes problems is the response format. The validation token must be returned as plain text, not JSON or HTML, and the endpoint must support secure HTTPS communication. Although the validation process occurs only once during subscription creation, getting it right is essential because no notifications will ever arrive if validation fails. PROCESSING NOTIFICATIONS SECURELY Once validation succeeds, Microsoft Graph begins sending notifications whenever monitored resources change. Each notification arrives as a JSON payload containing information about the event, including the affected resource, the type of change, timestamps, and the clientState value originally supplied during subscription creation. The very first step should always be validating the clientState. If the received value doesn't match the expected secret, the notification should be rejected immediately. Applications should also respond extremely quickly. Microsoft Graph expects a successful HTTP response within approximately three seconds. If responses become consistently slow, Microsoft Graph begins delaying future notifications. If endpoint performance deteriorates further, notifications may eventually be dropped entirely. For this reason, production applications rarely perform business logic directly inside the webhook endpoint. Instead, the endpoint immediately validates the notification, returns a successful HTTP response, and places the event into a background queue for asynchronous processing. This design keeps webhook endpoints responsive while allowing complex processing to occur separately without risking notification delivery. KEEPING SUBSCRIPTIONS ALIVE One of the most commonly overlooked aspects of Graph Webhooks is subscription management. Subscriptions automatically expire. If they aren't renewed before reaching their expiration date, Microsoft Graph simply stops sending notifications. Renewing a subscription is straightforward. Applications send an update request extending the expiration time before the current subscription expires. Microsoft Graph also provides Lifecycle Notifications, allowing applications to receive advance warnings whenever subscriptions are approaching expiration or authentication tokens require renewal. Rather than discovering failures after notifications stop arriving, applications can proactively renew subscriptions and maintain uninterrupted operation. Proper subscription lifecycle management is therefore just as important as creating the original subscription itself. WEBHOOKS AND DELTA QUERY: THE PERFECT COMBINATION Although webhooks provide immediate awareness of changes, they intentionally contain only limited information. Their purpose is simply to inform your application that something happened. To retrieve complete details, Microsoft recommends combining webhooks with Delta Query. The workflow is elegant. A webhook arrives indicating that a resource has changed. Become a supporter of this podcast: https://www.spreaker.com/podcast/m365-fm-modern-work-security-and-productivity-with-microsoft-365--6704921/support [https://www.spreaker.com/podcast/m365-fm-modern-work-security-and-productivity-with-microsoft-365--6704921/support?utm_source=rss&utm_medium=rss&utm_campaign=rss].
853 episodios
Comentarios
0Sé la primera persona en comentar
¡Regístrate ahora y únete a la comunidad de M365.FM - Modern work, security, and productivity with Microsoft 365!