I am using the microsoft graph php sdk to manipulate a user's contacts. I can do CRUD operations on individual contacts, but I am trying to figure out if it is possible to process multiple contacts in a single request. This works fine for adding a single client:
$graph=new Graph;
$graph->setAccessToken($token);
$graph->createRequest('POST', $url)
->attachBody($contact)
->setReturnType(Contact::class)
->execute();
For bulk I tried
->attachBody([$contact1, $contact2, $contact3]);
and I get the 400 error:
"code": "BadRequest",
"message": "Empty Payload. JSON content expected.",
I got the same error when trying in graph explorer, POSTing to https://graph.microsoft.com/v1.0/me/contacts
with this body:
[
{
"emailAddresses": [
{
"name": "John Doe",
"address": "jdoe@example.com"
}
],
"givenName": "John"
},
{
"emailAddresses": [
{
"name": "John Doe",
"address": "jdoe@example.com"
}
],
"givenName": "John"
}
]
Is there a way to do this or is it not supported? I couldn't find a mention of it either way in the documentation.
Sometimes I have to add thousands of contacts and doing 1 per request seems tedious.
Same question for all the update operations: post, patch, delete.
Microsoft Graph has a Batching feature specifically for this type of scenario. Each operation is still against a single record, but batching allows you to make one call to execute up to 20 operations:
{
"requests": [
{
"id": "1",
"method": "POST",
"url": "/me/contacts",
"headers":{
"Content-Type":"application/json"
},
"body": {
"emailAddresses": [
{
"name": "John Doe",
"address": "jdoe@example.com"
}
],
"givenName": "John"
}
},
{
"id": "2",
"method": "POST",
"url": "/me/contacts",
"headers":{
"Content-Type":"application/json"
},
"body": {
"emailAddresses": [
{
"name": "John Doe",
"address": "jdoe@example.com"
}
],
"givenName": "John"
}
},
{
"id": "3",
"method": "POST",
"url": "/me/contacts",
"headers":{
"Content-Type":"application/json"
},
"body": {
"emailAddresses": [
{
"name": "John Doe",
"address": "jdoe@example.com"
}
],
"givenName": "John"
}
}
]
}