SharePoint Online: Read / Update Person or Group Field (People Picker) Values using PowerShell

Requirement: Get or Set "Person or Group" Field Values in SharePoint Online using PowerShell.

Person or Group field provides searchable list of people and groups when they add or edit an item. It acts like a auto complete field when you type the first 4 character of the user names. Here is my collection PowerShell scripts to retrieve and update person or group field values.
sharepoint online get set people picker field values using powershell

SharePoint Online - Get People Picker Field Value using PowerShell:
#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"

#Set parameter values
$SiteURL="https://crescent.sharepoint.com/sites/pmo"
$ListName="Projects"
$FieldName= "ProjectManager" #Internal Name
$ListItemID="5"

#Get 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($SiteURL)
$Ctx.Credentials = $Credentials

#Get the List & Item to update
$List = $Ctx.Web.lists.GetByTitle($ListName) 
$ListItem = $List.GetItemById($ListItemID)
$Ctx.Load($ListItem)
$Ctx.ExecuteQuery()

#Get the People picker Column of the list item
$FieldValue = [Microsoft.SharePoint.Client.FieldUserValue]$ListItem[$FieldName]

#Get the Display Name and Email Field
Write-host $FieldValue.LookupValue
Write-host $FieldValue.Email


PowerShell to Retrieve Multi-value Person or Group Field Values in SharePoint Online:
#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"

#Set parameter values
$SiteURL="https://crescent.sharepoint.com/sites/pmo"
$ListName="Projects"
$FieldName= "ProjectMembers" #Internal Name
$ListItemID="25"

#Get 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($SiteURL)
$Ctx.Credentials = $Credentials

#Get the List & Item to update
$List = $Ctx.Web.lists.GetByTitle($ListName) 
$ListItem = $List.GetItemById($ListItemID)
$Ctx.Load($ListItem)
$Ctx.ExecuteQuery()

#Get the Column Values
$FieldValues = [Microsoft.SharePoint.Client.FieldUserValue[]]$ListItem[$FieldName]

Write-host -f Yellow "Found $($FieldValues.Count) Users in the Given Field Value!"

#Get Each User from the collection
ForEach($FieldValue in $FieldValues)
{
    #Get the Display Name and Email Field
    Write-Host -f Green $FieldValue.LookupValue : $FieldValue.Email
}
You can get SPUser object from Person or Group Column value as:
    
#Get the "Created By" column value of the Item
$CreatedBy = $ListItem.FieldValues["Author"]

#Get the User Object
$User = $Ctx.Web.GetUserById($CreatedBy.LookupId)
$Ctx.Load($User)
$Ctx.ExecuteQuery()

$User | Select Email, Title, LoginName

Update Person or Group Field Value using PowerShell:
#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"

#Set parameter values
$SiteURL="https://crescent.sharepoint.com/sites/pmo"
$ListName="Projects"
$FieldName= "ProjectManager" #Internal Name
$ListItemID="25"
$UserID="[email protected]"

#Get 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($SiteURL)
$Ctx.Credentials = $Credentials

#Get the List & Item to update
$List = $Ctx.Web.lists.GetByTitle($ListName) 
$ListItem = $List.GetItemById($ListItemID)
$Ctx.Load($ListItem)
$Ctx.ExecuteQuery()

#Get the User to Update
$SPOUser = $Ctx.Web.EnsureUser($UserID)
$Ctx.Load($SPOUser)
$Ctx.ExecuteQuery()

If($SPOUser -ne $Null)
{
    #Update the People picker column
    $ListItem[$FieldName] = $SPOUser
    $ListItem.Update()
    $Ctx.ExecuteQuery()
    Write-Host -f Green "List Item has been updated!"
}

PowerShell to Set Multi-valued People Picker Field Values in SharePoint Online:
#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"

#Set parameter values
$SiteURL="https://crescent.sharepoint.com/sites/pmo"
$ListName="Projects"
$FieldName= "ProjectMembers" #Internal Name
$ListItemID="25"
[email protected]("[email protected]","[email protected]","[email protected]")

#Get 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($SiteURL)
$Ctx.Credentials = $Credentials

#Get the List & Item to update
$List = $Ctx.Web.lists.GetByTitle($ListName) 
$ListItem = $List.GetItemById($ListItemID)
$Ctx.Load($ListItem)
$Ctx.ExecuteQuery()

#Resolve each user ID and get the User Objects
$UserValueColl = @()
ForEach($UserID in $UserIDs)
{
    #Get the User to Update
    $SPOUser = $Ctx.Web.EnsureUser($UserID)
    $Ctx.Load($SPOUser)
    $Ctx.ExecuteQuery()

    If($SPOUser -ne $Null)
    {
        $SPOUserValue = New-Object Microsoft.SharePoint.Client.FieldUserValue
        $SPOUserValue.LookupId = $SPOUser.id
        $UserValueColl += $SPOUserValue
    }
}

IF($UserValueColl.length -gt 0)
{
    $UserValueCollCollection = [Microsoft.SharePoint.Client.FieldUserValue[]]$UserValueColl
    #Update the Multi-People picker column
    $ListItem[$FieldName] = $UserValueCollCollection
    $ListItem.Update()
    $Ctx.ExecuteQuery()
    Write-Host -f Green "List Item has been updated!"
}

Update Person or Group Field Values using PnP PowerShell
Person or Group fields can be updated either with the Email ID of the user or the "id" of user or group in Site User Info List. Use this format to set people picker field values: @{"PersonField" = "[email protected]","1"}, Here "1" is the ID of the person. 
#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/pmo"
$ListName = "Projects"
$FieldName = "Project_x0020_Manager"
$ItemID = 2
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin
 
#Update person or group field value using PnP PowerShell
Set-PnPListItem -List $ListName -Identity $ItemID -Values @{$FieldName = "[email protected]"}
Similarly, to update multi-person or group column with User IDs, use:
Set-PnPListItem -List $ListName -Identity $ItemID -Values @{$FieldName = 13, 23}
Here the "13" and "23" are Ids of users in SharePoint Online site collection.

My another post to retrieve and update People picker field values for SharePoint On-premises using PowerShell: PowerShell to Get or Set Person or Group (People Picker) Field Values in SharePoint

1 comment:

Please Login and comment to get your questions answered!

Powered by Blogger.