Working With Site Columns On SharePoint Using PnP PowerShell

Introduction

In this article, 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 columns 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. 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 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).

  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.

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, which is used to create the site columns on SharePoint sites. The required parameters are to create the new site column; display name, internal name, group and type. The column can be made as mandatory, using the required parameter. The new values can be passed as the parameters. In my example, I have created the new site column called "PnPTextColumn", which is a text type.

  1. Add-SPOField -DisplayName "PnPTextColumn" -InternalName "PnPTextColumn" -Group "PnPGroup" -Type Text -Required  

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

  1. Add-SPOField -DisplayName "PnPColorColumn" -InternalName "PnPColorColumn" -Group "PnPGroup" -Type Choice -Choices @("red","blue","green")  

 As of now, only a few properties can be supported to create the site columns.

The image given below shows creating the new site column using PowerShell.



The image, given below, shows the created site column. The page can be accessed from http://siteurl/_layouts/15/mngfield.aspx.
 
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 the specific site columns. The properties like title, internal name, default value, description are required and can be accessed.

The code snippet, given below, shows getting all the site columns from the site:

  1. Get-SPOField  

 The code snippet, given below, shows getting a site column, using the column name:

  1. $field = Get-SPOField -Identity "PnPTextColumn"  
  2. Write-Host "Title " $field.Title  
  3. Write-Host "Internal Name" $field.InternalName  
  4. Write-Host "Default value " $field.DefaultValue  
  5. Write-Host "Description " $field.Description  
  6. Write-Host "Is Required" $field.Required  

The image, given below, shows the get operation for the birthday field or the column:

Add Site Column To Content Type
 
The site columns can be added to the site content types. The column and content type should exist before adding. Add - SPOFieldToContentType command is used to add the fields as the site columns to the content types. The code snippet, given below, shows adding the text column to a content type, using names.
  1. Add-SPOFieldToContentType -Field "PnPTextColumn" -ContentType "PnPContentType"  
The code snippet, given below, shows adding the field to the content type, using the identities.
  1. $colorColumn = Get-SPOField -Identity "PnPColorColumn"  
  2. $pnpCT = Get-SPOContentType -Identity "PnPContentType"  
  3. Add-SPOFieldToContentType -Field $colorColumn -ContentType $pnpCT 
Delete Site Column

The site column can be deleted from a SharePoint site or a sub site, using PnP PowerShell. Remove-SPOField is used to delete the columns (fields). The field name is passed to delete 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.

  1. Remove-SPOField -Identity "PnPTextColumn" -Force  

Summary

Thus, you have learned how to create, retrieve or delete the site columns on SharePoint sites and adding the columns to the content types, using PnP PowerShell. The operations will be compatible on SharePoint 2013 on premise / SharePoint online sites.

Up Next
    Ebook Download
    View all
    Learn
    View all