Add Site Column to List in SharePoint Online using PnP PowerShell

A Site Column is nothing but a column template that can be configured in a top-level site. 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.  In this post, we will explore how to add an existing site column to List (or Library) and how to add a site column to List content type.

Before starting, you should have already created the required site column. If you have not created it, refer to this post: Create Site Column in SharePoint Online using PnP PowerShell.

Summary

Add an existing Site Column to List using PnP PowerShell

The following commands add the existing site column to the given List or Library under the default content type.

#Provide Site URL and List name
$SiteURL = "https://contoso.sharepoint.com/sites/site_name"
$ListName= "Product Sales Report"

#Provide Internal Name of the existing Site Column
$ColumnName= "ProjectName"
 
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL
     
#Add existing Site column to List
Add-PnPField -List $ListName -Field $ColumnName -ErrorAction Stop
Write-host "Site column '$ColumnName' successfully added to the List" -f Green
}
catch {
Write-host "Error occured: $_" -f Red
}

Add Site Column to List Content Type using PnP PowerShell

The above script adds the site column to the default content type of the List. If you want to add the column in custom content type instead of default content type, we have to use the Add-PnPFieldToContentType cmdlet to add the site column directly to the required content type.

#Provide Site URL and Content type name
$SiteURL = "https://contoso.sharepoint.com/sites/site_name"
$ContentTypeName = "SalesItem"

#Provide Internal Name of the existing Site Column
$ColumnName= "ProductOwner"
 
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials $Cred
     
#Add Existing Site Column to Content type
Add-PnPFieldToContentType -Field $ColumnName -ContentType $ContentTypeName -ErrorAction Stop
Write-host "Site column '$ColumnName' successfully added to the Content type" -f Green

}
catch {
Write-host "Error occured: $_" -f Red
}
Advertisement

1 thought on “Add Site Column to List in SharePoint Online using PnP PowerShell”

  1. Morgan,

    I am unable to use this command when adding a site column from a higher level site in SPO. Do you have any guidance?

    Reply

Leave a Comment