We want to update the customValue user profile property using powershell sharepoint online. The code updates only for one account '[email protected]
' who is the globalk admin of the site. But none of other users, any suggestions
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
$username = "[email protected]"
$Adminurl = "https://Test-admin.sharepoint.com"
$siteColUrl = "https://Test.sharepoint.com"
try
{
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.UserProfiles.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.UserProfiles.dll"
#Bind to site collection
$password = Read-Host -Prompt "Enter password" -AsSecureString
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($siteColUrl)
#$ContextAdmin = New-Object Microsoft.SharePoint.Client.ClientContext($UserProfurl)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username,$Password)
$Context.Credentials = $Creds
Write-Host "Successfully connected.." -ForegroundColor Green
#Retrieve lists
$web = $Context.Web
$Users = $Context.Web.SiteUsers
$items = $list.GetItems($query)
$Context.Load($Users)
$Context.Load($web)
$Context.ExecuteQuery()
$CustomValue =10
Write-Host $list.Title
$PeopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($Context)
Write-Host $items.Count
Foreach ($User in $Users)
{
$UserNameValue = $User.LoginName
$UserProfile = $PeopleManager.GetPropertiesFor($UserNameValue)
$context.Load($UserProfile)
$Context.ExecuteQuery()
$PeopleManager.SetSingleValueProfileProperty($UserProfile.AccountName,"CustomValue", $CustomValue)
$Context.ExecuteQuery()
}
}