当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] SharePoint Online:使用 PowerShell 批量更新大列表中的所有项目

作者:精品下载站 日期:2024-12-14 15:03:24 浏览:16 分类:玩电脑

SharePoint Online:使用 PowerShell 批量更新大列表中的所有项目


要求:使用 PowerShell 批量更新 SharePoint Online 列表项。

[玩转系统] SharePoint Online:使用 PowerShell 批量更新大列表中的所有项目

如何使用PowerShell批量更新大量列表项?

您是否正在寻找一种快速更新 SharePoint Online 列表中所有项目的方法?也许需要更新列表中所有项目的列值。本文将向您展示如何使用 PowerShell 脚本快速批量更新 SharePoint Online 中的所有列表项。

虽然可以使用我的其他帖子中的方法更新包含

嗯,大列表必须分批更新。这是我的 PowerShell 脚本,用于批量更新大型列表:此 PowerShell 脚本更新“文件名”中的“标题”字段值。


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  
#Parameters
$SiteURL = "https://Crescent.sharepoint.com"
$ListName = "Documents"
$UserName = "[email protected]"
$Password = "Password123456"
$SecurePassword= $Password | ConvertTo-SecureString -AsPlainText -Force
$BatchSize =100
 
#Setup the Context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
 
#Get the List
$List = $Ctx.Web.Lists.GetByTitle($ListName)
$Ctx.Load($List)
$Ctx.ExecuteQuery()

#sharepoint online powershell caml batch update
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "@
    <View Scope='RecursiveAll'> 
        <Query> 
             <OrderBy><FieldRef Name='ID' Ascending='TRUE'/></OrderBy>
        </Query>
        <RowLimit>$BatchSize</RowLimit>
    </View>"

#Get List Items in Batches
Do
{
    #Get List Items 
    $ListItems = $List.GetItems($Query)
    $Ctx.Load($ListItems)
    $Ctx.ExecuteQuery()
    $ListItems.Count

    #Update Postion of the ListItemCollectionPosition
    $Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition

    If ($ListItems.Count -eq 0) { Break }
    
    #Update List Item
    ForEach($Item in $ListItems)
    {
        #Update List Item Title as File Name
        $Item["Title"]= $Item["FileLeafRef"]
        $Item.Update()
    }
    $Ctx.ExecuteQuery()
}While ($Query.ListItemCollectionPosition -ne $null)

如果要从 CSV 文件批量更新多个项目,请使用:使用 PowerShell 从 CSV 更新 SharePoint Online 列表

PnP PowerShell 更新 SharePoint Online 列表中的所有项目

使用 PnP PowerShell 更新多个列表项比上面的 CSOM 脚本相对容易(这就是 PnP 的力量!)。


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/PMO"
$ListName = "Projects"

#Connect to SharePoint Online site
Connect-PnPOnline $SiteURL -Interactive

#Get all Items from the list
$ListItems = Get-PnPListItem -List $ListName -PageSize 2000

#Update all list Items
ForEach($Item in $ListItems)
{
    #Update List Item Title as its ID value
    Set-PnPListItem -List $ListName -Identity $Item.Id -Values @{"Title"= $Item.Id} | Out-Null
    Write-host "Updated Item:"$Item.ID    
}

您还可以使用 PnP PowerShell 的 New-PnPBatch 方法更快地批量更新列表项!以下是参考:SharePoint Online:在 PowerShell 中使用 New-PnPBatch 执行批量操作

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯