powershell - 带有 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
该代码与其他在线示例中的代码相匹配,但未创建列表项,但不会引发错误。
在我的列表中,标题和项目代码是必需的,并且我使用的是我在管理门户中创建的自定义内容类型,然后包含在“项目”列表中。
解决方案
这是有效的解决方案。
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}
推荐阅读
- php - 想要回显“已发送电子邮件!” 提交表单后在特定的 div 中
- c - execv() 上面的行不运行?
- vba - ClosedXML - 获取应用的条件格式样式
- apache-kafka - 我们如何流式传输数据管道以将数据从雪花传输到 kafka?
- c - 在函数中将结构作为参数传递
- django-models - ForeignKey 以脆的形式保存数据
- flutter - 单个颤动屏幕上的两个列表 GridViewBuilder 和 ListViewBuilder
- laravel - 如何在没有 SPA 或图形界面的情况下,仅通过 POSTMAN 访问 Laravel Sanctum 的 API 路由?
- firebase - Firebase 安全规则使用给定的电子邮件写入数据库
- sql - 基于另一列的子类别条目 - SQL