Skip to content

Instantly share code, notes, and snippets.

@xen0bit
Created November 26, 2020 15:02
Show Gist options
  • Save xen0bit/dc11b49b3900a6aca74475edf73996b4 to your computer and use it in GitHub Desktop.
Save xen0bit/dc11b49b3900a6aca74475edf73996b4 to your computer and use it in GitHub Desktop.
graph api multiple users
const options = {
authProvider,
};
const client = Client.init(options);
//Despite "supporting" id's for requests, I seem to remember them being required last time I tried it?
const batchCreateUsers = {
"requests": [{
"id": "1",
"method": "POST",
"url": "/users",
"body": {
"accountEnabled": true,
"displayName": "displayName-value01",
"mailNickname": "mailNickname-value01",
"userPrincipalName": "upn-value01@tenant-value.onmicrosoft.com",
"passwordProfile": {
"forceChangePasswordNextSignIn": true,
"password": "{password-value}"
}
},
"headers": {
"Content-Type": "application/json"
}
}, {
"id": "2",
"method": "POST",
"url": "/users",
"body": {
"accountEnabled": true,
"displayName": "displayName-value02",
"mailNickname": "mailNickname-value02",
"userPrincipalName": "upn-value02@tenant-value.onmicrosoft.com",
"passwordProfile": {
"forceChangePasswordNextSignIn": true,
"password": "{password-value}"
}
},
"headers": {
"Content-Type": "application/json"
}
}
]
}
let res = await client.api('/$batch')
.post(batchCreateUsers);
//Should be roughly equivalent to
/*POST /v1.0/$batch HTTP/1.1
Host: graph.microsoft.com
Whatever auth headers you're using
Content-Type: application/json
{
"requests": [{
"id": "1",
"method": "POST",
"url": "/users",
"body": {
"accountEnabled": true,
"displayName": "displayName-value01",
"mailNickname": "mailNickname-value01",
"userPrincipalName": "upn-value01@tenant-value.onmicrosoft.com",
"passwordProfile": {
"forceChangePasswordNextSignIn": true,
"password": "{password-value}"
}
},
"headers": {
"Content-Type": "application/json"
}
}, {
"id": "2",
"method": "POST",
"url": "/users",
"body": {
"accountEnabled": true,
"displayName": "displayName-value02",
"mailNickname": "mailNickname-value02",
"userPrincipalName": "upn-value02@tenant-value.onmicrosoft.com",
"passwordProfile": {
"forceChangePasswordNextSignIn": true,
"password": "{password-value}"
}
},
"headers": {
"Content-Type": "application/json"
}
}
]
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment