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

时间:2016-07-13 13:56:47

标签: javascript rest sharepoint office365 sharepoint-jsom

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

我发现没有任何完全有效的东西,我需要以下属性:

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

提前致谢

2 个答案:

答案 0 :(得分:0)

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

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

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

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

要调用Microsoft Graph 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

因为我们需要获得不同的用户'配置文件,我们需要使用仅限app的令牌(守护程序服务应用程序)。

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

答案 1 :(得分:0)

如果只需将所有用户导出到文件,请使用powershell。 你需要在下面的代码中更改两件事:客户端库和网站集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