Back

Reading and Writing Data Using the Microsoft Intune API

Aug 8, 20245 minute read

Hey there, fellow JavaScript devs! Ready to dive into the world of Microsoft Intune API? Let's get our hands dirty with some data syncing for user-facing integrations. Buckle up!

Setting the Stage

Before we jump in, make sure you've got Node.js installed and you're comfortable with async/await syntax. Oh, and you'll need an Azure AD app registration - but I'm sure you've done that dance before.

Authentication: Your Backstage Pass

First things first, let's get that access token. It's like your VIP pass to the Intune API party.

const msal = require('@azure/msal-node'); const config = { auth: { clientId: 'YOUR_CLIENT_ID', authority: 'https://login.microsoftonline.com/YOUR_TENANT_ID' } }; const cca = new msal.ConfidentialClientApplication(config); async function getToken() { const result = await cca.acquireTokenByClientCredential({ scopes: ['https://graph.microsoft.com/.default'] }); return result.accessToken; }

Reading Data: The Intune Buffet

Now that we're in, let's grab some data. How about device info?

const axios = require('axios'); async function getDevices(token) { const response = await axios.get('https://graph.microsoft.com/v1.0/deviceManagement/managedDevices', { headers: { Authorization: `Bearer ${token}` } }); return response.data.value; }

Writing Data: Leave Your Mark

Time to make some changes. Let's update a device's compliance state:

async function updateDeviceCompliance(token, deviceId, isCompliant) { await axios.patch(`https://graph.microsoft.com/v1.0/deviceManagement/managedDevices/${deviceId}`, { complianceState: isCompliant ? 'compliant' : 'noncompliant' }, { headers: { Authorization: `Bearer ${token}` } }); }

Sync Like a Pro

Here's a basic sync function to get you started:

async function syncDevices() { const token = await getToken(); const devices = await getDevices(token); for (const device of devices) { // Your sync logic here console.log(`Syncing device: ${device.id}`); // Maybe update some local database? } }

Performance Boosters

Want to level up? Use delta queries to fetch only what's changed:

async function getDeltaDevices(token, deltaLink = null) { const url = deltaLink || 'https://graph.microsoft.com/v1.0/deviceManagement/managedDevices/delta'; const response = await axios.get(url, { headers: { Authorization: `Bearer ${token}` } }); return { devices: response.data.value, deltaLink: response.data['@odata.deltaLink'] }; }

Real-time Updates with Webhooks

Stay on your toes with webhooks:

const express = require('express'); const app = express(); app.post('/webhook', express.json(), (req, res) => { const { value } = req.body; value.forEach(notification => { console.log(`Change detected: ${notification.resourceData.id}`); // Handle the change }); res.sendStatus(202); });

Wrapping Up

There you have it! You're now equipped to read, write, and sync data like a champ using the Microsoft Intune API. Remember to play nice with rate limits, keep your tokens safe, and log everything - your future self will thank you.

Keep coding, stay curious, and may your API calls always return 200 OK! 🚀