Sao chép các mục danh sách giữa các danh sách SharePoint bằng PowerShell
Yêu cầu: Sao chép các mục danh sách giữa các danh sách SharePoint bằng PowerShell
Một trong các danh sách SharePoint của chúng tôi, có tên là “Tin tức”, đã phát triển quá mức và muốn lưu trữ nội dung của nó vào một danh sách khác theo định kỳ.
Giải pháp: Vì vậy, nhiệm vụ là sao chép các mục danh sách giữa các Danh sách SharePoint. Hãy đạt được điều đó bằng PowerShell.
Tập lệnh PowerShell để sao chép Mục danh sách:
Bạn có muốn sao chép các mục giữa hai danh sách SharePoint không? Có lẽ bạn có một danh sách các mục kiểm tra và bạn muốn di chuyển dữ liệu từ danh sách này sang danh sách khác mà không cần nhập tất cả dữ liệu theo cách thủ công. Bài viết này sẽ hướng dẫn bạn cách sao chép các mục giữa danh sách SharePoint thật dễ dàng khi sử dụng PowerShell.
#Configuration variables
$WebURL = "https://intranet.crescent.com"
$SourceListName = "News"
$TargetListName= "NewsArchive"
#Get Objects
$web = Get-SPWeb $WebURL
$SourceList = $web.Lists[$SourceListName]
$TargetList = $web.Lists[$TargetListName]
#Get all source items
$SourceColumns = $sourceList.Fields
$SourceItems = $SourceList.GetItems();
#Iterate through each item and add to target list
Foreach($SourceItem in $SourceItems)
{
$TargetItem = $TargetList.AddItem()
Foreach($column in $SourceColumns)
{
if($column.ReadOnlyField -eq $False -and $column.InternalName -ne "Attachments")
{
$TargetItem[$($column.InternalName)] = $sourceItem[$($column.InternalName)];
}
}
$TargetItem.Update();
}
Sao chép tệp đính kèm giữa các danh sách:
Nếu bạn muốn sao chép các mục danh sách có tệp đính kèm, hãy sử dụng tập lệnh này:
#Configuration variables
$WebURL = "https://intranet.crescent.com"
$SourceListName = "News"
$TargetListName= "NewsArchive"
#Get Objects
$web = Get-SPWeb $WebURL
$SourceList = $web.Lists[$SourceListName]
$TargetList = $web.Lists[$TargetListName]
#Get all source items
$SourceColumns = $sourceList.Fields
$SourceItems = $SourceList.GetItems();
#Iterate through each item and add to target list
Foreach($SourceItem in $SourceItems)
{
$TargetItem = $TargetList.AddItem()
Foreach($column in $SourceColumns)
{
if($column.ReadOnlyField -eq $False -and $column.InternalName -ne "Attachments")
{
$TargetItem[$($column.InternalName)] = $sourceItem[$($column.InternalName)];
}
}
$TargetItem.Update()
#Copy Attachments
Foreach($Attachment in $SourceItem.Attachments)
{
$spFile = $SourceList.ParentWeb.GetFile($SourceItem.Attachments.UrlPrefix + $Attachment)
$TargetItem.Attachments.Add($Attachment, $spFile.OpenBinary())
}
}
Nếu tên cột khác nhau thì sao?
$SourceItems = $SourceList.items
foreach ($SourceItem in $SourceItems)
{
write-host -foregroundcolor yellow Copying Item: $SourceItem["Title"]
$TargetItem = $TargetList.AddItem()
$TargetItem["Title"] = $SourceItem["Title-Column"]
$TargetItem["Another-Field"] = $SourceItem["Another-Field"]
$TargetItem.update()
}
