Hey there, fellow JavaScript devs! Ready to dive into the world of SharePoint webhooks? Let's get you up and running with a user-facing integration in no time.
Webhooks in SharePoint are like your app's personal news feed. They let you know when something interesting happens, so you can react in real-time. For user-facing integrations, this means smoother, more responsive experiences. Think instant updates without constant polling. Cool, right?
Before we jump in, make sure you've got:
axios
and @microsoft/microsoft-graph-client
)Got all that? Great! Let's roll.
First things first, let's tell SharePoint what we want to know about. Here's how you do it with the Microsoft Graph API:
const { Client } = require('@microsoft/microsoft-graph-client'); require('isomorphic-fetch'); const client = Client.init({ authProvider: (done) => { done(null, 'YOUR_ACCESS_TOKEN'); // You'll need to implement your own token retrieval } }); async function createSubscription() { const subscription = { changeType: 'created,updated', notificationUrl: 'https://your-webhook-endpoint.com/api/notifications', resource: '/sites/{site-id}/lists/{list-id}', expirationDateTime: new Date(Date.now() + 43200 * 60000).toISOString(), // 30 days from now clientState: 'SecretClientState' }; try { const result = await client.api('/subscriptions').post(subscription); console.log('Subscription created:', result); } catch (error) { console.error('Error creating subscription:', error); } } createSubscription();
SharePoint's gonna want to make sure your endpoint is legit. Here's a quick Express.js snippet to handle that:
const express = require('express'); const app = express(); app.post('/api/notifications', (req, res) => { if (req.query && req.query.validationToken) { res.set('Content-Type', 'text/plain'); res.status(200).send(req.query.validationToken); } else { // Handle actual notifications here res.status(202).end(); } });
Now that we're all set up, let's listen for those sweet, sweet notifications:
app.post('/api/notifications', (req, res) => { if (req.query && req.query.validationToken) { // Validation handling (as above) } else { const notifications = req.body.value; notifications.forEach(async (notification) => { console.log('Received notification:', notification); // Do something awesome with the notification }); res.status(202).end(); } });
Subscriptions don't last forever. Here's how to keep the party going:
async function renewSubscription(subscriptionId) { const subscription = { expirationDateTime: new Date(Date.now() + 43200 * 60000).toISOString() // Another 30 days }; try { const result = await client.api(`/subscriptions/${subscriptionId}`).update(subscription); console.log('Subscription renewed:', result); } catch (error) { console.error('Error renewing subscription:', error); } }
Breaking up is hard, but sometimes necessary:
async function deleteSubscription(subscriptionId) { try { await client.api(`/subscriptions/${subscriptionId}`).delete(); console.log('Subscription deleted'); } catch (error) { console.error('Error deleting subscription:', error); } }
clientState
secret and verify it in incoming notifications.And there you have it! You're now ready to add real-time magic to your SharePoint integrations. Remember, this is just the beginning. As you get more comfortable, try experimenting with different change types or resources.
Happy coding, and may your notifications always be timely! 🚀