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:
#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 its 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

SharePoint Online: Get Columns from Content Type using PowerShell SharePoint Online: Get Columns from Content Type using PowerShell Reviewed by Salaudeen Rajack on July 13, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.