How can I retrieve all the user profiles in a site collection with JSOM/CSOM or REST API ?
I find nothing that completely works, I need the following properties :
- Username
- Mobile
- Profile picture
Thanks in advance
How can I retrieve all the user profiles in a site collection with JSOM/CSOM or REST API ?
I find nothing that completely works, I need the following properties :
Thanks in advance
We can use the Microsoft Graph to retrieve all the users via the request below:
GET:https://graph.microsoft.com/v1.0/users
You can get the username, mobile and E-mail via DisplayName, mobilePhone, mail property. And to get the profile picture, we need to send request like below for each user:
https://graph.microsoft.com/v1.0/users/{id | userPrincipalName}/photo/$value
And to call the Microsoft Graph REST API, we need to register the app and grant the suitable permission for the app.
The Scope for List User:
User.ReadBasic.All; User.Read.All; User.ReadWrite.All; Directory.Read.All; Directory.ReadWrite.All; Directory.AccessAsUser.All
The Scope for get photo:
User.Read; User.ReadBasic.All; User.Read.All; User.ReadWrite.All; User.Read
And since we need to get different users' profile, we need to use app-only token(Daemon service app).
Refer to here for calling Microsoft Graph in a service or daemon app
If you need just to export all users to a file, use powershell. you need to change 2 things in the code below: locations of client libraries and site collection urtl.
Add-Type -Path "c:\DLLS\Microsoft.SharePoint.Client.dll"
Add-Type -Path "c:\DLLS\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "c:\DLLS\Microsoft.SharePoint.Client.UserProfiles.dll"
#Mysite URL
$site = 'https://NAME.sharepoint.com/'
#Get the Client Context and Bind the Site Collection
$context = New-Object Microsoft.SharePoint.Client.ClientContext($site)
#Authenticate
$newCredentials = Get-Credential
$UserName = $newCredentials.UserName
$SecurePassword = $newCredentials.Password
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
$context.Credentials = $credentials
#Fetch the users in Site Collection
$users = $context.Web.SiteUsers
$context.Load($users)
$context.ExecuteQuery()
#Create an Object [People Manager] to retrieve profile information
$people = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($context)
$collection = @()
Write-Host "Found" $users.Count " users. Exporting..."
for($I = 1; $I -lt $users.Count; $I++ ){
try
{
$percCompl = [Math]::Floor(($I / $users.Count) * 100)
Write-Progress -Activity Updating -Status 'Progress->' -CurrentOperation "$percCompl% complete" -PercentComplete $percCompl;
$user = $users[$I]
$userprofile = $people.GetPropertiesFor($user.LoginName)
$context.Load($userprofile)
$context.ExecuteQuery()
$profileData = "" | Select "FirstName", "LastName", "UserName", "WorkEmail", "WorkPhone", "Department", "JobTitle", "Location", "SiteUrl"
if($userprofile -ne $null -and $userprofile.Email -ne $null)
{
$upp = $userprofile.UserProfileProperties
$profileData.FirstName = $upp.FirstName
$profileData.LastName = $upp.LastName
$profileData.UserName = $upp.UserName
$profileData.WorkEmail = $upp.WorkEmail
$profileData.WorkPhone = $upp.WorkPhone
$profileData.Department = $upp.'SPS-Department'
$profileData.JobTitle = $upp.'SPS-JobTitle'
$profileData.Location = $upp.'SPS-Location'
$profileData.SiteUrl = $site
$collection += $profileData
}
else{
$profileData.FirstName = $user.UserId
$profileData.LastName = $upp.Title
$profileData.WorkEmail = $user.Email
$profileData.SiteUrl = $site
$collection += $profileData
}
}
catch
{
Write-Host "UserError: " $user.LoginName ". Error detail:" $($_)
}
}
$collection | Export-Csv C:\SPO-Users.csv -NoTypeInformation -Encoding UTF8
Write-Host "Done!" -ForegroundColor Green