|
| 1 | +## 1. GET ALL USERS IN A TENANT (GET /users) |
| 2 | + |
| 3 | +```php |
| 4 | +$graphServiceClient = new GraphServiceClient($tokenRequestContext); |
| 5 | + |
| 6 | +try { |
| 7 | + $users = $graphServiceClient->users()->get()->wait(); |
| 8 | + if ($users && $users->getValue()) { |
| 9 | + foreach ($users->getValue() as $user) { |
| 10 | + echo "User ID: {$user->getId()}<br>"; |
| 11 | + echo "User Display Name: {$user->getDisplayName()}<br>"; |
| 12 | + echo "User Mail: {$user->getMail()}<br><br>"; |
| 13 | + } |
| 14 | + } |
| 15 | +} catch (ApiException $ex) { |
| 16 | + echo "Error: " . $ex->getResponseStatusCode() . "\n"; |
| 17 | + echo "Error: " .$ex->getError()->getMessage();"; |
| 18 | +} |
| 19 | +``` |
| 20 | + |
| 21 | +## 2. GET A SPECIFIC USER (GET /users/{id | userPrincipalName}) |
| 22 | + |
| 23 | +```php |
| 24 | +$graphServiceClient = new GraphServiceClient($tokenRequestContext); |
| 25 | + |
| 26 | +$userId = "<USER-ID>"; |
| 27 | + |
| 28 | +try { |
| 29 | + $user = $graphServiceClient->users()->byUserId($userId)->get()->wait(); |
| 30 | + if ($user) { |
| 31 | + echo "User Principal Name: {$user->getUserPrincipalName()}<br>"; |
| 32 | + echo "User Display Name: {$user->getDisplayName()}<br>"; |
| 33 | + echo "User ID: {$user->getId()}<br>"; |
| 34 | + } |
| 35 | +} catch (ApiException $ex) { |
| 36 | + echo "Error: " . $ex->getResponseStatusCode() . "\n"; |
| 37 | + echo "Error: " .$ex->getError()->getMessage();"; |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +## 3. SEARCH USER BY NAME (GET /users/$search?=) |
| 42 | + |
| 43 | + |
| 44 | +```php |
| 45 | +$graphServiceClient = new GraphServiceClient($tokenRequestContext); |
| 46 | + |
| 47 | +$userId = "<USER-ID>"; |
| 48 | + |
| 49 | +try { |
| 50 | + $memberships = $graphServiceClient->users()->byUserId($userId)->transitiveMemberOf()->get()->wait(); |
| 51 | + if ($memberships && $memberships->getValue()) { |
| 52 | + foreach ($memberships->getValue() as $membership) { |
| 53 | + $obj = $graphServiceClient->directoryObjects()->byDirectoryObjectId($membership->getId())->get()->wait(); |
| 54 | + if ($obj && $obj->getOdataType() === '#microsoft.graph.group') { |
| 55 | + $group = $graphServiceClient->groups()->byGroupId($obj->getId())->get()->wait(); |
| 56 | + if ($group) { |
| 57 | + echo "Group ID: {$group->getId()}<br>"; |
| 58 | + echo "Group Types: " . json_encode($group->getGroupTypes()) . "<br>"; |
| 59 | + echo "Group Display Name: {$group->getDisplayName()}<br>"; |
| 60 | + echo "Group Mail: {$group->getMail()}<br><br>"; |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | +} catch (ApiException $ex) { |
| 66 | + echo "Error: " . $ex->getResponseStatusCode() . "\n"; |
| 67 | + echo "Error: " .$ex->getError()->getMessage();"; |
| 68 | +} |
| 69 | +``` |
0 commit comments