top of page
Writer's pictureNakkeeran Natarajan

Update User Profile Properties On SharePoint Online Using CSOM PowerShell

In this article, you will learn how to update SharePoint user profile properties programmatically in different ways using CSOM with PowerShell on SharePoint online - Office 365 sites. The update property option is available only on Office 365 sites as of now and it is not available on on-premise versions.

Steps Involved:

The following prerequisites need to be executed before going for any operations using CSOM PowerShell on SharePoint sites.

  • Add the references using the Add-Type command with necessary reference paths. The necessary references are Microsoft.SharePoint.Client.dll, Microsoft.SharePoint.Client.Runtime.dll and UserProfile dll. The references should be from 16 folder, since these are latest updates from Microsoft.

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.Runtime.dll"


Add-Type - Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\

Microsoft.SharePoint.Client.UserProfiles.dll"


  • Initialize client context object with the site URL.

$siteURL = "" $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)  

  • If you are trying to access SharePoint Online site, then you need to setup the site credentials with credentials parameter and get it set to the client context. 

# Setting Credentials for o365 - Start

$userId = ""

$pwd = Read-Host -Prompt "Enter password" -AsSecureString  

$creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)  

$ctx.credentials = $creds  

# Setting Credentials for o365 - End

  • There are multiple ways of updating the properties of SharePoint user profile. There will be properties with just a value or line of text and properties with collection of values. Two different methods are used to update those properties. Following tells us with examples. 

  1. Single Value properties - WorkPhone, Mobile, FirstName, LastName, etc. 

  2. Multi Valued properties - Ask Me About, Skills, Interests, etc.

Update Single Value Properties:

  • Initialize the people manager object with the context to get the user profile data.

  • Using SetSingleValueProfileProperty method set the property for any target user.

  • Load the object and execute query to get the data.

# Update User Profile property (office 365)

function SetSingleValueProperty()

{  

try

{          

$peopleManager = New- Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($ctx)                          

$peopleManager.SetSingleValueProfileProperty("i:0#.f|membership|abc@abc.onmicrosoft.com",

  "AboutMe", "Your description")          

$ctx.Load($peopleManager)          

$ctx.ExecuteQuery()          

Write-Host "Property updated"     

}  

catch

{          

Write-Host "$($_.Exception.Message)" -foregroundcolor Red      

}  }  


You can update as many properties as possible. 


$peopleManager = New- Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($ctx)                  

$peopleManager.SetSingleValueProfileProperty("i:0#.f|membership|abc@abc.onmicrosoft.com",

  "AboutMe", "Your description")  

$peopleManager.SetSingleValueProfileProperty("i:0#.f|membership|abc@abc.onmicrosoft.com",

  "SPS-Location", "Bangalore")  

$ctx.Load($peopleManager)  

$ctx.ExecuteQuery()  

Write-Host "Properties updated"


Update Multi Valued Properties:

In the above section, we have seen how we can set the single value profile properties for target user. Now we will see how we can set user profile multi valued properties for target user.

  • Initialize the people manager object with the context to get the user profile data.

  • Then initialize the list collection object and set the values in the collection variable.

  • Using SetMultiValuedProfileProperty method, set the multi valued property for any target user. (Here updated ask me about - Responsibility field).

  • Load the object and execute query to get the data.

# Update User Profile Multi Valued properties (office 365)

function SetMultiValuedProperty()

{  

try

{          

$peopleManager = New- Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($ctx)                          

$propertyValues =New-Object "System.Collections.Generic.List``1[System.string]"         

# My responsibilities         

$propertyValues.Add("SharePoint")          

$propertyValues.Add("Project1")                  

$propertyValues.Add("Project2")                  

# update value         

$peopleManager.SetMultiValuedProfileProperty("i:0#.f|membership|abc@abc.onmicrosoft.com", 

"SPS-Responsibility", $propertyValues)          

$ctx.Load($peopleManager)          

$ctx.ExecuteQuery()      

}  

catch

{          

Write-Host "$($_.Exception.Message)" -foregroundcolor Red      

}  }  


The above operation will override (set) the existing property values. If you want to update the existing property values, you need to retrieve the existing value using get operation and then set the properties along with new values.

Note: The above set of operations needs to be called to get it executed.SetSingleValueProperty

 

# Update User Profile properties (office 365)  

SetMultiValuedProperty 

# Update User Profile Multi Valued properties (office 365)  

0 comments

Comments


bottom of page