-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCopyListFolderStructureToAnotherList.ps1
123 lines (92 loc) · 3.64 KB
/
CopyListFolderStructureToAnotherList.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
function Get-folders()
{
param (
[Parameter(Mandatory=$true,Position=0)]
$Folders,
[Parameter(Mandatory=$true,Position=1)]
[string]$OriginalLibrary,
[Parameter(Mandatory=$true,Position=2)]
[string]$DestinationLibrary
)
$Host.Runspace.ThreadOptions = “ReuseThread”
$ll2=$ctx.Web.Lists.GetByTitle($DestinationLibrary.Replace("/",""))
foreach($folder in $folders)
{
$ctx.Load($folder.Folders)
$ctx.ExecuteQuery()
#Write-host $folder.ServerRelativeUrl $folder.folders.count
if($folder.ServerRelativeUrl -match $OriginalLibrary)
{
$urel= $folder.ServerRelativeUrl.Replace($OriginalLibrary,$DestinationLibrary)
Write-Host $urel
$newFolder=$ll2.RootFolder.Folders.Add($folder.ServerRelativeUrl.Replace($OriginalLibrary,$DestinationLibrary))
$ctx.Load($newFolder)
$ctx.ExecuteQuery()
}
if($folder.Folders.Count -gt 0)
{
Get-folders -Folders $folder.Folders -OriginalLibrary $OriginalLibrary -DestinationLibrary $DestinationLibrary
}
}
}
function Get-Webfolders()
{
param (
[Parameter(Mandatory=$true,Position=0)]
[string]$OriginalLibrary,
[Parameter(Mandatory=$true,Position=2)]
[string]$DestinationLibrary
)
$Host.Runspace.ThreadOptions = “ReuseThread”
$OriginalList=$ctx.Web.Lists.GetByTitle($OriginalLibrary)
$DestinationList=$ctx.Web.Lists.GetByTitle($DestinationLibrary)
$folderCollection=$OriginalList.RootFolder.Folders
$ctx.load($OriginalList)
$ctx.Load($folderCollection)
$ctx.ExecuteQuery()
$OriginalLibrary="/"+$OriginalLibrary+"/"
$DestinationLibrary="/"+$DestinationLibrary+"/"
foreach($fodler in $folderCollection)
{
$ctx.Load($fodler.Folders)
$ctx.ExecuteQuery()
#Write-host $fodler.ServerRelativeUrl $fodler.folders.count
if($fodler.ServerRelativeUrl -match $OriginalLibrary)
{
$urel= $fodler.ServerRelativeUrl.Replace($OriginalLibrary,$DestinationLibrary)
Write-Host $urel
$newFolder=$ll2.RootFolder.Folders.Add($fodler.ServerRelativeUrl.Replace($OriginalLibrary,$DestinationLibrary))
$ctx.Load($newFolder)
$ctx.ExecuteQuery()
}
if($fodler.Folders.Count -gt 0){
Get-folders -folders $fodler.Folders -OriginalLibrary $OriginalLibrary -DestinationLibrary $DestinationLibrary
}
}
}
function Connect-SPO()
{
param (
[Parameter(Mandatory=$true,Position=1)]
[string]$Username,
[Parameter(Mandatory=$true,Position=2)]
[string]$Url,
[Parameter(Mandatory=$true,Position=3)]
$AdminPassword
)
$global:ctx=New-Object Microsoft.SharePoint.Client.ClientContext($Url)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $AdminPassword)
$ctx.Load($ctx.Web)
$ctx.ExecuteQuery()
}
# Paths to SDK. Please verify location on your computer.
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
$admin="t@trial890.onmicrosoft.com"
$pass=Read-Host "Enter Password: " -AsSecureString
$site="https://trial890.sharepoint.com/sites/teamsitewithlibraries"
$libraryTitle="tescik2"
$destLibr="lib4"
$global:ctx
Connect-SPO -Username $admin -Url $site -AdminPassword $pass
Get-Webfolders -DestinationLibrary $destLibr -OriginalLibrary $libraryTitle -ErrorAction Continue