簡體   English   中英

使用 Pnp.PowerShell 新建 ListItem

[英]New ListItem with Pnp.PowerShell

我正在嘗試使用 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}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM