[玩转系统] SharePoint Online:使用 PowerShell 将列表项从一个列表复制到另一个列表
作者:精品下载站 日期:2024-12-14 21:06:49 浏览:13 分类:玩电脑
SharePoint Online:使用 PowerShell 将列表项从一个列表复制到另一个列表
要求: 将列表项复制到 SharePoint Online 中的另一个列表。
如何将列表项复制到 SharePoint Online 中的另一个列表?
您需要将列表项从一个列表复制到另一个列表吗?也许您需要将数据从旧列表迁移到新列表。或者也许您只需要快速轻松地在列表之间移动一些项目。无论如何,如果您正在寻找一种将项目从一个 SharePoint Online 列表复制到另一个的方法,我们将向您展示如何使用 PowerShell 在 SharePoint Online 列表之间复制列表项目。
使用快速编辑(数据表视图)在 SharePoint Online 的列表之间复制粘贴列表项。确保您有匹配的列,并且两个视图中的列顺序相同。
重要提示:在继续执行下面的任何脚本之前,请确保您已克隆现有列表(将列表另存为模板!)并创建一个新的列表实例(无数据)。这些脚本仅复制数据,但不创建列表!您还可以使用:如何从 SharePoint Online 中的现有列表创建新列表?
SharePoint Online:使用 PowerShell 将列表项复制到另一个列表
当您想要在 SharePoint Online 列表之间重复复制项目时,可以使用此 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"
Function Copy-ListItems()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $SourceListName,
[Parameter(Mandatory=$true)] [string] $TargetListName
)
Try {
#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Cred
#Get the Source List and Target Lists
$SourceList = $Ctx.Web.Lists.GetByTitle($SourceListName)
$TargetList = $Ctx.Web.Lists.GetByTitle($TargetListName)
#Get All Items from Source List
$SourceListItems = $SourceList.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
$Ctx.Load($SourceListItems)
$Ctx.ExecuteQuery()
#Get each column value from source list and add them to target
ForEach($SourceItem in $SourceListItems)
{
$NewItem =New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
$ListItem = $TargetList.AddItem($NewItem)
#Map each field from source list to target list - INTERNAL NAMES
$ListItem["Title"] = $SourceItem["Title"]
$ListItem["IsActive"] = $SourceItem["IsActive"]
$ListItem["ProjectStartDate"] = $SourceItem["ProjectStartDate"]
$ListItem["Department"] = $SourceItem["Department"]
$ListItem["Priority"] = $SourceItem["Priority"]
$ListItem.update()
}
$Ctx.ExecuteQuery()
write-host -f Green "Total List Items Copied from '$SourceListName' to '$TargetListName' : $($SourceListItems.count)"
}
Catch {
write-host -f Red "Error Copying List Items!" $_.Exception.Message
}
}
#Set Parameters
$SiteURL= "https://crescent.sharepoint.com/"
$SourceListName="Projects Template"
$TargetListName="Project Innovate"
#Call the function to copy list items
Copy-ListItems -siteURL $SiteURL -SourceListName $SourceListName -TargetListName $TargetListName
此脚本将所有映射的列值从源复制到目标列表。让我们增强脚本位,以自动将所有匹配的列从源列表复制到目标列表。
SharePoint Online:使用 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"
Function Copy-ListItems()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $SourceListName,
[Parameter(Mandatory=$true)] [string] $TargetListName
)
Try {
#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Cred
#Get the Source List and Target Lists
$SourceList = $Ctx.Web.Lists.GetByTitle($SourceListName)
$TargetList = $Ctx.Web.Lists.GetByTitle($TargetListName)
#Get All Items from Source List
$SourceListItems = $SourceList.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
$Ctx.Load($SourceListItems)
$Ctx.ExecuteQuery()
#Get All fields from Source List & Target List
$SourceListFields = $SourceList.Fields
$Ctx.Load($SourceListFields)
$TargetListFields = $TargetList.Fields
$Ctx.Load($TargetListFields)
$Ctx.ExecuteQuery()
#Get each column value from source list and add them to target
ForEach($SourceItem in $SourceListItems)
{
$NewItem =New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
$ListItem = $TargetList.AddItem($NewItem)
#Map each field from source list to target list
Foreach($SourceField in $SourceListFields)
{
#Skip Read only, hidden fields, content type and attachments
If((-Not ($SourceField.ReadOnlyField)) -and (-Not ($SourceField.Hidden)) -and ($SourceField.InternalName -ne "ContentType") -and ($SourceField.InternalName -ne "Attachments") )
{
$TargetField = $TargetListFields | where { $_.Internalname -eq $SourceField.Internalname}
if($TargetField -ne $null)
{
#Copy column value from source to target
$ListItem[$TargetField.InternalName] = $SourceItem[$SourceField.InternalName]
}
}
}
$ListItem.update()
$Ctx.ExecuteQuery()
}
write-host -f Green "Total List Items Copied from '$SourceListName' to '$TargetListName' : $($SourceListItems.count)"
}
Catch {
write-host -f Red "Error Copying List Items!" $_.Exception.Message
}
}
#Set Parameters
$SiteURL= "https://crescent.sharepoint.com/"
$SourceListName="Projects Template"
$TargetListName="Project Innovate"
#Call the function to copy list items
Copy-ListItems -siteURL $SiteURL -SourceListName $SourceListName -TargetListName $TargetListName
PowerShell 将带有附件的列表项从一个列表复制到另一个列表
让我们使用 PowerShell 将项目和附件从一个列表复制到 SharePoint Online 中的另一个列表:
#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"
Function Copy-ListItems()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $SourceListName,
[Parameter(Mandatory=$true)] [string] $TargetListName
)
Try {
#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Cred
#Get the Source List and Target Lists
$SourceList = $Ctx.Web.Lists.GetByTitle($SourceListName)
$TargetList = $Ctx.Web.Lists.GetByTitle($TargetListName)
#Get All Items from Source List
$SourceListItems = $SourceList.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
$Ctx.Load($SourceListItems)
$Ctx.ExecuteQuery()
Write-host "Total Number of List Items Found in the source:"$SourceListItems.count
#Get All fields from Source List & Target List
$SourceListFields = $SourceList.Fields
$Ctx.Load($SourceListFields)
$TargetListFields = $TargetList.Fields
$Ctx.Load($TargetListFields)
$Ctx.ExecuteQuery()
#Get each column value from source list and add them to target
ForEach($SourceItem in $SourceListItems)
{
$NewItem =New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
$ListItem = $TargetList.AddItem($NewItem)
#Map each field from source list to target list
Foreach($SourceField in $SourceListFields)
{
#Skip Read only, hidden fields, content type and attachments
If((-Not ($SourceField.ReadOnlyField)) -and (-Not ($SourceField.Hidden)) -and ($SourceField.InternalName -ne "ContentType") -and ($SourceField.InternalName -ne "Attachments") )
{
$TargetField = $TargetListFields | where { $_.Internalname -eq $SourceField.Internalname}
if($TargetField -ne $null)
{
#Copy column value from source to target
$ListItem[$TargetField.InternalName] = $SourceItem[$SourceField.InternalName]
}
}
}
$ListItem.update()
$Ctx.ExecuteQuery()
#Copy attachments
$AttachmentsColl = $SourceItem.AttachmentFiles
$Ctx.Load($AttachmentsColl)
$Ctx.ExecuteQuery()
ForEach($Attachment in $AttachmentsColl)
{
Write-host "Copying attachment '$($Attachment.FileName)' from Item ID '$($SourceItem.ID)'"
#Get attachment File from Source List Item
$File = $Ctx.Web.GetFileByServerRelativeUrl($Attachment.ServerRelativeUrl)
$Ctx.Load($File)
$Ctx.ExecuteQuery()
#Get the Source File Content
$FileContent = $File.OpenBinaryStream()
$Ctx.ExecuteQuery()
$Buffer = New-Object Byte[]($File.length)
$BytesRead = $FileContent.Value.Read($Buffer, 0, $Buffer.Length)
$MemoryStream = New-Object -TypeName System.IO.MemoryStream(,$Buffer)
#Add Attachment to Target List Item
$AttachmentCreation = New-Object Microsoft.SharePoint.Client.AttachmentCreationInformation
$AttachmentCreation.ContentStream = $MemoryStream
$AttachmentCreation.FileName = $Attachment.FileName
[void]$ListItem.AttachmentFiles.Add($AttachmentCreation)
$Ctx.ExecuteQuery()
$MemoryStream.Close()
}
Write-host "Copied Item to the Target List:"$SourceItem.id -f Yellow
}
write-host -f Green "Total List Items Copied from '$SourceListName' to '$TargetListName' : $($SourceListItems.count)"
}
Catch {
write-host -f Red "Error Copying List Items!" $_.Exception.Message
}
}
#Set Parameters
$SiteURL= "https://crescent.sharepoint.com/"
$SourceListName="Projects"
$TargetListName="Project Temp"
#Call the function to copy list items
Copy-ListItems -siteURL $SiteURL -SourceListName $SourceListName -TargetListName $TargetListName
这是 SharePoint 本地复制列表项的另一篇文章:使用 PowerShell 将 SharePoint 列表项复制到另一个列表
PnP PowerShell 在 SharePoint Online 列表之间复制项目
只要列表字段不是人员选择器、查找、托管元数据或超链接,上述脚本就可以正常工作!因此,这是处理所有复杂字段并复制列表项的脚本:
#Function to copy attachments between list items
Function Copy-SPOAttachments($SourceItem, $TargetItem)
{
Try {
#Get All Attachments from Source
$Attachments = Get-PnPProperty -ClientObject $SourceItem -Property "AttachmentFiles"
$Attachments | ForEach-Object {
#Download the Attachment to Temp
$File = Get-PnPFile -Url $_.ServerRelativeUrl -FileName $_.FileName -Path $env:TEMP -AsFile -force
#Add Attachment to Target List Item
$FileStream = New-Object IO.FileStream(($env:TEMP+"\"+$_.FileName),[System.IO.FileMode]::Open)
$AttachmentInfo = New-Object -TypeName Microsoft.SharePoint.Client.AttachmentCreationInformation
$AttachmentInfo.FileName = $_.FileName
$AttachmentInfo.ContentStream = $FileStream
$AttachFile = $TargetItem.AttachmentFiles.add($AttachmentInfo)
$Context.ExecuteQuery()
#Delete the Temporary File
Remove-Item -Path $env:TEMP$($_.FileName) -Force
}
}
Catch {
write-host -f Red "Error Copying Attachments:" $_.Exception.Message
}
}
#Function to list items from one list to another
Function Copy-SPOListItems()
{
param
(
[Parameter(Mandatory=$true)] [string] $SourceListName,
[Parameter(Mandatory=$true)] [string] $TargetListName
)
Try {
#Get All Items from the Source List in batches
Write-Progress -Activity "Reading Source..." -Status "Getting Items from Source List. Please wait..."
$SourceListItems = Get-PnPListItem -List $SourceListName -PageSize 500
$SourceListItemsCount= $SourceListItems.count
Write-host "Total Number of Items Found:"$SourceListItemsCount
#Get fields to Update from the Source List - Skip Read only, hidden fields, content type and attachments
$SourceListFields = Get-PnPField -List $SourceListName | Where { (-Not ($_.ReadOnlyField)) -and (-Not ($_.Hidden)) -and ($_.InternalName -ne "ContentType") -and ($_.InternalName -ne "Attachments") }
#Loop through each item in the source and Get column values, add them to target
[int]$Counter = 1
ForEach($SourceItem in $SourceListItems)
{
$ItemValue = @{}
#Map each field from source list to target list
Foreach($SourceField in $SourceListFields)
{
#Check if the Field value is not Null
If($SourceItem[$SourceField.InternalName] -ne $Null)
{
#Handle Special Fields
$FieldType = $SourceField.TypeAsString
If($FieldType -eq "User" -or $FieldType -eq "UserMulti" -or $FieldType -eq "Lookup" -or $FieldType -eq "LookupMulti") #People Picker or Lookup Field
{
$LookupIDs = $SourceItem[$SourceField.InternalName] | ForEach-Object { $_.LookupID.ToString()}
$ItemValue.add($SourceField.InternalName,$LookupIDs)
}
ElseIf($FieldType -eq "URL") #Hyperlink
{
$URL = $SourceItem[$SourceField.InternalName].URL
$Description = $SourceItem[$SourceField.InternalName].Description
$ItemValue.add($SourceField.InternalName,"$URL, $Description")
}
ElseIf($FieldType -eq "TaxonomyFieldType" -or $FieldType -eq "TaxonomyFieldTypeMulti") #MMS
{
$TermGUIDs = $SourceItem[$SourceField.InternalName] | ForEach-Object { $_.TermGuid.ToString()}
$ItemValue.add($SourceField.InternalName,$TermGUIDs)
}
Else
{
#Get Source Field Value and add to Hashtable
$ItemValue.add($SourceField.InternalName,$SourceItem[$SourceField.InternalName])
}
}
}
Write-Progress -Activity "Copying List Items:" -Status "Copying Item ID '$($SourceItem.Id)' from Source List ($($Counter) of $($SourceListItemsCount))" -PercentComplete (($Counter / $SourceListItemsCount) * 100)
#Copy column value from source to target
$NewItem = Add-PnPListItem -List $TargetListName -Values $ItemValue
#Copy Attachments
Copy-SPOAttachments -SourceItem $SourceItem -TargetItem $NewItem
Write-Host "Copied Item ID from Source to Target List:$($SourceItem.Id) ($($Counter) of $($SourceListItemsCount))"
$Counter++
}
}
Catch {
Write-host -f Red "Error:" $_.Exception.Message
}
}
#Connect to PnP Online
Connect-PnPOnline -Url "https://crescent.sharepoint.com/sites/marketing/" -Credentials (Get-Credential)
$Context = Get-PnPContext
#Call the Function to Copy List Items between Lists
Copy-SPOListItems -SourceListName "Projects" -TargetListName "ProjectsArchive"
如果要在网站之间复制列表项,请使用:如何使用 PowerShell 在 SharePoint Online 中的网站集之间复制列表项?
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag