SharePoint Online: Add Person or Group (People Picker) Column to List using PowerShell
Requirement: Create Person or Group Column in SharePoint Online List.
How to Add 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 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 appropriate matching name from the available values as you type. Follow these steps to add people picker column to list.
PowerShell to Create Person or Group Column to List in SharePoint Online:
How to bulk add multiple columns to List from a CSV file?
How to Add Multiple value People Picker Field?
To Add People Picker field with "Allow Multiple Selections", Use this Field schema:
PnP PowerShell to Add Person or Group Column to SharePoint Online List
How to Add 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 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 appropriate matching name from the available values as you type. Follow these steps to add people picker column to list.
- Browse to your SharePoint Online site and Navigate to the target list in which you want to add Person or Group column.
- Under the List tab, click on "Create Column" button in the ribbon.
- Provide the Name to your new column, specify the type as " Person or Group"
- Scroll down and fill other optional values such as Required column, Unique, Allow multiple values, etc and click on "OK" to create Person or Group field in SharePoint Online list.
PowerShell to Create Person or Group Column to List 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" 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="", [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 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 -UseWebLogin #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 field 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://crescenttech.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 $ListNameSimilarly, you can add multi person or group column from XML schema as well.
$FieldXML= "<Field Type='UserMulti' Name='TeamMembers' ID='$([GUID]::NewGuid())' DisplayName='Team Members' Required ='FALSE' UserSelectionMode='PeopleAndGroups' Mult='TRUE' ></Field>"
Hi,
ReplyDeleteWe 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
Hi, do you have the same powershell code, but for On-Premise Sharepoint.
ReplyDelete