SharePoint Online: Get All List Fields using PowerShell
Requirement: SharePoint Online PowerShell to Get All List Fields.
How to Get List Fields in SharePoint Online?
Do you ever need to get list columns from a SharePoint Online List? Maybe you wanted to know the internal name of a SharePoint Online column or some other properties? This blog post will show you how to use PowerShell and a web browser interface to get the internal name of a SharePoint Online list column.
You can get list columns in SharePoint Online by navigating to the list settings under the “Columns” section, and you’ll find all fields of the particular list.
SharePoint Online: PowerShell to Get List Fields
Here is the SharePoint Online PowerShell to get list columns: Let’s use PowerShell to get list field properties and export to a CSV file, such as:
- List field name
- Field Type
- Field ID
- Field Internal Name
- Field Schema XML
- and Field description
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Get the List and list fields
$List = $Ctx.Web.Lists.GetByTitle($ListName)
$Ctx.Load($List)
#sharepoint online powershell get list columns
$Ctx.Load($List.Fields)
$Ctx.ExecuteQuery()
#Iterate through each field in the list
Foreach ($Field in $List.Fields)
{
#Skip System Fields
if(($Field.ReadOnlyField -eq $False) -and ($Field.Hidden -eq $False))
{
#get internal name of sharepoint online list column powershell
Write-Host $Field.Title : $Field.InternalName
}
}
SharePoint Online: Get All Fields of a List and Export to CSV
Let’s get all list fields from a SharePoint Online list and export them to a CSV file.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#function to Get all fields from a SharePoint Online list or library
Function Get-SPOListFields()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $ListName
)
Try {
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Get the List
$List = $Ctx.Web.Lists.GetByTitle($ListName)
$Ctx.Load($List)
$Ctx.Load($List.Fields)
$Ctx.ExecuteQuery()
#Array to hold result
$FieldData = @()
#Iterate through each field in the list
Foreach ($Field in $List.Fields)
{
Write-Host $Field.Title `t $Field.Description `t $Field.InternalName `t $Field.Id `t $Field.TypeDisplayName
#Send Data to object array
$FieldData += New-Object PSObject -Property @{
'Field Title' = $Field.Title
'Field Description' = $Field.Description
'Field ID' = $Field.Id
'Internal Name' = $Field.InternalName
'Type' = $Field.TypeDisplayName
'Schema' = $Field.SchemaXML
}
}
Return $FieldData
}
Catch {
write-host -f Red "Error Getting Fields from List!" $_.Exception.Message
}
}
#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$CSVLocation ="C:\Temp\ListFields.csv"
#Call the function to get all list fields
Get-SPOListFields -SiteURL $SiteURL -ListName $ListName | Export-Csv $CSVLocation -NoTypeInformation
PnP PowerShell to Get All Fields and Internal Names from a List
Do you need to know the column’s internal name in a SharePoint Online list? If so, Here is the PowerShell to get all fields along with field name, Internal name, and GUID from a SharePoint Online list.
#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName ="Projects"
#Get Credentials to connect
$Cred = Get-Credential
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials $Cred
#Get All Columns from List
Get-PnPField -List $ListName
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
Export All Fields from All Lists in a SharePoint Online Site
For backup or auditing purpose, you want to extract all fields information from every list and library in a site? Sure! Here is the PnP PowerShell way to do that!
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/retail"
$CSVFile = "C:\Temp\ListFields.csv"
$ListFields = @()
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Get All lists from the site - Exclude Hidden
$Lists = Get-PnPList | Where {$_.Hidden -eq $false}
ForEach($List in $Lists)
{
Write-host -f Yellow "Processing List:"$List.Title
#Get All Fields from the List
$Fields = Get-PnPField -List $List | Where {$_.Hidden -eq $false}
ForEach($Field in $Fields)
{
$ListFields += [PSCustomObject][ordered]@{
ListName = $List.Title
FieldTitle = $Field.Title
InternalName = $Field.InternalName
ID = $Field.ID
SchemaXml = $Field.SchemaXml
Required = $Field.Required
TypeAsString = $Field.TypeAsString
}
}
}
#Export data to CSV
$ListFields | Export-Csv -Path $CSVFile -NoTypeInformation
}
Catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
Here is the sample output from the above script:
By retrieving all fields, you can obtain information about your SharePoint Online list columns and perform various actions, such as adding or removing fields.
Hi,
I need a script to export all the list and library name, Column Internal Name, Column Display Name, Column Type from a site collection to excel without specifying a list/library name, could you please help?
Thanks!
Sure! Use the last script under “Export All Fields from All Lists in a SharePoint Online Site” heading!