Tuesday, April 14, 2015

An approach to list all external users in your SharePoint Online environment

image

Kirk Liemohn recently asked a question on the Office 365 Yammer network if there was an API to list all the external users on his tenant. Being the search guy I am, I suggested using the search API to do this.
If I want to list all external users on the Puzzlepart tenant I can fire off the following REST query:

https://tenant.sharepoint.com/_api/search/query?querytext=%27-AccountName:puzzlepart.com%27&selectproperties=%27AccountName%2cPreferredName%27&rowlimit=500&properties=%27SourceName:Local%20People%20Results%27

The above query will get the top 500 external users, which happens to be accounts without puzzlepart.com in the AccountName managed property. I’m also just retrieving AccountName and preferredname to limit data transfer, but include any property you need.

[Update 2021-03-01]

You can also use a query like AccountName:"ext tenant.onmicrosoft.com" as all external users have an account name similar to i:0#.f|membership|some.one_domain.com#ext#@tenant.onmicrosoft.com. Special characters are tokenized away, which is why spaces work fine.