List Operations in SharePoint Online using the PnP PowerShell – Add Column, Remove Column, Get Colum

SharePoint Online: List operations using PnP PowerShell

No comments

 8,439 total views,  140 views today

In this article, we will learn about various list operations in SharePoint Online using the PnP PowerShell script, like how to add a column, how to get column details, and how to delete a column using the PnP PowerShell script in the SharePoint Online list.

How to add a column in SharePoint online list using PnP PowerShell?

Using the below PnP PowerShell code we can add the column in the SharePoint Online list. The “Add-PnPField” command is used to create a column in the SharePoint list.


#Create list column using the PnP PowerShell
Add-PnPField -DisplayName "PnPLTestColumn" -InternalName "PnPLTestColumn" -Group "Custom Columns" -Type Text -List $listName -AddToDefaultView -Required
#Create list column using the PnP PowerShell - ends here

How to get column property in SharePoint online list using PnP PowerShell?

Using the below PnP PowerShell code, we can get the column property for the given column. Here the “Get-PnPField” command is responsible for getting the property of the column. In the below example I have used the “Title” column as the key column – meaning bypassing the “Title” column as the identity parameter, we get all properties of the “Title” column.


#The below codes getting the column property of Title column.

$myColumn = Get-PnPField -List $listName -Identity "Title"
Write-Host "Description :" $myColumn.Description
Write-Host "Group Name :" $myColumn.Group
Write-Host "Internal Name :" $myColumn.InternalName
Write-Host "Static Name :" $myColumn.StaticName
Write-Host "Scope :" $myColumn.Scope
Write-Host "Type :" $myColumn.TypeDisplayName
Write-Host "Schema XML :" $myColumn.SchemaXml
Write-Host "Is Required? :" $myColumn.Required
Write-Host "Is read only? :" $myColumn.ReadOnlyField
Write-Host "Unique? :" $myColumn.EnforceUniqueValues
Write-Host "----------------------------------"

#The below codes getting the column property of Title column - ends here.

How to get all column properties in SharePoint online list using PnP PowerShell?

Using the below PnP PowerShell we will get all column properties in SharePoint online list – it just the same as the above command “Get-PnPField” which only takes the list as a parameter, then it loops through all the columns for the given list.


#The below codes generate all column properties from the given list.

$allColumns = Get-PnPField -List $listName
foreach($oneColumn in $allColumns)
{
Write-Host "Column Title :" $oneColumn.Title
Write-Host "Description :" $oneColumn.Description
Write-Host "Group Name :" $oneColumn.Group
Write-Host "Internal Name :" $oneColumn.InternalName
Write-Host "Static Name :" $oneColumn.StaticName
Write-Host "Scope :" $oneColumn.Scope
Write-Host "Type :" $oneColumn.TypeDisplayName
Write-Host "Schema XML :" $oneColumn.SchemaXml
Write-Host "Is Required? :" $oneColumn.Required
Write-Host "Is read only? :" $oneColumn.ReadOnlyField
Write-Host "Unique? :" $oneColumn.EnforceUniqueValues
Write-Host "---------------------------------"
}

How to delete a column in SharePoint online list using the PnP PowerShell?

Using the below PnP PowerShell command we can delete a column from the SharePoint Online list. The “Remove-SPOField” is used to delete the column from the list which takes the list as the list name and identity as the column name parameter.


#The below code removes the particular list column from the given list.

Remove-SPOField -List $listName -Identity "PnPLTestColumn" -Force

#The below code removes the particular list column from the given list - ends here

List operations in SharePoint Online using PnP PowerShell – complete code

Below is the complete code for the list operations in SharePoint Online using the PnP PowerShell.


####The below script is used to do the PnP list operations like Create, Read, Delete columns in the SharePoint online list.

cls

$PSshell = Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorVariable err -ErrorAction SilentlyContinue
if($PSshell -eq $null)
{
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}

$fileName = "List_Operation_using_PnP_PowerShell"

#'yyyyMMddhhmm yyyyMMdd
$enddate = (Get-Date).tostring("yyyyMMddhhmmss")
#$filename = $enddate + '_VMReport.doc'
$logFileName = $fileName +"_"+ $enddate+"_Log.txt"
$invocation = (Get-Variable MyInvocation).Value
$directoryPath = Split-Path $invocation.MyCommand.Path

$directoryPathForLog=$directoryPath+"\"+"LogFiles"
if(!(Test-Path -path $directoryPathForLog))
{
New-Item -ItemType directory -Path $directoryPathForLog
#Write-Host "Please Provide Proper Log Path" -ForegroundColor Red
}
#$logPath = $directoryPath + "\" + $logFileName

$logPath = $directoryPathForLog + "\" + $logFileName

$isLogFileCreated = $False

function Write-Log([string]$logMsg)
{
if(!$isLogFileCreated){
Write-Host "Creating Log File..."
if(!(Test-Path -path $directoryPath))
{
Write-Host "Please Provide Proper Log Path" -ForegroundColor Red
}
else
{
$script:isLogFileCreated = $True
Write-Host "Log File ($logFileName) Created..."
[string]$logMessage = [System.String]::Format("[$(Get-Date)] - {0}", $logMsg)
Add-Content -Path $logPath -Value $logMessage
}
}
else
{
[string]$logMessage = [System.String]::Format("[$(Get-Date)] - {0}", $logMsg)
Add-Content -Path $logPath -Value $logMessage
}
}

 

#Paramaters area
$adminSiteURL="https://globalsharepoint2020-admin.sharepoint.com"
$SiteURL="https://globalsharepoint2020.sharepoint.com/"
$userName = "YourSPOUser@YourSPOTenant.sharepoint.com"
$passWord = "YourSPOPassword"
$encPassWord = convertto-securestring -String $passWord -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $userName, $encPassWord
$listName="Employee"
#Paramaters area - Ends

try
{
Connect-PnPOnline -Url $SiteURL -Credentials $cred

#Create list column using the PnP PowerShell
Add-PnPField -DisplayName "PnPLTestColumn" -InternalName "PnPLTestColumn" -Group "Custom Columns" -Type Text -List $listName -AddToDefaultView -Required
#Create list column using the PnP PowerShell - ends here

#The below codes getting the column property of the Title column.

$myColumn = Get-PnPField -List $listName -Identity "Title"
Write-Host "Description :" $myColumn.Description
Write-Host "Group Name :" $myColumn.Group
Write-Host "Internal Name :" $myColumn.InternalName
Write-Host "Static Name :" $myColumn.StaticName
Write-Host "Scope :" $myColumn.Scope
Write-Host "Type :" $myColumn.TypeDisplayName
Write-Host "Schema XML :" $myColumn.SchemaXml
Write-Host "Is Required? :" $myColumn.Required
Write-Host "Is read only? :" $myColumn.ReadOnlyField
Write-Host "Unique? :" $myColumn.EnforceUniqueValues
Write-Host "----------------------------------"

#The below codes getting the column property of Title column - ends here.

#The below codes generate all columns properties from the given list.

$allColumns = Get-PnPField -List $listName
foreach($oneColumn in $allColumns)
{
Write-Host "Column Title :" $oneColumn.Title
Write-Host "Description :" $oneColumn.Description
Write-Host "Group Name :" $oneColumn.Group
Write-Host "Internal Name :" $oneColumn.InternalName
Write-Host "Static Name :" $oneColumn.StaticName
Write-Host "Scope :" $oneColumn.Scope
Write-Host "Type :" $oneColumn.TypeDisplayName
Write-Host "Schema XML :" $oneColumn.SchemaXml
Write-Host "Is Required? :" $oneColumn.Required
Write-Host "Is read only? :" $oneColumn.ReadOnlyField
Write-Host "Unique? :" $oneColumn.EnforceUniqueValues
Write-Host "---------------------------------"
}

#The below code removes the particular list column from the given list.

Remove-SPOField -List $listName -Identity "PnPLTestColumn" -Force

#The below code removes the particular list column from the given list - ends here

}
catch
{

$ErrorMessage = $_.Exception.Message +"in PnP list operation Create, Read, Delete columns!:"
Write-Host $ErrorMessage -BackgroundColor Red
Write-Log $ErrorMessage

}

Summary: What we had here?

Thus, in this article we have learned the below with respect to list operations in SharePoint Online using the PnP PowerShell:

  • How to add a column in SharePoint online list using PnP PowerShell?
  • How to get column property in SharePoint online list using PnP PowerShell?
  • How to get all column properties in SharePoint online list using PnP PowerShell?
  • How to delete a column in SharePoint online list using the PnP PowerShell?
  • How to remove a column from the SharePoint Online list using PnP PowerShell?
  • List operations in SharePoint Online using PnP PowerShell – complete code

See Also:

You may also like the following SharePoint PowerShell tutorials:

Download SharePoint Online & Office 365 Administration eBook

Buy the premium version of SharePoint Online & Office 365 administration eBook from here:



Buy SharePoint Online & Office 365 Administration eBook


 

Get the free demo PDF eBook from here:

FREE DOWNLOAD

Send download link to:

Subscribe to get exclusive content and recommendations every month. You can unsubscribe anytime.