A Site Column is nothing but a template of a configured column. Once you created a site column in a top-level site, you can reuse the column in any lists and libraries under the site hierarchy. You can return to a Site Column’s configuration at any time and make changes if needed, the changes in the Site Columns are automatically reflected in the places you have used them. In this post, we will explore how to create a new Site Column in SharePoint Online using PnP PowerShell.
We can use the Add-PnPField command from the PnP PowerShell module to add a site column. This command supports adding a column with different column types. Here we have provided samples to create some types of columns. You can check the supported column types and just replace the required column type in the below script to add a new column with the required field type.
- Create Text Column
- Create Multi-Line Text Column
- Create DateTime Column
- Create Choice Column
- Create Person or Group (People Picker) Column
Create Text Site Column in SharePoint Online using PnP PowerShell
The following commands create a single line of text site column in the SharePoint Online site.
#Site column configuration details
$SiteURL = "https://contoso.sharepoint.com/sites/site_name"
$ColumnName = "Product Name"
$ColumnInternalName = "ProductName"
$ColumnGroup = "MTS Site Columns"
#Specify the field type
$ColumnType = "Text"
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL
#Create Site Column
Add-PnPField -DisplayName $ColumnName -InternalName $ColumnInternalName -Type $ColumnType -Group $ColumnGroup
} Catch {
Write-Host "Error occured: $_" -f Red
}
Create Multi-Line Text Column using PnP PowerShell
We need to set the column type as Note for Multiple Lines of Text field.
#Site column configuration details
$SiteURL = "https://contoso.sharepoint.com/sites/site_name"
$ColumnName = "Product Description"
$ColumnInternalName = "ProductDescription"
$ColumnGroup = "MTS Site Columns"
#Specify the field type as Note for multi-lines text column
$ColumnType = "Note"
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL
#Create Site Column
Add-PnPField -DisplayName $ColumnName -InternalName $ColumnInternalName -Type $ColumnType -Group $ColumnGroup
} Catch {
Write-Host "Error occured: $_" -f Red
}
Create DateTime Column using PnP PowerShell
We need to set the column type as DateTime to create a date-time site column.
#Site column configuration details
$SiteURL = "https://contoso.sharepoint.com/sites/site_name"
$ColumnName = "Product Sale Date"
$ColumnInternalName = "ProductSaleDate"
$ColumnGroup = "MTS Site Columns"
#Specify the field type
$ColumnType = "DateTime"
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL
#Create Site Column
Add-PnPField -DisplayName $ColumnName -InternalName $ColumnInternalName -Type $ColumnType -Group $ColumnGroup
} Catch {
Write-Host "Error occured: $_" -f Red
}
Create Choice Column using PnP PowerShell
We need to set the column type as Choice and pass the options as a string array to the parameter Choices.
#Site column configuration details
$SiteURL = "https://contoso.sharepoint.com/sites/site_name"
$ColumnName = "Product Sale Status"
$ColumnInternalName = "Product Sale Status"
$ColumnGroup="MTS Site Columns"
#Specify the field type as Choice and provide options
$ColumnType = "Choice"
$Options = @("Demo", "Support", "Completed")
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL
#Create Site Column
Add-PnPField -DisplayName $ColumnName -InternalName $ColumnInternalName -Type $ColumnType -Choices $Options -Group $ColumnGroup
} Catch {
Write-Host "Error occured: $_" -f Red
}
Create Person or Group (People Picker) Site Column using PnP PowerShell
We need to set the column type as User to create the user picker column. We can specify the Required switch to mark the field as required/mandatory.
#Site column configuration details
$SiteURL = "https://contoso.sharepoint.com/sites/site_name"
$ColumnName = "Product Owner"
$ColumnInternalName = "ProductOwner"
$ColumnGroup = "MTS Site Columns"
#Specify the field type
$ColumnType = "User"
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL
#Create Site Column
Add-PnPField -DisplayName $ColumnName -InternalName $ColumnInternalName -Type $ColumnType -Required -Group $ColumnGroup
} Catch {
Write-Host "Error occured: $_" -f Red
}