如何使用JSOM/CSOM或REST API檢索網站集中的所有用戶配置文件?如何獲取網站集中的所有用戶配置文件? (SharePoint Online O365)
我覺得沒有什麼是完全的作品,我需要以下屬性:
- 用戶名
- 移動
- 電子郵件
- 檔案圖片
在此先感謝
如何使用JSOM/CSOM或REST API檢索網站集中的所有用戶配置文件?如何獲取網站集中的所有用戶配置文件? (SharePoint Online O365)
我覺得沒有什麼是完全的作品,我需要以下屬性:
在此先感謝
我們可以使用M icrosoft圖通過如下申請檢索所有用戶:
GET:https://graph.microsoft.com/v1.0/users
您可以通過顯示名稱,mobilePhone,郵件屬性來獲取用戶名,手機和電子郵件。並獲得了個人資料圖片,我們需要發送的請求像下面爲每個用戶:
https://graph.microsoft.com/v1.0/users/{id | userPrincipalName}/photo/$value
,並呼籲微軟圖形REST API,我們需要註冊應用程序,並授予該應用的適當許可。
爲List User範圍:
User.ReadBasic.All; User.Read.All; User.ReadWrite.All; Directory.Read.All; Directory.ReadWrite.All; Directory.AccessAsUser.All
爲get photo範圍:
User.Read; User.ReadBasic.All; User.Read.All; User.ReadWrite.All; User.Read
而且因爲我們需要讓不同的用戶的個人資料,我們需要使用的應用程序,只是象徵性地(守護進程服務應用程序)。
參考here調用微軟圖形的服務或守護程序
如果你只需要對所有用戶導出到文件中,使用PowerShell。 您需要在下面的代碼中更改2件事:客戶端庫和網站集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