如何获取网站集中的所有用户配置文件?(SharePoint Online O365)

How can I get all the user profiles in a site collection? (SharePoint Online O365)

本文关键字:配置文件 用户 SharePoint O365 Online 何获取 获取 网站 集中      更新时间:2023-09-26

如何使用 JSOM/CSOM 或 REST API 检索网站集中的所有用户配置文件?

我找不到完全有效的内容,我需要以下属性:

  • 用户名
  • 移动
  • 电子邮件
  • 个人资料图片

提前致谢

我们可以使用Microsoft图通过以下请求检索所有用户:

GET:https://graph.microsoft.com/v1.0/users

您可以通过显示名称移动电话邮件属性获取用户名,手机和电子邮件。为了获得个人资料图片,我们需要为每个用户发送如下请求:

https://graph.microsoft.com/v1.0/users/{id | userPrincipalName}/photo/$value

要调用Microsoft图形 REST API,我们需要注册应用并为应用授予适当的权限。

列表用户的范围:

User.ReadBasic.All; User.Read.All; User.ReadWrite.All; Directory.Read.All; Directory.ReadWrite.All; Directory.AccessAsUser.All

获取照片的范围:

User.Read; User.ReadBasic.All; User.Read.All; User.ReadWrite.All; User.Read

由于我们需要获取不同用户的配置文件,因此我们需要使用仅限应用程序的令牌(守护程序服务应用程序)。

有关在服务或守护程序中调用 Microsoft Graph 的信息,请参阅此处

如果只需要将所有用户导出到文件,请使用 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