Wednesday, January 25, 2017

SharePoint Online: Update User Profile Properties using PowerShell

Requirement: Update User Profile Property in SharePoint Online using PowerShell

How to Update User Profiles in SharePoint Online?
To update SharePoint Online user profile properties, follow these steps:

  • Login to SharePoint Online Admin Center >> Click on "User Profiles" link from the left navigation
  • In User Profiles, Click on "Manage User Profiles" under People tab. Use Search to find the user profile of the user to update >> Click on "Edit My Profile" link from the context menu drop down of the user result.
  • Update any allowed user profile property and click on "Save and Close" button.
    powershell sharepoint online user profile properties
SharePoint Online: Update user profile using PowerShell

Here is the PowerShell for SharePoint Online to set User profile properties.
#Load SharePoint CSOM Assemblies
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"

Function Update-UserProfileProperty()
{
    param
    (
        [Parameter(Mandatory=$true)] [string] $AdminCenterURL,
        [Parameter(Mandatory=$true)] [string] $UserAccount,
        [Parameter(Mandatory=$true)] [string] $PropertyName,
        [Parameter(Mandatory=$true)] [string] $PropertyValue
    )    
    Try {
        #Setup Credentials to connect
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($AdminCenterURL)
        $Ctx.Credentials = $Credentials
        
        #Get the User
        $User = $Ctx.web.EnsureUser($UserAccount)
        $Ctx.Load($User)
        $Ctx.ExecuteQuery()

        #Get User Profile
        $PeopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($Ctx)
        
        #update User Profile Property
        $PeopleManager.SetSingleValueProfileProperty($User.LoginName, $PropertyName, $PropertyValue)
        $Ctx.ExecuteQuery()

        Write-host "User Profile Property has been Updated!" -f Green
    }
    Catch {
        write-host -f Red "Error Updating User Profile Property!" $_.Exception.Message
    }
}
#Define Parameter values
$AdminCenterURL="https://crescent-admin.sharepoint.com"
$UserAccount="Zahia@Crescent.com"
$PropertyName="Department"
$PropertyValue="Operations - IT"

#Call the function
Update-UserProfileProperty -AdminCenterURL $AdminCenterURL -UserAccount $UserAccount -PropertyName $PropertyName -PropertyValue $PropertyValue

PowerShell for SharePoint Online User profile properties Update:
There are fields with Multi-values in People profiles. E.g. Skills! Lets update Multi-valued fields in SharePoint Online user profiles.
#Load SharePoint CSOM Assemblies
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"

Function Update-MultiUserProfileProperty()
{
    param
    (
        [Parameter(Mandatory=$true)] [string] $AdminCenterURL,
        [Parameter(Mandatory=$true)] [string] $UserAccount,
        [Parameter(Mandatory=$true)] [string] $PropertyName,
        [Parameter(Mandatory=$true)] [System.Collections.Generic.List``1[System.string]] $PropertyValues
    )    
    Try {
        #Setup Credentials to connect
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($AdminCenterURL)
        $Ctx.Credentials = $Credentials
        
        #Get the User
        $User = $Ctx.web.EnsureUser($UserAccount)
        $Ctx.Load($User)
        $Ctx.ExecuteQuery()

        #Get User Profile
        $PeopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($Ctx)
        $PropertyValues
        #update User Profile Property
        $PeopleManager.SetMultiValuedProfileProperty($User.LoginName, $PropertyName, $PropertyValues)
        $Ctx.ExecuteQuery()

        Write-host "User Profile Property has been Updated!" -f Green
    }
    Catch {
        write-host -f Red "Error Updating User Profile Property!" $_.Exception.Message
    }
}
#Define Parameter values
$AdminCenterURL="https://crescent-admin.sharepoint.com"
$UserAccount="SPAdmin@crescent.com"
$PropertyName="SPS-Skills"
$PropertyValues = New-Object "System.Collections.Generic.List``1[System.string]"
$PropertyValues.Add("SharePoint")
$PropertyValues.Add("PowerShell")
$PropertyValues.Add("C#")

#Call the function
Update-MultiUserProfileProperty -AdminCenterURL $AdminCenterURL -UserAccount $UserAccount -PropertyName $PropertyName -PropertyValues $PropertyValues



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!


You might also like:

Related Posts Plugin for WordPress, Blogger...