How to Add a Column to List in SharePoint Online Using PowerShell?
Requirement: Add a Column to a List in SharePoint Online with PowerShell.
How to Add a Column to SharePoint Online List or Library?
SharePoint Online Lists are used to store data, and each list can have multiple columns. Columns define the type of data that can be stored in the list. For example, a list of employees may have columns for their name, department, and job title. Adding a column to a list in SharePoint Online is a straightforward process.
Here’s a step-by-step guide on how to add a new column to a SharePoint Online list using the web interface:
- Navigate to your SharePoint Online site and go to the desired list where you want to add the new column. You can access the list by clicking on its name in the site’s navigation or by going to Site Contents and selecting the list.
- Once you are on the list view, click on the “+ Add column” button, which is located in the header of the list view.
- A pop-up page will appear, offering several column types to choose from, such as:
- Single line of text
- Multiple lines of text
- Choice (drop-down menu)
- Number
- Currency
- Date and Time
- Lookup (reference to another list)
- Yes/No (checkbox)
- Person or Group
- Hyperlink or Picture
- Select the desired column type, and a new configuration panel will appear on the right side of the screen.
- In the configuration panel, provide the following information:
- Name: Enter a name for the new column.
- Description: Optionally, provide a description to help users understand the purpose of the column.
- Type-specific settings: Depending on the column type you selected, there may be additional configuration options available, such as choices for a drop-down menu, number formatting, or date and time formats.
- After you have configured the new column, click the “Save” button at the bottom of the configuration panel.
- The new column will now appear in the list view, and you can start using it to store and display additional information.
That’s it! Using the web interface, you have successfully added a new column to a SharePoint Online list. Remember that the column’s settings can be adjusted later if needed by going to the list settings page and selecting the column’s name.
Add a Column to the list using the PowerShell CSOM script
With the help of PowerShell, you can streamline your SharePoint Online workflow by automating repetitive tasks, such as adding a column to a list. Let me guide you through the steps to add a column to a list 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"
#Set parameters
$SiteURL="https://salaudeen.sharepoint.com/sites/Retail"
$ListName="Projects"
$Name="ProjectCode"
$DisplayName="Project Code"
$Description="Enter the Project Code"
$IsRequired = "TRUE"
$EnforceUniqueValues="FALSE" #Must set Indexed="FALSE", if Unique="TRUE"
$MaxLength="100"
#Generate new GUID for Field ID
$FieldID = New-Guid
Try {
#Get Credentials to connect
$Cred= Get-Credential
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#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='Text' ID='{$FieldID}' Name='$Name' StaticName='$Name' DisplayName='$DisplayName' Description='$Description' Required='$IsRequired' EnforceUniqueValues='$EnforceUniqueValues' MaxLength='$MaxLength' />"
$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
}
This can be especially useful if you need to add multiple fields or if you want to automate the process of adding fields to lists.
PnP PowerShell to Add a Column to SharePoint Online List
If you prefer to add a column to a list programmatically in SharePoint Online, use this PnP PowerShell script. Ensure that you have the PnP.PowerShel Module installed on your computer before running this script. Use the Add-PnPField cmdlet to add a new column to your SharePoint Online list. This cmdlet requires you to specify several parameters, such as the column name, column type, and display name.
#Parameters
$SiteURL = "https://Crescent.sharepoint.com/sites/Projects"
$ListName= "Team Projects"
$FieldTitle= "Project ID"
$FieldInternalName ="ProjectID"
$FieldType = "Text"
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Add new field to list
Add-PnPField -List $ListName -DisplayName $FieldTitle -InternalName $FieldInternalName -Type $FieldType -Required -AddToDefaultView -ErrorAction Stop
Write-host -f Green "New Field '$FieldTitle' Added to the List!"
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
Make sure you replace the parameters per your requirements. Similarly, You can add a field to the SharePoint Online list by its schema:
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/retail"
$ListName= "Projects"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Define XML Schema for Text Field
$FieldXML= "<Field Type='Text' Name='ProjectCode' ID='$([GUID]::NewGuid())' DisplayName='Project Code' Required ='TRUE' EnforceUniqueValues = 'TRUE' Indexed='TRUE'></Field>"
#Add a Field to list from XML schema
Add-PnPFieldFromXml -FieldXml $FieldXML -List $ListName
Refer to this Microsoft article for all available field schemas: https://learn.microsoft.com/en-us/sharepoint/dev/schema/field-element-field
How about adding other field types?
Here is my list of posts on adding other field types to the SharePoint Online list using PowerShell:
- Add Single Line of Text Column to List
- Add Calculated Column to List
- Add Lookup Column to List
- Add Choice Field to List
- Add Date and Time Column to List
- Add Person or Group (People Picker) Column
- Add Multiple Lines of Text Column to List
- Add “Yes/No” Check Box Column to List
- Add Hyperlink or Picture Column to List
- Add Managed Metadata Column to List
- Add Currency Column to List
- Add Number Column to List
Wrapping up
In this article, we walked through the process of adding a column to a list in SharePoint Online using PowerShell. By leveraging the power of the SharePoint Online CSOM and PnP PowerShell, you can automate and streamline the process of adding your SharePoint Online list columns. In conclusion, adding a column to a list in SharePoint Online using PowerShell is a simple process that can save time and reduce errors in your workflow.