How do I get fieldvalues from a content Type Uisng PnP PowerShell

Regular Contributor

Hi

 

Get-PnPContentTypes only brings back a subset of all of the site columns in a contenttype.

 

So I have a loop so :-

 

$CTypes = Get-PnPContentType
foreach($contentType in $CTypes)
{
 $fieldInUse = $contentType.FieldLinks | Where {$_Name -eq $columnInternalName }
 if($fieldInUse -ne $null)
 {
  Write-Host "Found The Column in ContentType: " $contentType.Name -ForegroundColor DarkGreen
  
 }

}

 

I get an error :-

 

An error occurred while enumerating through a collection: The collection has not been initialized. It has not been
requested or the request has not been executed. It may need to be explicitly requested..
At line:1 char:1
+ $contentType.FieldLinks
+ ~~~~~~~~~~~~~~~~~~~~~~~

 

How do I get FieldLinks loaded via Get-PnPContentType ?

 

Thanks

 

Nigel

2 Replies

Hi,

 

you need to load collections via the context, so something like:

 

$CTypes = Get-PnPContentType
foreach($contentType in $CTypes)
{

$ctx = Get-PnPContext

$ctx.Load($contentType.FieldLinks)

$ctx.ExecuteQuery()

$fieldInUse = $contentType.FieldLinks | Where {$_Name -eq $columnInternalName }

 

# etc.

 

Thanks

 

Nigel