-
-
Save princeppy/d36ac4a64cea19989efe6df4efcba955 to your computer and use it in GitHub Desktop.
Get external users for a site collection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| string tenantAdminUrl = "https://yourtenant-admin.sharepoint.com/"; | |
| string userName = "[email protected]"; | |
| string userPassword = "password"; | |
| string siteCollectionUrl = "https://yourtenant.sharepoint.com/sites/teamsite/"; | |
| //Open the Tenant Administration Context with the Tenant Admin Url | |
| using (var tenantContext = new ClientContext(tenantAdminUrl)) | |
| { | |
| //Authenticate with a Tenant Administrator | |
| var passWord = new SecureString(); | |
| foreach (char c in userPassword.ToCharArray()) passWord.AppendChar(c); | |
| tenantContext.Credentials = new SharePointOnlineCredentials(userName, passWord); | |
| var o365Tenant = new Office365Tenant(tenantContext); | |
| //initalize varables to going through the paged results | |
| int position = 0; | |
| bool hasMore = true; | |
| while (hasMore) | |
| { | |
| //get external users 50 at a time (this is the limit and why we are paging) | |
| var externalUsers = o365Tenant.GetExternalUsersForSite(siteCollectionUrl, position, 50, String.Empty, SortOrder.Descending); | |
| tenantContext.Load(externalUsers, i => i.TotalUserCount); | |
| tenantContext.Load(externalUsers, i => i.ExternalUserCollection); | |
| tenantContext.ExecuteQuery(); | |
| //convert each external user to our own entity | |
| foreach (var extUser in externalUsers.ExternalUserCollection) | |
| { | |
| position++; | |
| Console.WriteLine(extUser.DisplayName + " " + extUser.UniqueId + " " + | |
| extUser.AcceptedAs + " " + extUser.InvitedAs + " " + extUser.InvitedBy + extUser.WhenCreated); | |
| } | |
| //determine if we have more pages to process | |
| hasMore = (externalUsers.TotalUserCount > position); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment