SharePoint Online:使用 PowerShell 更新列表项
要求:SharePoint Online PowerShell 来编辑列表项。
使用 PowerShell CSOM 脚本更新 SharePoint Online 中的列表项
如果您想要更新 SharePoint Online 中的列表项,但不想每次都通过 Web 用户界面 - 因为更新频繁且耗时,有一种方法可以使用 PowerShell 自动执行此过程。本博文将向您展示如何使用 PowerShell 更新 SharePoint Online 中的列表项。
以下是 SharePoint Online PowerShell 更新列表项的示例:
#Load SharePoint CSOM Assemblies
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"
#Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/"
$ListName="Projects"
$UserName="admin@crescent.com"
$Password ="Password goes here"
#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
#Set up the context
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Context.Credentials = $credentials
try{
#Filter and Get the List Items using CAML
$List = $Context.web.Lists.GetByTitle($ListName)
#Get List Item by ID
$ListItem = $List.GetItemById(1)
#Update List Item title
$ListItem["Title"] = "Project Darwin"
$ListItem.Update()
$Context.ExecuteQuery()
write-host "Item Updated!" -foregroundcolor Green
}
catch{
write-host "$($_.Exception.Message)" -foregroundcolor red
}
这将更新给定列表的 ID 为 1 的项目。您还可以使用 $ListItem.SystemUpdate() 方法来保留元数据(例如上次修改的元数据)并保持版本完整。
上面的脚本更新特定的列表项。如果您想更新列表中的所有项目怎么办?
PowerShell 更新 SharePoint Online 中的所有列表项:
可以使用 PowerShell 更新 SharePoint Online 中的列表项。这可以通过检索列表项然后更新其属性来完成。以下是用于更新 SharePoint Online 中的列表项的 PowerShell:
#Load SharePoint CSOM Assemblies
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"
#Variables
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
Try {
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
$Web = $Ctx.web
#Get the List
$List = $Ctx.Web.Lists.GetByTitle($ListName)
$Ctx.Load($List)
$Ctx.ExecuteQuery()
#Get All List items
$ListItemsCAML = New-Object Microsoft.SharePoint.Client.CamlQuery
$ListItemsCAML.ViewXml = "<View Scope='RecursiveAll'></View>"
$ListItems = $List.GetItems($ListItemsCAML)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()
Write-host "Total Items Found:"$List.ItemCount
#Iterate through each item and update
Foreach ($ListItem in $ListItems)
{
#Set New value for List column
$ListItem["Reference"] = $ListItem["ID"]
$ListItem.Update()
$Ctx.ExecuteQuery()
}
Write-host "All Items in the List: $ListName Updated Successfully!" -ForegroundColor Green
}
Catch {
write-host -f Red "Error Updating List Items!" $_.Exception.Message
}
SharePoint Online:PnP PowerShell 更新列表项
PowerShell 是一种功能强大的脚本语言,可用于自动执行各种任务,包括对 SharePoint 列表项的更新。为了更新列表项,您必须首先使用 PowerShell 连接到您的 SharePoint 网站。连接后,您可以使用 Get-PnPListItem cmdlet 检索要更新的列表项。要更新该项目,只需使用 Set-PnPListItem cmdlet,指定列表项的 ID 以及要更新的字段的新值。此 cmdlet 会自动将您的更改保存回 SharePoint。
下面介绍了如何使用 Set-PnPListItem cmdlet 在 SharePoint Online 中使用 PnP PowerShell 更新列表项。
#Config Variables
$SiteURL = "https://crescent.sharepoint.com"
$ListName ="Projects"
$ListItemID ="10"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
#Get List Item to Update
$ListItem = Get-PnPListItem -List $ListName -Id $ListItemID -ErrorAction Stop
#Update List Item - Internal Names of the columns : Value
Set-PnPListItem -List $ListName -Identity $ListItem -Values @{"ProjectName" = "SharePoint 2016 Migration"; "ProjectID"="Abj-IT-3021"}
同样,我们可以使用查询参数来过滤并获取列表项:
#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName ="Projects"
$ListItemTitle ="SharePoint 2016 Migration"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
#Get List Item to Update by Query
$ListItem = Get-PnPListItem -List $ListName -Query "<View><Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>$ListItemTitle</Value></Eq></Where></Query></View>" -PageSize 1 -ErrorAction Stop
#Update List Item - Internal Names of the columns : Value
Set-PnPListItem -List $ListName -Identity $ListItem -Values @{"Title" = "SharePoint 2016 Migration V2"; "ProjectID"="Abj-IT-3025"}
如果要从 CSV 文件批量更新列表项,请使用:SharePoint Online:使用 PowerShell 从 CSV 文件更新列表项
如何更新查找、人员选择器、托管元数据、超链接等字段?
上述脚本适用于简单的字段类型,例如单行文本、选择、是/否等。但是,我们必须针对其他字段类型(例如托管元数据、超链接、查找、人员选取器等)以不同的方式处理它们。以下是我的帖子可以提供帮助:
- SharePoint Online:PowerShell 更新查找字段值
- SharePoint Online:使用 PowerShell 更新超链接字段值
- SharePoint Online:PowerShell 更新查找字段值
- SharePoint Online:使用 PowerShell 更新托管元数据字段值
- SharePoint Online:使用 PowerShell 更新人员或组字段值
- SharePoint Online:使用 PowerShell 更新选择字段值
- SharePoint Online:使用 PowerShell 更新“是/否(复选框)”字段值
- SharePoint Online:PowerShell 更新日期字段值
