Monday, September 26, 2016

SharePoint Online: Add Choice Column to List using PowerShell

How to Add Choice Field to SharePoint Online List?

  • Browse to your SharePoint Online site and Navigate to the target list in which you want to add Choice column.
  • Under the List tab, click on "Create Column" button in the ribbon.
  • Provide the Name to your new column, specify the type as "Choice (menu to choose from)" 
    sharepoint online add choice column to list using powershell
  • Scroll down and enter choices to the given list as one choice per line
    powershell to create choice column in sharepoint list
  • Fill other optional values such as Choice field type (Dropdown/Radio button/Checkbox) and Click on "OK" to create Choice field in SharePoint Online list.

PowerShell to Create Choice Field in SharePoint Online List:
Here is the PowerShell script to add choice column to SharePoint Online list.
#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"

#Custom function to add column to list
Function Add-ChoiceColumnToList()
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $Name,
        [Parameter(Mandatory=$true)] [string] $DisplayName,
        [Parameter(Mandatory=$true)] [string] $ChoiceValues,
        [Parameter(Mandatory=$true)] [string] $DefaultValue,
        [Parameter(Mandatory=$false)] [string] $Description="",
        [Parameter(Mandatory=$false)] [string] $IsRequired = "FALSE",
        [Parameter(Mandatory=$false)] [string] $EnforceUniqueValues= "FALSE",
        [Parameter(Mandatory=$false)] [string] $Format ="Dropdown",
        [Parameter(Mandatory=$false)] [string] $FillInChoice="FALSE"

    #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)

        #Check if the column exists in list already
        $Fields = $List.Fields
        $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
           #Frame Choices
            $Choices = $ChoiceValues.Split(",")
            foreach ($Choice in $Choices)
                $ChoiceOptions = $ChoiceOptions + "<CHOICE>$Choice</CHOICE>"
            #Define XML for Field Schema
            $FieldSchema = "<Field Type='Choice' ID='{$FieldID}' DisplayName='$DisplayName' Name='$Name' Description='$Description' Required='$IsRequired' FillInChoice='$FillInChoice' Format='$Format'><Default>$DefaultValue</Default> <CHOICES>$ChoiceOptions</CHOICES></Field>"
            $NewField = $List.Fields.AddFieldAsXml($FieldSchema,$True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)

            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
$DisplayName="Project Department"
$Description="Enter the Project Department"
$Format="RadioButtons" #Override default parameter value Dropdown

#Call the function to add column to list
Add-ChoiceColumnToList -SiteURL $SiteURL -ListName $ListName -Name $Name -DisplayName $DisplayName -Description $Description -Format $Format -ChoiceValues $ChoiceValues -DefaultValue $DefaultValue
This PowerShell script adds a new choice column for given parameters.

Tips: How to Add MultiChoice Column? Just change "Field Type='MultiChoice'" in the Field schema XML

You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.

Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...