You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何加载访问PnPListItem的ContentType属性?Name与ID未加载问题

解决PnP PowerShell获取列表项ContentType名称和ID的问题

问题原因

当使用Get-PnPListItem时同时指定-Fields和-IncludeContentType参数,PnP只会请求你指定的字段(此处为Title),虽然会返回ContentType对象,但不会自动加载它的Name和ID属性,导致这两个值为空。

解决方案

方案1:移除-Fields参数(允许获取所有字段时使用)

如果不需要严格限制只获取Title字段,直接去掉-Fields Title参数,-IncludeContentType会自动加载ContentType的完整属性:

$x = Get-PnPListItem -List $list -PageSize 100 -IncludeContentType
# 正常访问属性
$x[0].ContentType.Name
$x[0].ContentType.Id

方案2:在-Fields中添加ContentTypeId字段,通过列表匹配获取信息

如果必须指定-Fields参数,可添加ContentTypeId字段,之后通过列表的ContentTypes集合根据ID匹配对应的ContentType:

$x = Get-PnPListItem -List $list -PageSize 100 -Fields Title, ContentTypeId -IncludeContentType
# 遍历列表项获取ContentType名称和ID
foreach($item in $x){
    $contentType = $list.ContentTypes | Where-Object {$_.Id.StringValue -eq $item["ContentTypeId"]}
    Write-Host "ContentType名称: $($contentType.Name), ID: $($contentType.Id.StringValue)"
}

方案3:使用Load-PnPProperty手动加载ContentType属性

对已获取的列表项,手动加载ContentType的Name和Id属性:

$x = Get-PnPListItem -List $list -PageSize 100 -Fields Title -IncludeContentType
# 加载单个项的ContentType属性
Load-PnPProperty -ClientObject $x[0].ContentType -Properties Name, Id
# 访问属性
$x[0].ContentType.Name
$x[0].ContentType.Id

# 批量加载所有项的ContentType属性
foreach($item in $x){
    Load-PnPProperty -ClientObject $item.ContentType -Properties Name, Id
}
Invoke-PnPQuery # 执行批量加载请求
# 之后可访问所有项的ContentType属性

内容的提问来源于stack exchange,提问作者Craig.C

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 13:33:32