-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMove-Lists.ps1
101 lines (81 loc) · 3.55 KB
/
Move-Lists.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
param (
[string]$Path
)
#-----------------------------------------------------------------------
# Script lets you migrate one or more SharePoint lists from source site
# To destination site
# Denis Molodtsov, 2021
#-----------------------------------------------------------------------
$ErrorActionPreference = "Stop"
Clear-Host
Write-Host $Path -ForegroundColor Green
Set-Location $Path
. .\MISC\PS-Forms.ps1
Get-ChildItem -Recurse | Unblock-File
# Legacy PowerShell PnP Module is used because the new one has a critical bug
Import-Module (Get-ChildItem -Recurse -Filter "*.psd1").FullName -DisableNameChecking
$Migration = @{
Source_Site = "https://contoso.sharepoint.com/sites/Site_A"
Target_Site = "https://contoso.sharepoint.com/sites/Site_b"
Lists = ""
Verbose_Logs = "FALSE"
}
$Migration = Get-FormItemProperties -item $Migration -dialogTitle "Enter source and target sites" -propertiesOrder @("Source_Site", "Target_Site", "Lists", "Verbose_Logs") -note "List titles are comma-separated. Leave blank to have an interactive selector"
Connect-PnPOnline -Url $Migration.Source_Site -UseWebLogin -WarningAction Ignore
if([System.Convert]::ToBoolean($Migration.Verbose_Logs)){
Write-host "Verbose logging is on" -ForegroundColor Yellow
Set-PnPTraceLog -On -Level Debug
}
if ($Migration.Lists) {
$titles = $Migration.Lists.Split(",")
}
else {
$lists = Get-PnPList
$lists = $lists | Where-Object { $_.Hidden -eq $false }
$selectedLists = Get-FormArrayItems ($lists) -dialogTitle "Select lists and libraries to migrate" -key Title
$titles = $selectedLists.Title
# If a list contains custom content types, we will have to move them all.
$exportContentTypes = $false
foreach ($list in $selectedLists) {
$listContentTypes = Get-PnPContentType -List $list
$context = $list.Context
foreach ($contentType in $listContentTypes) {
$context.Load($contentType)
$context.ExecuteQuery()
[xml]$xml = $contentType.SchemaXml
if ($null -eq $xml.ContentType.FeatureId) {
$exportContentTypes = $true;
}
}
}
}
if ($exportContentTypes) {
Get-PnPProvisioningTemplate -ListsToExtract $titles -Out "Lists.xml" -Handlers Lists, ContentTypes, Fields -Force -WarningAction Ignore
}
else {
Get-PnPProvisioningTemplate -ListsToExtract $titles -Out "Lists.xml" -Handlers Lists -Force -WarningAction Ignore
}
((Get-Content -Path Lists.xml -Raw) -replace 'RootSite', 'Web') | Set-Content -Path Lists.xml
foreach ($title in $titles) {
# Get the latest list item form layout. Footer, Header and the Body:
$list = Get-PnPList $title -Includes ContentTypes
$contentType = $list.ContentTypes | Where-Object { $_.Name -eq "Item" }
$contentType.ClientFormCustomFormatter | Set-Content .\$title.json
}
Disconnect-PnPOnline
Connect-PnPOnline -Url $Migration.Target_Site -UseWebLogin
Apply-PnPProvisioningTemplate -Path Lists.xml
foreach ($title in $titles) {
$list = Get-PnPList $title -Includes ContentTypes
$contentType = $list.ContentTypes | Where-Object { $_.Name -eq "Item" }
if ($contentType) {
$json = Get-Content .\$title.json
$contentType.ClientFormCustomFormatter = $json
$contentType.Update($false)
$contentType.Context.ExecuteQuery();
}
}
Write-Host ""
Write-Host "Target site" $($Migration.Target_Site + "/_layouts/15/viewlsts.aspx") -ForegroundColor Yellow
$Url_To_Open = ($Migration.Target_Site + "/_layouts/15/viewlsts.aspx")
Start-Process $Url_To_Open