A project I was working involved retrieving all external / guest users that have been added to Office 365 and especially to Office 365 Groups. As it had to build in to an Office 365 application I choose to work with the Microsoft Graph.
First thing I did was retrieving all users with the standard API call:
API Call: https://graph.microsoft.com/v1.0/users
This is one of the results I got from the API call:
As you can see, one of my guest users I have added is my professional account. All these external users have #EXT# in the userPrincipalName. First I thought to do a "contains" or "search" query, but it appears that is not yet possible.
When I went to check my Azure Active Directory users, I saw an interesting property: User Type.
For all the external users in my environment this is set to Guest. The best news is, you can also query this property via the Microsoft Graph API.
If you want to retrieve all the external users inside your tenant, all you should do is user the following API call:
API Call: https://graph.microsoft.com/v1.0/users?$filter=userType eq 'Guest'
When I was writing this post, I found out that the beta endpoint gives you even more information about the user. This would have saved me some time today.