SharePoint Online: Add Person or Group (People Picker) Column to List using PowerShell
Requirement: Create a Person or Group Column in SharePoint Online List.
How to Add a Person or Group Column to List in SharePoint Online?
The “Person or Group” column in SharePoint Online is used to get user or group value. This field can be configured to accept any value from the authentication provider, such as the Active directory, or can be restricted to fetch individuals from a particular SharePoint group. Users can be located either by their name or e-mail address. It also auto-fills the appropriate matching names from the available values as you type. Follow these steps to add the people picker column to the list.
- Browse to your SharePoint Online site and navigate to the target list where you want to add the Person or Group column.
- Under the List tab, click on the “Create Column” button in the ribbon.
- Provide the Name to your new column, specify the type as “Person or Group”
- Scroll down, fill in other optional values such as required column, Unique, Allow multiple values, etc., and click “OK” to create a Person or Group field in the SharePoint Online list.
PowerShell to Create Person or Group Column to List in SharePoint Online:
Here is how to add a person or group field in the SharePoint list programmatically 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"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll"
#Custom function to add column to list
Function Add-PersonOrGroupColumnToList()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $ListName,
[Parameter(Mandatory=$true)] [string] $Name,
[Parameter(Mandatory=$true)] [string] $DisplayName,
[Parameter(Mandatory=$false)] [string] $Description=[string]::Empty,
[Parameter(Mandatory=$false)] [string] $IsRequired = "FALSE",
[Parameter(Mandatory=$false)] [string] $EnforceUniqueValues = "FALSE",
[Parameter(Mandatory=$false)] [string] $SelectionMode="PeopleOnly"
)
#Generate new GUID for Field ID
$FieldID = New-Guid
Try {
$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
$List = $Ctx.Web.Lists.GetByTitle($ListName)
$Ctx.Load($List)
$Ctx.ExecuteQuery()
#Check if the column exists in list already
$Fields = $List.Fields
$Ctx.Load($Fields)
$Ctx.executeQuery()
$NewField = $Fields | where { ($_.Internalname -eq $Name) -or ($_.Title -eq $DisplayName) }
if($NewField -ne $NULL)
{
Write-host "Column $Name already exists in the List!" -f Yellow
}
else
{
#Create Column in the list
$FieldSchema = "<Field Type='User' ID='{$FieldID}' DisplayName='$DisplayName' Name='$Name' Description='$Description' Required='$IsRequired' EnforceUniqueValues='$EnforceUniqueValues' ShowField='ImnName' List='UserInfo' UserSelectionMode='$SelectionMode' />"
$NewField = $List.Fields.AddFieldAsXml($FieldSchema,$True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
$Ctx.ExecuteQuery()
Write-host "New Column Added to the List Successfully!" -ForegroundColor Green
}
}
Catch {
write-host -f Red "Error Adding Column to List!" $_.Exception.Message
}
}
#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$Name="ProjectMembers"
$DisplayName="Project Members"
$Description="Enter the Project Team Members"
$SelectionMode="PeopleOnly" #Or PeopleAndGroups
#Call the function to add column to list
Add-PersonOrGroupColumnToList -SiteURL $SiteURL -ListName $ListName -Name $Name -DisplayName $DisplayName -Description $Description -SelectionMode $SelectionMode
How to bulk add multiple columns to a List from a CSV file?
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/london"
$ListName = "Desk Allocation"
$CSVFilePath = "C:\Temp\columns.csv"
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $SiteUrl -Interactive
#Get Data from CSV
$ListColumns = Import-Csv $CSVFilePath
ForEach($Column in $ListColumns)
{
Add-PnPField -List $ListName -Type User -InternalName $Column.Name -DisplayName $Column.Name -AddToDefaultView
Write-Host "Added Column '$($Column.Name)' to the List!"
}
}
Catch {
Write-Host "Error:"$_.Exception.Message
}
How to Add Multiple value People Picker Field?
To Add People Picker fields with “Allow Multiple Selections”, Use this Field schema:
$FieldSchema = "<Field Type='UserMulti' ID='{$FieldID}' DisplayName='$DisplayName' Name='$Name' Description='$Description' Required='$IsRequired' EnforceUniqueValues='$EnforceUniqueValues' ShowField='ImnName' List='UserInfo' UserSelectionMode='$SelectionMode' Mult='TRUE' />"
PnP PowerShell to Add Person or Group Column to SharePoint Online List
#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
$ListName= "Projects"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
#Define XML Schema for User Field
$FieldXML= "<Field Type='User' Name='ProjectManager' ID='$([GUID]::NewGuid())' DisplayName='Project Manager' Required ='FALSE' UserSelectionMode='PeopleOnly'></Field>"
#Add User Field to list
Add-PnPFieldFromXml -FieldXml $FieldXML -List $ListName
Similarly, you can also add a multi-person or group column from the XML schema.
$FieldXML= "<Field Type='UserMulti' Name='TeamMembers' ID='$([GUID]::NewGuid())' DisplayName='Team Members' Required ='FALSE' UserSelectionMode='PeopleAndGroups' Mult='TRUE' ></Field>"
Hi, do you have the same powershell code, but for On-Premise Sharepoint.
Hi,
We are working with people Picker field,
in the below code, we added Mult=’TRUE’
$FieldXML= “”
But we want to take 2 email ids, there is semicolon between email ids( in CVS file), during the run time its taking one email id only and throwing the error.
Error:
The email id xxxxx ; xxxx could not found. This is the error.
Can you suggest here.
Thanks,
Raju