I'm trying to manage a distribution list in ms 365 from my website where I keep all user info. I'd like the users to be added to a distribution list with their personal email and so without being a member of the ms365 environment.
$graph = new Graph();
$graph
->setBaseUrl("https://graph.microsoft.com/")
->setApiVersion("beta")
->setAccessToken($this->accessToken);
$group_id = 'xxxxx-xxxx-xxxx-xxxx-xxxxxxxxx';
$data = json_encode(array(
"GivenName" => "John",
"Surname" => "Doe",
"EmailAddresses" => array(
array(
"Address" => "[email protected]",
"Name" => "John Doe"
)
)
));
$response = $graph->createRequest("post", "/groups/{$group_id}/members")
->addHeaders(array("Content-Type" => "application/json"))
->setReturnType(Model\User::class)
->attachBody($data)
->setTimeout("1000")
->execute();
The response I get is:
Uncaught GuzzleHttp\Exception\ClientException: Client error:
POST https://graph.microsoft.com/beta/groups/xxxxx-xxxx-xxxx-xxxx-xxxxxxxxx/members
resulted in a400 Bad Request
response: {"error":{"code":"Request_BadRequest","message":"Unsupported resource type 'DirectoryObject' for operation 'Create'.","i (truncated...)
Further error stack:
C:\xampp\htdocs\site\content\libraries\MicrosoftGraph\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php:113 Stack trace: #0
C:\xampp\htdocs\site\content\libraries\MicrosoftGraph\vendor\guzzlehttp\guzzle\src\Middleware.php(69): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(GuzzleHttp\Psr7\Response), NULL, Array, NULL) #1
C:\xampp\htdocs\site\content\libraries\MicrosoftGraph\vendor\guzzlehttp\promises\src\Promise.php(204): GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Response)) #2
C:\xampp\htdocs\site\content\libraries\MicrosoftGraph\vendor\guzzlehttp\promises\src\Promise.php(153): GuzzleHttp\Promise\Promise::callHandler(1, Object(GuzzleHttp\Psr7\Response), NULL) #3
C:\xampp\htdocs\site\content\libraries\MicrosoftGraph\vendor\guzzlehttp\promises\src\TaskQueue.php(48): GuzzleHttp\Promise\Promise::GuzzleHttp\Promise\{closure}() #4
C:\xampp\htdocs\site\content\libraries\MicrosoftGraph\vendor\guzzlehttp\promises\src\Promise.php(248): GuzzleHttp\Promise\TaskQueue->run(true) #5
C:\xampp\htdocs\site\content\libraries\MicrosoftGraph\vendor\guzzlehttp\promises\src\Promise.php(224): GuzzleHttp\Promise\Promise->invokeWaitFn() #6
C:\xampp\htdocs\site\content\libraries\MicrosoftGraph\vendor\guzzlehttp\promises\src\Promise.php(269): GuzzleHttp\Promise\Promise->waitIfPending() #7
C:\xampp\htdocs\site\content\libraries\MicrosoftGraph\vendor\guzzlehttp\promises\src\Promise.php(226): GuzzleHttp\Promise\Promise->invokeWaitList() #8
C:\xampp\htdocs\site\content\libraries\MicrosoftGraph\vendor\guzzlehttp\promises\src\Promise.php(62): GuzzleHttp\Promise\Promise->waitIfPending() #9
C:\xampp\htdocs\site\content\libraries\MicrosoftGraph\vendor\guzzlehttp\guzzle\src\Client.php(187): GuzzleHttp\Promise\Promise->wait() #10
C:\xampp\htdocs\site\content\libraries\MicrosoftGraph\vendor\microsoft\microsoft-graph\src\Http\GraphRequest.php(270): GuzzleHttp\Client->request('post', 'beta/groups/aa2...', Array)
Now, from the documentation I understand the proper way to add one to a list/group is by using:
"@odata.id": "https://graph.microsoft.com/beta/directoryObjects/{id}"
with its user ID but I'd like to use a non-existing user. From within the portal I can do this as admin but is there a way for the graph API to fix this as well? https://learn.microsoft.com/en-us/graph/api/group-post-members?view=graph-rest-beta&tabs=http
I never used this api, though, the documentations does not say that {id} should refer to an existing user, it says:
In the request body, supply a JSON representation of the id of the directoryObject, user, or group object you want to add.
So I'm guessing if you post to the directory object it should work
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With