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

%3CLINGO-SUB%20id%3D%22lingo-sub-181082%22%20slang%3D%22en-US%22%3EHow%20do%20I%20get%20fieldvalues%20from%20a%20content%20Type%20Uisng%20PnP%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-181082%22%20slang%3D%22en-US%22%3E%3CP%3EHi%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EGet-PnPContentTypes%20only%20brings%20back%20a%20subset%20of%20all%20of%20the%20site%20columns%20in%20a%20contenttype.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESo%20I%20have%20a%20loop%20so%20%3A-%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24CTypes%20%3D%20Get-PnPContentType%3CBR%20%2F%3Eforeach(%24contentType%20in%20%24CTypes)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%26nbsp%3B%24fieldInUse%20%3D%20%24contentType.FieldLinks%20%7C%20Where%20%7B%24_Name%20-eq%20%24columnInternalName%20%7D%3CBR%20%2F%3E%26nbsp%3Bif(%24fieldInUse%20-ne%20%24null)%3CBR%20%2F%3E%26nbsp%3B%7B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3BWrite-Host%20%22Found%20The%20Column%20in%20ContentType%3A%20%22%20%24contentType.Name%20-ForegroundColor%20DarkGreen%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%3CBR%20%2F%3E%26nbsp%3B%7D%3C%2FP%3E%3CP%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20get%20an%20error%20%3A-%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAn%20error%20occurred%20while%20enumerating%20through%20a%20collection%3A%20The%20collection%20has%20not%20been%20initialized.%20It%20has%20not%20been%3CBR%20%2F%3Erequested%20or%20the%20request%20has%20not%20been%20executed.%20It%20may%20need%20to%20be%20explicitly%20requested..%3CBR%20%2F%3EAt%20line%3A1%20char%3A1%3CBR%20%2F%3E%2B%20%24contentType.FieldLinks%3CBR%20%2F%3E%2B%20~~~~~~~~~~~~~~~~~~~~~~~%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHow%20do%20I%20get%20FieldLinks%20loaded%20via%20Get-PnPContentType%20%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENigel%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-181082%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPnP%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-191928%22%20slang%3D%22en-US%22%3ERe%3A%20How%20do%20I%20get%20fieldvalues%20from%20a%20content%20Type%20Uisng%20PnP%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-191928%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%20Nigel%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-190902%22%20slang%3D%22en-US%22%3ERe%3A%20How%20do%20I%20get%20fieldvalues%20from%20a%20content%20Type%20Uisng%20PnP%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-190902%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eyou%20need%20to%20load%20collections%20via%20the%20context%2C%20so%20something%20like%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3E%24CTypes%20%3D%20Get-PnPContentType%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3Eforeach(%24contentType%20in%20%24CTypes)%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%7B%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3E%24ctx%20%3D%20Get-PnPContext%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3E%24ctx.Load(%24contentType.FieldLinks)%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3E%24ctx.ExecuteQuery()%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3E%24fieldInUse%20%3D%20%24contentType.FieldLinks%20%7C%20Where%20%7B%24_Name%20-eq%20%24columnInternalName%20%7D%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23%26nbsp%3Betc.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENigel%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
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
Highlighted

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

Highlighted