SharePoint Online: Get Columns from Content Type using PowerShell

Requirement: PowerShell to Get Content Type Fields in SharePoint Online.

PowerShell to Get Columns from Content Type in SharePoint Online:

Have you ever needed to get a list of all fields in your SharePoint Online content type? Well, you can get all fields of the content type from the content type settings page.

Get Columns from Content Type in SharePoint Online using PowerShell

How about exporting all the metadata for a particular content type? PowerShell to the rescue!

#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 Get-SPOContentTypeFields()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ContentTypeName
    )
 
    Try {
        #Get Credentials to connect
        $Cred= Get-Credential
 
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
     
        #Get the web
        $Web = $Ctx.Web
        $Ctx.Load($Web)
        $Ctx.ExecuteQuery()

        #Get the content type from web
        $ContentTypeColl = $Web.ContentTypes
        $Ctx.Load($ContentTypeColl)
        $Ctx.ExecuteQuery()
 
        #Get the content type
        $CType = $ContentTypeColl | Where {$_.Name -eq $ContentTypeName}
        If($CType -ne $Null)
        {
            $Ctx.Load($CType.FieldLinks)
            $Ctx.ExecuteQuery()

            #Get columns from the content type
            ForEach($FieldLink in $CType.FieldLinks)
            {           
                Write-Host -f Green $FieldLink.Name
            } 
        }
        else
        {
            Write-host "Content Type '$ContentTypeName' doesn't exist!'" -f Yellow
            Return 
        }
   }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
}
 
#Set parameter values
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
$ContentTypeName="Announcement"
 
#Call the function
Get-SPOContentTypeFields -SiteURL $SiteURL -ContentTypeName $ContentTypeName

SharePoint Online: PowerShell to Get Content Type Fields from a List

Here is my PowerShell to get a list Content Type and their columns, loop through them, and output a CSV file with the following data:

  • Column Title
  • Column Internal Name
  • Column ID
  • Column Group
  • Column 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"
 
Function Get-SPOListCTypeFields()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $ContentTypeName,
        [Parameter(Mandatory=$true)] [string] $CSVPath
    )
 
    Try {
        #Get Credentials to connect
        $Cred= Get-Credential
 
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
     
        #Get content types from web
        $ContentTypes = $Ctx.Web.ContentTypes
        $Ctx.Load($ContentTypes)
        $Ctx.ExecuteQuery()
 
        #Get the content type
        $CType = $ContentTypes | Where {$_.Name -eq $ContentTypeName}
        If($CType -ne $Null)
        {
            #Get Columns from the content type
            $Ctx.Load($CType.Fields)
            $Ctx.ExecuteQuery()
 
            $ResultArray = @()
            #Loop through the Fields in the Content Type
            ForEach ($Field in $CType.Fields)
            {
                #Create a new custom object to hold our row of data with property names:
                $Result = New-Object PSObject
                $Result | Add-Member -MemberType NoteProperty -Name "Title" -Value $Field.Title
                $Result | Add-Member -MemberType NoteProperty -Name "Internal Name" -Value $Field.InternalName
                $Result | Add-Member -MemberType NoteProperty -Name "ID" -Value $Field.Id
                $Result | Add-Member -MemberType NoteProperty -Name "Group" -Value $Field.Group
                $Result | Add-Member -MemberType NoteProperty -Name "Description" -Value $Field.Description
 
                #Add the object to array
                $ResultArray += $Result
            }
            $ResultArray | Format-table -AutoSize
            # Export the results to CSV
            $ResultArray | Export-Csv $CSVPath -NoTypeInformation -force

            Write-host "Content Type Columns Exported to CSV successfully!'" -f Green
        }
        else
        {
            Write-host "Content Type '$ContentTypeName' doesn't exist!'" -f Yellow
        }
   }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
}
 
#Set parameter values
$SiteURL ="https://crescent.sharepoint.com/sites/marketing"
$ListName ="News"
$ContentTypeName="Announcement"
$CSVPath = "C:\Temp\CtypeFields.csv"

#Call the function
Get-SPOListCTypeFields -SiteURL $SiteURL -ListName $ListName -ContentTypeName $ContentTypeName -CSVPath $CSVPath

PnP PowerShell to Get All Fields from a Content type

Let me show you how to use PowerShell to get fields from a content type.

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ContentTypeID ="0x0104004A217DA260E04940AC9DB4A010797423"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
 
#Get the site content type
$ContentType = Get-PnPContentType -Identity $ContentTypeID
 
#Get All Fields from the Content Type
$ContentTypeFields = Get-PnPProperty -ClientObject $ContentType -Property Fields

#Get Field Title and ID
$ContentTypeFields | Select Title, ID

Similarly, to get all fields from a list content type, use:

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ListName = "Projects"
$ContentTypeName ="Crescent Project V2"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
 
#Get the List content type
$ContentType = Get-PnPContentType -Identity $ContentTypeName -List $ListName
 
#Get All Fields from the Content Type
$ContentTypeFields = Get-PnPProperty -ClientObject $ContentType -Property Fields

#Get Field Title, Internal Name and ID
$ContentTypeFields | Select Title, InternalName, ID

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

Leave a Reply