首页 > 解决方案 > 带有 Pnp.PowerShell 的新 ListItem

问题描述

我正在尝试使用 PowerShell 脚本中的 Pnp.PowerShell 模块在 SharePoint Online 列表中创建一个新列表项。这是我的代码和输出:

## ... urls and security creds removed... 
Connect-PnPOnline -Url $km_site -ClientId $clientid -ClientSecret $secret -WarningAction Ignore
[Microsoft.SharePoint.Client.List]$projects_list = Get-PnPList "Projects"
Write-Host $projects_list.Title -ForegroundColor Green
$lci = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation -ArgumentList @()
[Microsoft.SharePoint.Client.ListItem]$new_item = $projects_list.AddItem($lci)
Write-Host $new_item.Id -ForegroundColor Cyan
$new_item["Title"] = $title
$new_item["Project_x0020_Code"] = $code
$new_item["_ExtendedDescription"] = $desc
$new_item["ContentTypeId"] = "0x010097F10B16E4516A4E80FC5C8FABF9BAC400AEEB05A0E486404FA588439EADC25541"
# Write-Host $new_item.FieldValues -ForegroundColor Cyan
$new_item.Update()
# $projects_list.Update()
Write-Host "Created!!" -ForegroundColor Yellow
Write-Host $new_item.Id -ForegroundColor Yellow

输出:

Projects
-1
Created!!
-1

该代码与其他在线示例中的代码相匹配,但未创建列表项,但不会引发错误。

在我的列表中,标题和项目代码是必需的,并且我使用的是我在管理门户中创建的自定义内容类型,然后包含在“项目”列表中。

标签: powershellsharepoint-online

解决方案


这是有效的解决方案。

Connect-PnPOnline -Url $km_site -ClientId $clientid -ClientSecret $secret -WarningAction Ignore

[Microsoft.SharePoint.Client.listItem]$list_item = Add-PnPListItem -List "Projects" -ContentType "Project Info" -Values @{"Title" = $title; "_ExtendedDescription"=$desc; "Project_x0020_Code"=$code}

推荐阅读