I tried to get users who have manager in my organization.
https://graph.microsoft.com/v1.0/users?$expand=manager&$select=id,userPrincipalName
But I get all users, those with managers and those without managers.
In the response I need only users who have manager. How can I filter them ?
When I ran the same query as you, I too got same results with users having manager and without manager like below:
GET https://graph.microsoft.com/v1.0/users?$expand=manager&$select=id,userPrincipalName
Response:
Note that, there is no direct graph query to retrieve only users having manager. Instead, you can use any coding language to customize the response of above query.
In my case, I used below Python code and got only users having manager in the response like below:
import requests
url = "https://graph.microsoft.com/v1.0/users?$expand=manager&$select=id,userPrincipalName"
headers = {
"Authorization": "Bearer <access_token>"
}
response = requests.get(url, headers=headers)
data = response.json()
users_with_manager = [user for user in data["value"] if user.get("manager")]
for user in users_with_manager:
print("User ID:", user["id"])
print("User Principal Name:", user["userPrincipalName"])
Response: