Working With List Columns On SharePoint Using PnP PowerShell


Introduction

In this article, you will learn how we can create, retrieve, and delete the columns on SharePoint lists, using PnP PowerShell. The Client Side Object Model is used internally for these operations. The update operation is not available for the site/list columns (fields).


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. The installers are available here. Online version installer is preferred and is named On Premise or Office 365 operations. You can also install all three installers for testing (SharePoint 2013, 2016, online).


The PnP PowerShell is supported from SharePoint 2013 On Premise and Office 365 versions. The following operations are tested on SharePoint 2013 and Office 365 environments.


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):

  1. $siteurl = "https://abc.sharepoint.com"

  2. Connect-SPOnline -Url $siteurl    

  3. $ctx = Get-SPOContext  

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


Retrieve List Columns

The list columns (fields) can be retrieved using PnP commands.

  • Get-SPOField command is used to get all the columns, available on the SharePoint list.

  • The required parameters to get list column are the list and identity. The list name is passed through the list parameter. The field name is passed through the identity parameter.

  • The properties like title, internal name, default value, description, etc. can be accessed.

The code snippet, given below, shows getting the properties of a list column from the SharePoint list, using the column name:

  1. $column = Get-SPOField -List "PnPList" -Identity "PnPListColumn"

  2. Write-Host "Column Title  :" $column.Title  

  3. Write-Host "Description   :" $column.Description 

  4.  Write-Host "Group Name    :" $column.Group  

  5. Write-Host "Internal Name :" $column.InternalName  

  6. Write-Host "Static Name   :" $column.StaticName  

  7. Write-Host "Scope         :" $column.Scope  

  8. Write-Host "Type          :" $column.TypeDisplayName  

  9. Write-Host "Schema XML    :" $column.SchemaXml  

  10. Write-Host "Is Required?  :" $column.Required  

  11. Write-Host "Is read only? :" $column.ReadOnlyField  

  12. Write-Host "Unique?       :" $column.EnforceUniqueValues  

  13. Write-Host "-------------------------------------------"

To get all the fields from a SharePoint list, the identity parameter is not required. The code snippet, given below, shows getting all the columns from SharePoint list:

  1. $columns = Get-SPOField -List "PnPList" foreach($column in $columns){      

  2. Write-Host "Column Title  :" $column.Title      

  3. Write-Host "Description   :" $column.Description      

  4. Write-Host "Group Name    :" $column.Group     

  5.  Write-Host "Internal Name :" $column.InternalName      

  6. Write-Host "Static Name   :" $column.StaticName      

  7. Write-Host "Scope         :" $column.Scope      

  8. Write-Host "Type          :" $column.TypeDisplayName      

  9. Write-Host "Schema XML    :" $column.SchemaXml      

  10. Write-Host "Is Required?  :" $column.Required      

  11. Write-Host "Is read only? :" $column.ReadOnlyField      

  12. Write-Host "Unique?       :" $column.EnforceUniqueValues      

  13. Write-Host "-------------------------------------------" }  


Create List Column

The columns can be created on the lists, available on the SharePoint site or sub site by setting the context, using the site URL. PnP CSOM PowerShell can be used to complete the operation. 

  • Add-SPOField command, which is used to create the columns on SharePoint lists.

  • The required parameters for creating a new column on the list are the display name, internal name, group name and field type. 

  • The new values can be passed as the parameters. 

  • AddToDefaultView option is used to make the list column, available on the default views. The column can be made as mandatory, using the required parameter.

In my example, I have created the new column called "PnPListColumn", which is a text type. The code snippet, given below, shows adding a new column.

  1.  Add-SPOField -DisplayName "PnPListColumn" -InternalName "PnPListColumn" -Group "PnPGroup" -Type Text -List "PnPList" -AddToDefaultView -Required  

As of now, only a few properties can be updated, while creating the fields.


The image, given below, shows the list of the default view with the list column:















Delete List Column

The columns can be deleted from a SharePoint list, using PnP PowerShell.

  • Remove-SPOField is used to delete the columns (fields).

  • The field name and the list name are required to delete the column. The name or Id can be passed with the command, using the identity parameter. The list name is passed, using the list parameter.

  • The force attribute is used to delete the field without any confirmation prompts.

The code snippet, given below, shows removing the list column.

  1. Remove-SPOField -List "PnPList" -Identity "PnPListColumn" -Force  


Summary

Thus, you have learned how to create, retrieve or delete the list columns on SharePoint lists, using PnP PowerShell. PnP PowerShell is supported from SharePoint 2013 On Premise and Office 365 versions. The operations mentioned above, are tested on SharePoint 2013 and Office 365 environments.