从 SharePoint 在线租户获取所有用户并通过 Powershell 设置用户配置文件 属性
Getting ALL users from SharePoint online tenant and set userprofile property via Powershell
更新:
Vadim 在下面有一个很好的答案,但我就是这样做的:
namespace UserProfile.Manipulation.CSOM.Console
{
class Program
{
static void Main(string[] args)
{
searchAllUsers();
}
private static void searchAllUsers()
{
string siteCollectionUrl = "https://tenant.sharepoint.com/sites/intranet";
string tenantAdminLoginName = "adminlogin";
string tenantAdminPassword = "Password";
using (ClientContext clientContext = new ClientContext(siteCollectionUrl))
{
SecureString passWord = new SecureString();
foreach (char c in tenantAdminPassword.ToCharArray()) passWord.AppendChar(c);
clientContext.Credentials = new SharePointOnlineCredentials(tenantAdminLoginName, passWord);
KeywordQuery keywordQuery = new KeywordQuery(clientContext);
keywordQuery.QueryText = "*";
keywordQuery.SourceId = new Guid("B09A7990-05EA-4AF9-81EF-EDFAB16C4E31");
keywordQuery.TrimDuplicates = false;
keywordQuery.RowLimit = 500;
//startrow changed 3 times since 500 limitations.
keywordQuery.StartRow = 1001;
SearchExecutor searchExecutor = new SearchExecutor(clientContext);
ClientResult<ResultTableCollection> results = searchExecutor.ExecuteQuery(keywordQuery);
clientContext.ExecuteQuery();
foreach (var resultRow in results.Value[0].ResultRows)
{
SetSingleValueProfileProperty(resultRow["AccountName"].ToString());
}
}
}
private static void SetSingleValueProfileProperty(string accountName)
{
string tenantAdministrationUrl = "https://tentnatname-admin.sharepoint.com";
string tenantAdminLoginName = "adminlogin";
string tenantAdminPassword = "password";
string UserAccountName = accountName;
using (ClientContext clientContext = new ClientContext(tenantAdministrationUrl))
{
SecureString passWord = new SecureString();
foreach (char c in tenantAdminPassword.ToCharArray()) passWord.AppendChar(c);
clientContext.Credentials = new SharePointOnlineCredentials(tenantAdminLoginName, passWord);
PeopleManager peopleManager = new PeopleManager(clientContext);
peopleManager.SetSingleValueProfileProperty(UserAccountName, "SPS-PicturePlaceholderState", "1");
clientContext.ExecuteQuery();
}
}
}
}
我需要在租户内的所有用户配置文件上设置 属性。
似乎无法通过 CSOM 和 powershell 从 "UPA" 获取所有用户配置文件!?
可以(理论上)遍历所有网站集并将所有用户添加到某个数组中,然后仅 select 从该数组中唯一(以删除重复项):
$sites = Get-SPOSite | select *
foreach ($site in $sites) {
Get-SPOUser -Site $site
...等等
然后遍历该数组并使用 csom:
$pMAn = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($context)
$userProfile = $pMan.GetPropertiesFor($user.LoginName)
$ctx.Load($userProfile)
$ctx.ExecuteQuery()
$pMan.SetSingleVlueProfileProperty($userProfile.AccountName, "property", $value)
$ctx.ExecuteQuery()
谁能想出更聪明的解决方案?
如您所述,没有直接的方法可以使用 SharePoint Online CSOM API 获取 SharePoint 租户中的所有用户配置文件。但是您可以考虑以下方法:
- 利用Azure Active Directory Module for Windows PowerShell来
检索租户 (
Get-MsolUser
cmdlet) 中的所有用户
- 迭代用户并利用 SharePoint 用户配置文件 CSOM API
检索用户个人资料
例子
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.UserProfiles.dll"
Function Get-SPOContext([string]$Url,[string]$UserName,[string]$Password)
{
$context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
$context.Credentials = Get-SPOCredentials -UserName $UserName -Password $Password
return $context
}
Function Get-SPOCredentials([string]$UserName,[string]$Password)
{
$SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
return New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
}
Function Print-UserProfileInfo([Microsoft.SharePoint.Client.UserProfiles.PeopleManager]$PeopleManager,[string]$AccountName){
$ctx = $PeopleManager.Context
$accountName = "i:0#.f|membership|" + $AccountName #claim format
$userProfile = $PeopleManager.GetPropertiesFor($AccountName)
$ctx.Load($userProfile)
$ctx.ExecuteQuery()
Write-Host $userProfile.PersonalUrl
}
$tenantUrl = "https://contoso.sharepoint.com/"
$userName = "jdoe@contoso.onmicrosoft.com"
$password = "password"
$secPassword = ConvertTo-SecureString $password -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($userName, $secPassword)
Connect-MsolService -Credential $cred
$allUsers = Get-MsolUser
$Context = Get-SPOContext -Url $tenantUrl -UserName $userName -Password $password
$peopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($Context)
$allUsers | % { Print-UserProfileInfo -PeopleManager $peopleManager -AccountName $_.UserPrincipalName }
$Context.Dispose()
更新:
Vadim 在下面有一个很好的答案,但我就是这样做的:
namespace UserProfile.Manipulation.CSOM.Console
{
class Program
{
static void Main(string[] args)
{
searchAllUsers();
}
private static void searchAllUsers()
{
string siteCollectionUrl = "https://tenant.sharepoint.com/sites/intranet";
string tenantAdminLoginName = "adminlogin";
string tenantAdminPassword = "Password";
using (ClientContext clientContext = new ClientContext(siteCollectionUrl))
{
SecureString passWord = new SecureString();
foreach (char c in tenantAdminPassword.ToCharArray()) passWord.AppendChar(c);
clientContext.Credentials = new SharePointOnlineCredentials(tenantAdminLoginName, passWord);
KeywordQuery keywordQuery = new KeywordQuery(clientContext);
keywordQuery.QueryText = "*";
keywordQuery.SourceId = new Guid("B09A7990-05EA-4AF9-81EF-EDFAB16C4E31");
keywordQuery.TrimDuplicates = false;
keywordQuery.RowLimit = 500;
//startrow changed 3 times since 500 limitations.
keywordQuery.StartRow = 1001;
SearchExecutor searchExecutor = new SearchExecutor(clientContext);
ClientResult<ResultTableCollection> results = searchExecutor.ExecuteQuery(keywordQuery);
clientContext.ExecuteQuery();
foreach (var resultRow in results.Value[0].ResultRows)
{
SetSingleValueProfileProperty(resultRow["AccountName"].ToString());
}
}
}
private static void SetSingleValueProfileProperty(string accountName)
{
string tenantAdministrationUrl = "https://tentnatname-admin.sharepoint.com";
string tenantAdminLoginName = "adminlogin";
string tenantAdminPassword = "password";
string UserAccountName = accountName;
using (ClientContext clientContext = new ClientContext(tenantAdministrationUrl))
{
SecureString passWord = new SecureString();
foreach (char c in tenantAdminPassword.ToCharArray()) passWord.AppendChar(c);
clientContext.Credentials = new SharePointOnlineCredentials(tenantAdminLoginName, passWord);
PeopleManager peopleManager = new PeopleManager(clientContext);
peopleManager.SetSingleValueProfileProperty(UserAccountName, "SPS-PicturePlaceholderState", "1");
clientContext.ExecuteQuery();
}
}
}
}
我需要在租户内的所有用户配置文件上设置 属性。
似乎无法通过 CSOM 和 powershell 从 "UPA" 获取所有用户配置文件!?
可以(理论上)遍历所有网站集并将所有用户添加到某个数组中,然后仅 select 从该数组中唯一(以删除重复项):
$sites = Get-SPOSite | select *
foreach ($site in $sites) {
Get-SPOUser -Site $site
...等等
然后遍历该数组并使用 csom:
$pMAn = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($context)
$userProfile = $pMan.GetPropertiesFor($user.LoginName)
$ctx.Load($userProfile)
$ctx.ExecuteQuery()
$pMan.SetSingleVlueProfileProperty($userProfile.AccountName, "property", $value)
$ctx.ExecuteQuery()
谁能想出更聪明的解决方案?
如您所述,没有直接的方法可以使用 SharePoint Online CSOM API 获取 SharePoint 租户中的所有用户配置文件。但是您可以考虑以下方法:
- 利用Azure Active Directory Module for Windows PowerShell来
检索租户 (
Get-MsolUser
cmdlet) 中的所有用户
- 迭代用户并利用 SharePoint 用户配置文件 CSOM API 检索用户个人资料
例子
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.UserProfiles.dll"
Function Get-SPOContext([string]$Url,[string]$UserName,[string]$Password)
{
$context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
$context.Credentials = Get-SPOCredentials -UserName $UserName -Password $Password
return $context
}
Function Get-SPOCredentials([string]$UserName,[string]$Password)
{
$SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
return New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
}
Function Print-UserProfileInfo([Microsoft.SharePoint.Client.UserProfiles.PeopleManager]$PeopleManager,[string]$AccountName){
$ctx = $PeopleManager.Context
$accountName = "i:0#.f|membership|" + $AccountName #claim format
$userProfile = $PeopleManager.GetPropertiesFor($AccountName)
$ctx.Load($userProfile)
$ctx.ExecuteQuery()
Write-Host $userProfile.PersonalUrl
}
$tenantUrl = "https://contoso.sharepoint.com/"
$userName = "jdoe@contoso.onmicrosoft.com"
$password = "password"
$secPassword = ConvertTo-SecureString $password -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($userName, $secPassword)
Connect-MsolService -Credential $cred
$allUsers = Get-MsolUser
$Context = Get-SPOContext -Url $tenantUrl -UserName $userName -Password $password
$peopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($Context)
$allUsers | % { Print-UserProfileInfo -PeopleManager $peopleManager -AccountName $_.UserPrincipalName }
$Context.Dispose()