Site columns in SharePoint OnlineSite columns provides great re-usability in SharePoint without having to recreate fields every time you need them in lists and libraries. They save a lot of time especially when you have to standardize columns, create columns with data (such as choice fields) or assign default values to them. Once created at the top site, a site column can be utilized in any list or library or even content types throughout your site collection (or across site collections with content type hub).
How to Create Site Column in SharePoint Online?To create site column in SharePoint online, follow these steps:
- Login to SharePoint online site. Click on Site Settings Gear and select Site Settings.
- On Site Settings page, Click on "Site Columns" link under "Web Designer Galleries" group.
- In Site Columns page, Click on "Create" Link at the top.
- In create column page, Enter the name for your site column. Select the column type, Specify settings specific to the column. Click on "OK" button at the bottom to complete creating site column in SharePoint Online.Now the site column will be added to the site.
Create Site Columns using PowerShell in SharePoint Online:Here is my collection of PowerShell scripts to create site columns in SharePoint online using client side object mode (CSOM).
Important: Field Schema XML is case sensitive!
Create Single Line of Text Site Column in SharePoint Online 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"
#Parameters
$SiteURL="https://crescent.sharepoint.com"
$ColumnName="ProjectCode"
$IsRequired = "TRUE"
$ColumnGroup="Crescent Projects"
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 all Site columns from the site
$Fields = $Ctx.web.Fields
$Ctx.Load($Fields)
$Ctx.executeQuery()
#Check if the column name exists
$NewField = $Fields | where {$_.Title -eq $ColumnName}
if($NewField -ne $NULL)
{
Write-host "Site Column $ColumnName already exists!" -f Yellow
}
else
{
#Define XML for Field Schema
$FieldSchema = "<Field Type='Text' DisplayName='$ColumnName' Name='$ColumnName' required='$IsRequired' Group='$ColumnGroup'/>"
$NewField = $Fields.AddFieldAsXml($FieldSchema,$True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldToDefaultView)
$Ctx.ExecuteQuery()
Write-host "Site Column Created Successfully!" -ForegroundColor Green
}
}
Catch {
write-host -f Red "Error Creating Site Column!" $_.Exception.Message
}
Add Multiple Lines of Text Field using PowerShell: This time, lets wrap the code into a re-usable function
#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 Multiline Text Field
Function Add-MultilineSiteColumn()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $ColumnName,
[Parameter(Mandatory=$true)] [string] $NumberofLines,
[Parameter(Mandatory=$true)] [string] $IsRequired,
[Parameter(Mandatory=$true)] [string] $RichText,
[Parameter(Mandatory=$true)] [string] $ColumnGroup
)
#Generate new GUID for Field ID
$FieldID = ([GUID]::NewGuid()).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 all Site columns from the site
$Fields = $Ctx.web.Fields
$Ctx.Load($Fields)
$Ctx.executeQuery()
#Check if the column name exists
$NewField = $Fields | where {$_.Title -eq $ColumnName}
if($NewField -ne $NULL)
{
Write-host "Site Column $ColumnName already exists!" -f Yellow
}
else
{
#Define XML for Field Schema
$FieldSchema = "<Field Type='Note' ID='{$FieldID}' DisplayName='$ColumnName' Name='$ColumnName' RichText='$RichText' Required='$IsRequired' Group='$ColumnGroup'/>"
$NewField = $Fields.AddFieldAsXml($FieldSchema,$True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldToDefaultView)
$Ctx.ExecuteQuery()
Write-host "Site Column Created Successfully!" -ForegroundColor Green
}
}
Catch {
write-host -f Red "Error Creating Site Column!" $_.Exception.Message
}
}
#Define value for parameters
$SiteURL="https://crescent.sharepoint.com"
$ColumnName="ProjectDescription"
$IsRequired = "TRUE"
$ColumnGroup="Crescent Projects"
$RichText ="FALSE"
$NumberofLines="5"
#Call the function to create Multiline site column
Add-MultilineSiteColumn -SiteURL $SiteURL -ColumnName $ColumnName -IsRequired $IsRequired -NumberofLines $NumberofLines -RichText $RichText -ColumnGroup $ColumnGroup
PowerShell to Create Person or Group (People Picker) Site Column to 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"
#Custom function to add site column
Function Add-PersonOrGroupSiteColumn()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $ColumnName,
[Parameter(Mandatory=$true)] [string] $ColumnDisplayName,
[Parameter(Mandatory=$true)] [string] $IsRequired,
[Parameter(Mandatory=$true)] [string] $ColumnGroup
)
#Generate new GUID for Field ID
$FieldID = ([GUID]::NewGuid()).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 all Site columns from the site
$Fields = $Ctx.web.Fields
$Ctx.Load($Fields)
$Ctx.executeQuery()
#Check if the column name exists already
$NewField = $Fields | where { ($_.Internalname -eq $ColumnName) -or ($_.Title -eq $ColumnDisplayName) }
if($NewField -ne $NULL)
{
Write-host "Site Column $ColumnName already exists!" -f Yellow
}
else
{
#Frame Choices
$ChoiceOptions=""
$Choices = $ChoiceValues.Split(",")
foreach ($Choice in $Choices)
{
$ChoiceOptions = $ChoiceOptions + "<CHOICE>$Choice</CHOICE>"
}
#Define XML for Field Schema
$FieldSchema = "<Field Type='User' ID='{$FieldID}' DisplayName='$ColumnDisplayName' Name='$ColumnName' Required='$IsRequired' Group='$ColumnGroup' ShowField='ImnName' List='UserInfo' UserSelectionMode='PeopleOnly' />"
$FieldSchema
$NewField = $Fields.AddFieldAsXml($FieldSchema,$True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldToDefaultView)
$Ctx.ExecuteQuery()
Write-host "Site Column Created Successfully!" -ForegroundColor Green
}
}
Catch {
write-host -f Red "Error Creating Site Column!" $_.Exception.Message
}
}
#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ColumnName="ProjectMembers"
$ColumnDisplayName="Project Members"
$IsRequired = "TRUE"
$ColumnGroup="Crescent Projects"
#Call the function to create site column
Add-PersonOrGroupSiteColumn -SiteURL $SiteURL -ColumnName $ColumnName -ColumnDisplayName $ColumnDisplayName -IsRequired $IsRequired -ColumnGroup $ColumnGroup
Add Date and Time Site Column to 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"
#Custom function to add site column
Function Add-DateTimeSiteColumn()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $ColumnName,
[Parameter(Mandatory=$true)] [string] $ColumnDisplayName,
[Parameter(Mandatory=$true)] [string] $Format,
[Parameter(Mandatory=$true)] [string] $IsRequired,
[Parameter(Mandatory=$true)] [string] $ColumnGroup
)
#Generate new GUID for Field ID
$FieldID = ([GUID]::NewGuid()).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 all Site columns from the site
$Fields = $Ctx.web.Fields
$Ctx.Load($Fields)
$Ctx.executeQuery()
#Check if the column name exists already
$NewField = $Fields | where { ($_.Internalname -eq $ColumnName) -or ($_.Title -eq $ColumnDisplayName) }
if($NewField -ne $NULL)
{
Write-host "Site Column $ColumnName already exists!" -f Yellow
}
else
{
#Define XML for Field Schema
$FieldSchema = "<Field Type='DateTime' ID='{$FieldID}' DisplayName='$ColumnDisplayName' Name='$ColumnName' Format='$Format' Required='$IsRequired' Group='$ColumnGroup'/>"
$NewField = $Fields.AddFieldAsXml($FieldSchema,$True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldToDefaultView)
$Ctx.ExecuteQuery()
Write-host "Site Column Created Successfully!" -ForegroundColor Green
}
}
Catch {
write-host -f Red "Error Creating Site Column!" $_.Exception.Message
}
}
#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ColumnName="ProjectStartDate"
$ColumnDisplayName="Project Start Date"
$Format="DateOnly"
$IsRequired = "FALSE"
$ColumnGroup="Crescent Projects"
#Call the function to create site column
Add-DateTimeSiteColumn -SiteURL $SiteURL -ColumnName $ColumnName -ColumnDisplayName $ColumnDisplayName -IsRequired $IsRequired -Format $Format -ColumnGroup $ColumnGroup
PowerShell CSOM script to Add Currency Site Column for 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"
#Custom function to add site column
Function Add-CurrencySiteColumn()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $ColumnName,
[Parameter(Mandatory=$true)] [string] $ColumnDisplayName,
[Parameter(Mandatory=$true)] [string] $LCID,
[Parameter(Mandatory=$true)] [string] $IsRequired,
[Parameter(Mandatory=$true)] [string] $ColumnGroup
)
#Generate new GUID for Field ID
$FieldID = ([GUID]::NewGuid()).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 all Site columns from the site
$Fields = $Ctx.web.Fields
$Ctx.Load($Fields)
$Ctx.executeQuery()
#Check if the column name exists already
$NewField = $Fields | where { ($_.Internalname -eq $ColumnName) -or ($_.Title -eq $ColumnDisplayName) }
if($NewField -ne $NULL)
{
Write-host "Site Column $ColumnName already exists!" -f Yellow
}
else
{
#Define XML for Field Schema
$FieldSchema = "<Field Type='Currency' ID='{$FieldID}' DisplayName='$ColumnDisplayName' Name='$ColumnName' LCID='$LCID' Required='$IsRequired' Group='$ColumnGroup'/>"
$NewField = $Fields.AddFieldAsXml($FieldSchema,$True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldToDefaultView)
$Ctx.ExecuteQuery()
Write-host "Site Column Created Successfully!" -ForegroundColor Green
}
}
Catch {
write-host -f Red "Error Creating Site Column!" $_.Exception.Message
}
}
#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ColumnName="TotalInvestment"
$ColumnDisplayName="Total Investment Amount"
$LCID="1081" #India
$IsRequired = "TRUE"
$ColumnGroup="Crescent Projects"
#Call the function to create site column
Add-CurrencySiteColumn -SiteURL $SiteURL -ColumnName $ColumnName -ColumnDisplayName $ColumnDisplayName -IsRequired $IsRequired -LCID $LCID -ColumnGroup $ColumnGroup
Refer
https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splocale.lcid.aspx for all LCIDs.
SharePoint Online: Add Number Site Column with 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"
#Custom function to add site column
Function Add-NumberSiteColumn()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $ColumnName,
[Parameter(Mandatory=$true)] [string] $ColumnDisplayName,
[Parameter(Mandatory=$true)] [string] $IsRequired,
[Parameter(Mandatory=$true)] [string] $ColumnGroup
)
#Generate new GUID for Field ID
$FieldID = ([GUID]::NewGuid()).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 all Site columns from the site
$Fields = $Ctx.web.Fields
$Ctx.Load($Fields)
$Ctx.executeQuery()
#Check if the column name exists
$NewField = $Fields | where { ($_.Internalname -eq $ColumnName) -or ($_.Title -eq $ColumnDisplayName) }
if($NewField -ne $NULL)
{
Write-host "Site Column $ColumnName already exists!" -f Yellow
}
else
{
#Define XML for Field Schema
$FieldSchema = "<Field Type='Number' ID='{$FieldID}' DisplayName='$ColumnDisplayName' Name='$ColumnName' Required='$IsRequired' Group='$ColumnGroup'/>"
$NewField = $Fields.AddFieldAsXml($FieldSchema,$True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldToDefaultView)
$Ctx.ExecuteQuery()
Write-host "Site Column Created Successfully!" -ForegroundColor Green
}
}
Catch {
write-host -f Red "Error Creating Site Column!" $_.Exception.Message
}
}
#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ColumnName="HeadCount"
$ColumnDisplayName="Head Count"
$IsRequired = "TRUE"
$ColumnGroup="Crescent Projects"
#Call the function to create site column
Add-NumberSiteColumn -SiteURL $SiteURL -ColumnName $ColumnName -ColumnDisplayName $ColumnDisplayName -IsRequired $IsRequired -ColumnGroup $ColumnGroup
How to Add Choice Site Column to SharePoint Online 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"
#Custom function to add site column
Function Add-ChoiceSiteColumn()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $ColumnName,
[Parameter(Mandatory=$true)] [string] $ColumnDisplayName,
[Parameter(Mandatory=$true)] [string] $ChoiceValues,
[Parameter(Mandatory=$true)] [string] $IsRequired,
[Parameter(Mandatory=$true)] [string] $ColumnGroup
)
#Generate new GUID for Field ID
$FieldID = ([GUID]::NewGuid()).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 all Site columns from the site
$Fields = $Ctx.web.Fields
$Ctx.Load($Fields)
$Ctx.executeQuery()
#Check if the column name exists already
$NewField = $Fields | where { ($_.Internalname -eq $ColumnName) -or ($_.Title -eq $ColumnDisplayName) }
if($NewField -ne $NULL)
{
Write-host "Site Column $ColumnName already exists!" -f Yellow
}
else
{
#Frame Choices
$ChoiceOptions=""
$Choices = $ChoiceValues.Split(",")
foreach ($Choice in $Choices)
{
$ChoiceOptions = $ChoiceOptions + "<CHOICE>$Choice</CHOICE>"
}
#Define XML for Field Schema
$FieldSchema = "<Field Type='Choice' ID='{$FieldID}' DisplayName='$ColumnDisplayName' Name='$ColumnName' Required='$IsRequired' Group='$ColumnGroup'> <CHOICES>$ChoiceOptions</CHOICES></Field>"
$FieldSchema
$NewField = $Fields.AddFieldAsXml($FieldSchema,$True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldToDefaultView)
$Ctx.ExecuteQuery()
Write-host "Site Column Created Successfully!" -ForegroundColor Green
}
}
Catch {
write-host -f Red "Error Creating Site Column!" $_.Exception.Message
}
}
#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ColumnName="ProjectDepartment"
$ColumnDisplayName="Project Department"
$IsRequired = "TRUE"
$ChoiceValues="IT,Sales,Operations,Marketing,HR"
$ColumnGroup="Crescent Projects"
#Call the function to create site column
Add-ChoiceSiteColumn -SiteURL $SiteURL -ColumnName $ColumnName -ColumnDisplayName $ColumnDisplayName -IsRequired $IsRequired -ChoiceValues $ChoiceValues -ColumnGroup $ColumnGroup
You can also add Type="MultiChoice", Format="RadioButtons" to customize it further.
Create Lookup Site Column in SharePoint Online 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"
#Custom function to add site column
Function Add-LookupSiteColumn()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $ColumnName,
[Parameter(Mandatory=$true)] [string] $ColumnDisplayName,
[Parameter(Mandatory=$true)] [string] $LookupListName,
[Parameter(Mandatory=$true)] [string] $LookupField,
[Parameter(Mandatory=$true)] [string] $IsRequired,
[Parameter(Mandatory=$true)] [string] $ColumnGroup
)
#Generate new GUID for Field ID
$FieldID = ([GUID]::NewGuid()).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 all Site columns from the site
$Web = $Ctx.web
$Ctx.Load($Web)
$Fields = $web.Fields
$Ctx.Load($Fields)
$Ctx.executeQuery()
#Check if the column name exists already
$NewField = $Fields | where { ($_.Internalname -eq $ColumnName) -or ($_.Title -eq $ColumnDisplayName) }
if($NewField -ne $NULL)
{
Write-host "Site Column $ColumnName already exists!" -f Yellow
}
else
{
#Get IDs of Lookup List and Web
$LookupList=$Web.Lists.GetByTitle($LookupListName)
$Ctx.Load($LookupList)
$Ctx.executeQuery()
$LookupListID= $LookupList.id
$LookupWebID=$Ctx.web.Id
#Define XML for Field Schema
$FieldSchema = "<Field Type='Lookup' ID='{$FieldID}' DisplayName='$ColumnDisplayName' Name='$ColumnName' Required='$IsRequired' Group='$ColumnGroup' List='$LookupListID' WebId='$LookupWebID' ShowField='$LookupField' />"
$NewField = $Fields.AddFieldAsXml($FieldSchema,$True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldToDefaultView)
$Ctx.ExecuteQuery()
Write-host "Site Column Created Successfully!" -ForegroundColor Green
}
}
Catch {
write-host -f Red "Error Creating Site Column!" $_.Exception.Message
}
}
#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ColumnName="ParentProject"
$ColumnDisplayName="Parent Project"
$IsRequired = "TRUE"
$ColumnGroup="Crescent Projects"
$LookupList="Projects" #Parent Lookup
$LookupField="ProjectName"
#Call the function to create site column
Add-LookupSiteColumn -SiteURL $SiteURL -ColumnName $ColumnName -ColumnDisplayName $ColumnDisplayName -IsRequired $IsRequired -ColumnGroup $ColumnGroup -LookupList $LookupList -LookupField $LookupField
Refer this MSDN article to frame schema XML attributes for site columns: https://msdn.microsoft.com/en-us/library/office/aa979575.aspx