Update User Profile Properties In SharePoint Online Using CSOM

In dynamic business need, we may required to frequently update user profile properties , such as email, manager and other business and personal information. In this article, I am going write Powershell script to set or update user profile properties programmatically using CSOM.

The Microsoft SharePoint Online SDK assembly Microsoft.SharePoint.Client.UserProfiles.dll includes the class PeopleManager and this class includes the following methods:

  • SetSingleValueProfileProperty – This method used to set a single valued property for any user. 
  • SetMultiValuedProfileProperty – This method used to update multi valued property for any user.
  • SetMyProfilePicture – This method used update current user profile picture. Users can only upload a picture to their own profile.

Update Single Value User Properties

Use the below Powershell script to update single valued user property.

#Add required references to SharePoint client assembly to use CSOM 
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")  
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.UserProfiles")

$UserName = "[email protected]"
$SecPwd = $(ConvertTo-SecureString 'adminPassword' -asplaintext -force) 
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl) 
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,$SecPwd) 
$ctx.credentials = $credentials 
 
$targetUser = "i:0#.f|membership|[email protected]" 
#Initialize PeopleManager object
$peopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($ctx) 
# Update user's department info
$peopleManager.SetSingleValueProfileProperty($targetUser, "Department", "Sales Team") 
$ctx.ExecuteQuery()

Update Multi Valued User Properties

Use the below Powershell script to set multi valued SharePoint user properties.

$siteUrl = "https://spotenant-admin.sharepoint.com" 

$UserName = "[email protected]"
$SecPwd = $(ConvertTo-SecureString 'adminPassword' -asplaintext -force) 
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl) 
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,$SecPwd) 
$ctx.credentials = $credentials 
 
$targetUser = "i:0#.f|membership|[email protected]" 
#Initialize PeopleManager object
$peopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($ctx) 
#Initialize string list to values
$propertyValues =New-Object "System.Collections.Generic.List``1[System.string]"   
# My responsibilities   
$propertyValues.Add("SharePoint")   
$propertyValues.Add("Office 365")           
$propertyValues.Add("Powershell") 
$propertyValues.Add("CSOM") 
# Update profile property
$peopleManager.SetMultiValuedProfileProperty($targetUser, "SPS-Skills", $propertyValues) 
$ctx.ExecuteQuery()

Advertisement

Leave a Comment