kwizcom banner advertisement

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:
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"
    sharepoint online add person or group column to list using PowerShell
  • 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 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' />"
SharePoint Online: Add Person or Group (People Picker) Column to List using PowerShell  SharePoint Online: Add Person or Group (People Picker) Column to List using PowerShell Reviewed by Salaudeen Rajack on 2:54 PM Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.