kwizcom banner advertisement

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
}


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="salaudeen.rajack@crescent.com"

#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:
s#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"
$UserIDs=@("salaudeen.rajack@crescent.com","Charles.jonathan@crescent.com","mark.jones@crescent.com")

#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!"
}
Here is my another post for SharePoint On-premises on retrieving and updating People picker field values using PowerShell: PowerShell to Get or Set Person or Group (People Picker) Field Values in SharePoint
SharePoint Online: Read / Update Person or Group Field (People Picker) Values using PowerShell SharePoint Online: Read / Update Person or Group Field (People Picker) Values using PowerShell Reviewed by Salaudeen Rajack on October 18, 2017 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.