-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy List Items.ps1
62 lines (48 loc) · 3.53 KB
/
Copy List Items.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Script to copy list items iteratively between lists on a site
# Configuration variables
$WebURL = "http://XXXXXXXXXXXXX"
$SourceListName = "REQUEST FOR INFORMATION 2019"
$TargetListName = "REQUEST FOR INFORMATION"
### DO NOT TOUCH THE CODE BELOW ###
# Required if not run in SharePoint Management Shell
if($ver.Version.Major -gt 1)
{
$Host.Runspace.ThreadOptions = "ReuseThread"
}
If((Get-PSSnapin -Name Microsoft.Sharepoint.Powershell -ErrorAction SilentlyContinue) -eq $null)
{
Add-PSSnapin Microsoft.Sharepoint.Powershell
}
# Get objects
$Web = Get-SPWeb $WebURL
$SourceList = $Web.Lists[$SourceListName]
$TargetList = $Web.Lists[$TargetListName]
# Get all source items
$SourceColumns = $SourceList.Fields
$SourceItems = $SourceList.GetItems();
$StartTime = Get-Date -Format o
Write-Output "Process started at: $StartTime"
# Iterate through each item and add to target target
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
If ($SourceItem.Attachments -eq $null) { continue;}
Foreach($Attachment in $SourceItem.Attachments)
{
$SpFile = $SourceList.ParentWeb.GetFile($SourceItem.Attachments.UrlPrefix + $Attachment)
$TargetItem.Attachments.Add($Attachment, $SpFile.OpenBinary())
}
}
$StopTime = Get-Date -Format o
Write-Output "Process finished at: $StopTime"
$ElapsedTime = New-TimeSpan -Start $StartTime -End $StopTime
Write-Output "Elapsed time: $ElapsedTime"