如何使用SharePoint Online Management Shell添加SharePoint列表项?
SharePoint Online Management Shell 本身没有直接对应 Add-PnPListItem 的原生 cmdlet,以下是两种可行的实现方式:
方法1:在SPO Management Shell中兼容使用PnP PowerShell
如果允许在SPO Management Shell中安装PnP模块,这是最简便的方式,操作和你之前的习惯几乎一致:
- 安装PnP PowerShell模块(首次使用):
Install-Module -Name PnP.PowerShell -Force -AllowClobber
- 连接到目标站点:
Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/yoursite" -Interactive
- 添加列表项:
Add-PnPListItem -List "你的列表名称" -Values @{"标题"="新列表项";"自定义字段"="字段值"}
方法2:纯原生CSOM脚本实现
如果要完全依赖SPO Management Shell的原生能力,可使用CSOM编写脚本:
# 配置参数 $siteUrl = "https://yourtenant.sharepoint.com/sites/yoursite" $listName = "你的列表名称" $itemProperties = @{ "Title" = "新列表项标题" "CustomFieldInternalName" = "自定义字段值" # 替换为字段的内部名称 } # 获取SPO账号凭据 $credential = Get-Credential # 加载CSOM程序集(路径需匹配你的环境) Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll" Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll" # 初始化客户端上下文 $context = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl) $context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credential.UserName, $credential.Password) # 获取目标列表 $list = $context.Web.Lists.GetByTitle($listName) # 创建并配置新列表项 $itemCreateInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation $newItem = $list.AddItem($itemCreateInfo) foreach ($key in $itemProperties.Keys) { $newItem[$key] = $itemProperties[$key] } # 提交更改 $newItem.Update() $context.ExecuteQuery() Write-Host "列表项添加成功"
注意事项
- 方法2中,字段必须使用内部名称,而非显示名称,可通过列表设置中字段的URL查看内部名称
- 确保CSOM程序集的路径正确,若你的SharePoint安装路径不同,请自行调整路径
- 运行脚本的账号需拥有目标列表的编辑权限
内容的提问来源于stack exchange,提问作者Yamuna T
相关产品推荐
相关产品推荐

