Working With Site Columns On SharePoint Using PnP PowerShell

In this post, you will learn, how we can create, retrieve and delete the site columns on SharePoint sites, using PnP PowerShell. Also you will see how to add site column to a site content type. The Client Side Object Model is used internally for these operations. The update operation is not available for the site columns.


Prerequisite:

You need to have PowerShell 3.0 available on a Windows machine. You need to install or import PnP PowerShell packages. You can download the installers or view more documentation on the official site. Online version installer is preferred for on premise or office 365 operations. You can also install all three installers for testing (SharePoint 2013, 2016, online).


The following operations will be compatible with SharePoint 2013 on premise and Office 365 versions.


Connect To Site:

Connect to the site, using the snippet, given below. The PnP PowerShell code, given below, helps in getting the current context of the site, using the Client Side Object Model (CSOM).




Once connected, you can carry out any of the operations mentioned below, based on the requirement.


Create Site Column:

The columns can be created on the site collections by setting the context, using the site URL. PnP CSOM PowerShell can be used to complete the operation.


Add-SPOField command is used to create the site columns on SharePoint sites. The required parameters to create the new site column are display name, internal name, group and type. The column can be made as mandatory using required parameter. The new values can be passed as the parameters. In my example, I have created new site column called "PnPTextColumn" which is text type.

The below code snippet shows creating new column with choice type. Similarly other field types can be created.

As of now only few properties can be supported for creating site columns.

The below image shows creating new site column using PowerShell.


The below image shows the site column created.


Retrieve Site Columns:

The site columns can be retrieved using PnP commands. Get-SPOField command is used to get all the site columns available on the SharePoint site collection. The identity parameter is used to retrieve specific site columns. The properties like title, internal name, default value, description, is required can be accessed.

The below code snippet shows getting all the site columns from the site.

The below code snippet shows getting a site column using column name.


$field = Get-SPOField -Identity "PnPTextColumn"

Write-Host "Title " $field.Title  

Write-Host "Internal Name" $field.InternalName  

Write-Host "Default value " $field.DefaultValue  

Write-Host "Description " $field.Description  

Write-Host "Is Required" $field.Required  


The below image shows the get operation for birthday field or column.



Add Site Column To Content Type:

The site columns can be added to site content types. The column and content type should exist before adding. Add - SPOFieldToContentType command is used in adding the fields as site columns to content types. The below code snippet shows adding text column to a content type using names.

The below code snippet shows adding field to content type using identities. $colorColumn = Get-SPOField -Identity "PnPColorColumn"

$pnpCT = Get-SPOContentType -Identity "PnPContentType"

Add-SPOFieldToContentType -Field $colorColumn -ContentType $pnpCT  Delete Site Column:

The site column can be deleted from SharePoint site or sub site, using PnP PowerShell. Remove-SPOField is used to delete the columns (fields). The field name is passed for deleting the column. The name or ID can be passed with the command, using the identity parameter. The force attribute is used to delete the field without any confirmation prompts.