SharePoint Online: Add “Yes/No” Check Box Column to List using PowerShell

Requirement: Add the “Yes/No (Check box)” column to the SharePoint Online list using PowerShell.

How to Create Yes/No Check box column in SharePoint Online List?

The Yes/No column in SharePoint represents the Boolean data type, and it stores either a Yes or No value based on the field’s checked or unchecked state. In this article, we will look at how to add a “Yes/No” check box column to a list using PowerShell.

To add the Yes/No field to the SharePoint Online list, follow these steps:

  1. Browse to your SharePoint Online site and navigate to the target list in which you want to add the Yes/No column.
  2. Under the List tab, click on the “Create Column” button in the ribbon.
  3. Provide the Name of your new column, and specify the type as “Yes/No Check box”.powershell to add yes no checkbox field to list in sharepoint online
  4. Fill in other optional values, such as field description, Default Value, and Click on “OK” to create a Yes/No check box field in the SharePoint Online list.

PowerShell to Create “Yes/No” Check Box Column in SharePoint Online List:

Here is the PowerShell script to add the Yes/No field to a Sharepoint Online list, with the below steps:

  • Step 1: Connect to SharePoint Online
  • Step 2: Get the List
  • Step 3: Check if the list has the column already
  • Step 4: Add the Column
#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-YesNoColumnToList()
{ 
    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] $DefaultValue = "0"
    )

    #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
        {
            #Define XML for Field Schema
            $FieldSchema = "<Field Type='Boolean' ID='{$FieldID}' DisplayName='$DisplayName' Name='$Name' Description='$Description'><Default>$DefaultValue</Default></Field>"
            $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="IsActive"
$DisplayName="Is Active"
$Description="Specify if the Project is Active"
$DefaultValue="1" #0 for No / 1 for Yes

#Call the function to add column to list
Add-YesNoColumnToList -SiteURL $SiteURL -ListName $ListName -Name $Name -DisplayName $DisplayName -Description $Description -DefaultValue $DefaultValue

PnP PowerShell to Add Yes/No Column to SharePoint Online List

Use the Add-PnPField cmdlet in PnP PowerShell to add a Yes/No field to a list:

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
$ListName= "Projects"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Add Yes/No Field to list
Add-PnPField -Type Boolean -List $ListName -DisplayName "Is Active" -InternalName "IsActive" -AddToDefaultView

Similarly, you can add Yes/No column from the XML schema.

#Define XML Schema for currency Field
$FieldXML= "<Field Type='Boolean' Name='IsActive' ID='$([GUID]::NewGuid())' DisplayName='Is Active' Required ='FALSE'></Field>"

#Add Currency Field to list from XML
Add-PnPFieldFromXml -FieldXml $FieldXML -List $ListName 

In conclusion, The “Yes/No” check box column is useful for storing simple Boolean values in a list. You can easily add the column to the list by following the steps outlined in this article.

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

Your email address will not be published. Required fields are marked *