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?
Navigate to list settings, under "Columns" section, you'll find all fields of the particular list.
sharepoint Online get all list fields

SharePoint Online: PowerShell to Get List Fields
Here is the SharePoint Online PowerShell to get list columns: Lets 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
Lets get all list fields 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 from List 
Here is the PowerShell to get all fields from SharePoint Online
#Config Variables
$SiteURL = "https://crescenttech.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
}
sharepoint online powershell get list fields
SharePoint Online: Get All List Fields using PowerShell SharePoint Online: Get All List Fields using PowerShell Reviewed by Salaudeen Rajack on February 03, 2017 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.