Yes/No Column PnP PowerShell

PnP PowerShell: Add Choice Column in SharePoint Online

SharePoint Online is a powerful platform that enables organizations to collaborate, manage documents, and streamline business processes. One of the key features of SharePoint is the ability to create custom columns that enhance data organization and improve the user experience. In this blog post, we will explore how to create a choice column in SharePoint Online using the Patterns and Practices (PnP) PowerShell module. Choice columns allow you to provide predefined options for users to select from, ensuring consistent and accurate data entry.

Prerequisites

Before we dive into the steps, make sure you have the following prerequisites in place:

  1. SharePoint Online Site: You need access to a SharePoint Online site collection where you have the necessary permissions to create columns.
  2. PnP PowerShell Module: Install the PnP PowerShell module if you haven’t already. To setup the PnP PowerShell Module, you can follow another blog post written by me at PnP PowerShell: How to get started? – Hemant Kabra

Creating a Choice Column

Follow these steps to create a choice column in SharePoint Online using PnP PowerShell:

Step 1: Connect to SharePoint Online

Open PowerShell and connect to your SharePoint Online site using the Connect-PnPOnline cmdlet. Replace https://yourdomain.sharepoint.com/sites/yoursite with the URL of your SharePoint site.

Connect-PnPOnline -Url https://yourdomain.sharepoint.com/sites/yoursite -Interactive

Step 2: Define Choice Options

Before creating the choice column, define the options that users will be able to select from. Create an array of option values, for example:

$choiceOptions = "Option 1", "Option 2", "Option 3"

Step 3: Create the Choice Column

Use the Add-PnPField cmdlet to create the choice column. Specify the column name, display name, and the choice options.

# Define the column properties
$columnName = "YourColumnName"
$columnDisplayName = "Your Column Display Name"
$columnGroup = "Your Column Group" # Optional: You can group columns together for better organization.


# This will add a site column of type choice with the given choice options
Add-PnPField -DisplayName $columnDisplayName -InternalName $columnName -Type Choice -Group $columnGroup -Choices $choiceOptions

Below is another example of choice column which will add a multiple-choice column to the list. It will allow users to pick several choices for the same item.

# Define the column properties
$columnName = "YourColumnName"
$columnDisplayName = "Your Column Display Name"
$columnGroup = "Your Column Group" # Optional: You can group columns together for better organization.
$ListName = "YourListName"

Add-PnPField -List $ListName -DisplayName $columnDisplayName -InternalName $columnName -Type MultiChoice -Group $columnGroup -AddToDefaultView -Choices $choiceOptions

Alternatively, you can also use XML schema to add a new choice column. Below is an exmple,

$ListName = "YourListName"

# Define XML Schema
$FieldXML= "<Field Type='Choice' DisplayName='Your Column Display Name' Required='TRUE' Format='Dropdown' ID='$([GUID]::NewGuid())' Name='YourColumnName'><Default>Choice #1</Default><CHOICES><CHOICE>Choice #1</CHOICE><CHOICE>Choice #2</CHOICE><CHOICE>Choice #3</CHOICE></CHOICES></Field>"

# Add the Column to the SharePoint Online List
Add-PnPFieldFromXml -FieldXml $FieldXML -List $ListName

In the above all the examples,

  • Replace “YourColumnName” with the internal name you want to assign to the column. It should be unique and not contain spaces.
  • Replace “Your Column Display Name” with the user-friendly display name you want to show for the column.
  • Replace “Your Column Group” with the name of the column group. You can omit this line if you don’t want to group the column.
  • Replace “YourListName” with the internal name of the SharePoint Online List.

Explanation of the parameters:

  • -DisplayName: The display name of the site column that will be visible to users.
  • -InternalName: The internal name of the site column used for internal references. It must be unique and not contain spaces.
  • -Type: The type of the site column. For a Choice column, we use “Choice” or “MultiChoice”.
  • -Group: The name of the column group where the site column will be added. You can use an existing group or create a new one.
  • -List: The name of the SharePoint Online list where the site column will be added.
  • -AddToDefaultView: Adds the column to the Default List View of the SharePoint Online List

Step 4: Verify the Column Creation

To verify the successful creation of the column, navigate to the settings of your SharePoint site and review the list of site columns.

You can also use the PnP PowerShell to verify the newly created column using the below mentioned code:

Get-PnPSiteColumn -Identity $columnName

This command will display the details of the newly created site column, including its name, display name, and other properties.

Conclusion

Creating a choice column in SharePoint Online using PnP PowerShell is a straightforward process that empowers you to better organize and manage your data. By providing users with predefined options, you can ensure data consistency and improve the overall user experience. The PnP PowerShell module simplifies the column creation process, enabling you to efficiently customize your SharePoint sites to meet your organization’s needs. Start leveraging choice columns today and enhance the way you capture and categorize information in SharePoint Online.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *