How to interact with PnPListItem ContentType

33 views Asked by At

If you run a command like this, it should pull some information about the ContentType of the List Items.

$x = Get-PnPListItem -List $list -PageSize 100 -Fields Title -IncludeContentType

Indeed it seems to have a property containing sub properties.

$x[0].ContentType | fl

However, Name and ID are both unset so this is of limited use as is. How can these values be loaded or accessed?

This seems to work fine if you target a single item or do not use the -fields param e.g.

(Get-PnPListItem -List $list -PageSize 1000 -IncludeContentType).ContentType.Name
1

There are 1 answers

0
Craig.C On

I was able to access via FieldValues

Get-PnPListItem -List $list  -PageSize 1000 -Fields Title, ContentTypeId | 
    ForEach-Object{
        $_.FieldValues["ContentTypeId"]
    }

However, I was unable to access the ContentType while the Fields was in use, even when explicitly trying to include and load these fields.

$context = Get-PnPContext
Get-PnPListItem -List $list  -PageSize 1000 -Fields Title, FileDirRef -IncludeContentType -Includes ContentType, ContentType.Name | 
    Select-Object -First 10 |
    ForEach-Object{
        $context.Load($_)
        $context.ExecuteQuery()
        $_.ContentType.Name
    }