Skip to content

Commit 54fc05e

Browse files
authored
Merge branch 'develop' into develop
2 parents 699d46b + faa1ace commit 54fc05e

File tree

4 files changed

+133
-165
lines changed

4 files changed

+133
-165
lines changed

Items Management/Attachments/Copy list attachments to a separate library/CopyAttachments.ps1

Lines changed: 83 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
function Get-SPOListFields
2-
{
3-
4-
<#
5-
.link
6-
http://social.technet.microsoft.com/wiki/contents/articles/32334.sharepoint-online-spomod-cmdlets-resources.aspx
7-
8-
#>
9-
10-
11-
param (
12-
[Parameter(Mandatory=$true,Position=3)]
13-
[string]$ListTitle,
14-
[Parameter(Mandatory=$false,Position=4)]
15-
[bool]$IncludeSubsites=$false
16-
)
1+
function Get-SPOListFields{
2+
<#
3+
.link
4+
http://social.technet.microsoft.com/wiki/contents/articles/32334.sharepoint-online-spomod-cmdlets-resources.aspx
5+
#>
6+
7+
param (
8+
[Parameter(Mandatory=$true,Position=3)]
9+
[string]$ListTitle,
10+
[Parameter(Mandatory=$false,Position=4)]
11+
[bool]$IncludeSubsites=$false
12+
)
1713

1814
$ll=$ctx.Web.Lists.GetByTitle($ListTitle)
1915
$ctx.Load($ll)
@@ -23,51 +19,48 @@ function Get-SPOListFields
2319

2420
$fieldsArray=@()
2521
$fieldslist=@()
26-
foreach ($fiel in $ll.Fields)
27-
{
28-
#Write-Host $fiel.Description `t $fiel.EntityPropertyName `t $fiel.Id `t $fiel.InternalName `t $fiel.StaticName `t $fiel.Tag `t $fiel.Title `t $fiel.TypeDisplayName
22+
23+
foreach ($fiel in $ll.Fields){
24+
#Write-Host $fiel.Description `t $fiel.EntityPropertyName `t $fiel.Id `t $fiel.InternalName `t $fiel.StaticName `t $fiel.Tag `t $fiel.Title `t $fiel.TypeDisplayName
2925

30-
$array=@()
31-
$array+="InternalName"
26+
$array=@()
27+
$array+="InternalName"
3228
$array+="StaticName"
33-
$array+="Title"
29+
$array+="Title"
3430

35-
$obj = New-Object PSObject
36-
$obj | Add-Member NoteProperty $array[0]($fiel.InternalName)
37-
$obj | Add-Member NoteProperty $array[1]($fiel.StaticName)
38-
$obj | Add-Member NoteProperty $array[2]($fiel.Title)
31+
$obj = New-Object PSObject
32+
$obj | Add-Member NoteProperty $array[0]($fiel.InternalName)
33+
$obj | Add-Member NoteProperty $array[1]($fiel.StaticName)
34+
$obj | Add-Member NoteProperty $array[2]($fiel.Title)
3935

40-
$fieldsArray+=$obj
41-
$fieldslist+=$fiel.InternalName
42-
Write-Output $obj
43-
}
36+
$fieldsArray+=$obj
37+
$fieldslist+=$fiel.InternalName
38+
Write-Output $obj
39+
}
4440

45-
46-
$ctx.Dispose()
41+
$ctx.Dispose()
42+
4743
return $fieldsArray
48-
4944
}
5045

5146

52-
function Get-SPOListItems
53-
{
47+
function Get-SPOListItems{
5448
<#
5549
.link
5650
http://social.technet.microsoft.com/wiki/contents/articles/32334.sharepoint-online-spomod-cmdlets-resources.aspx
57-
5851
#>
5952

60-
param (
61-
[Parameter(Mandatory=$true,Position=1)]
62-
[string]$ListTitle,
63-
[Parameter(Mandatory=$false,Position=2)]
64-
[bool]$IncludeAllProperties=$false,
65-
[switch]$Recursive,
66-
[Parameter(Mandatory=$false,Position=4)]
67-
$DestinationLibrary,
68-
[Parameter(Mandatory=$false,Position=5)]
69-
[bool]$Overwrite
70-
)
53+
param (
54+
[Parameter(Mandatory=$true,Position=1)]
55+
[string]$ListTitle,
56+
[Parameter(Mandatory=$false,Position=2)]
57+
[bool]$IncludeAllProperties=$false,
58+
[switch]$Recursive,
59+
[Parameter(Mandatory=$false,Position=4)]
60+
$DestinationLibrary,
61+
[Parameter(Mandatory=$false,Position=5)]
62+
[bool]$Overwrite
63+
)
7164

7265

7366
$ll=$ctx.Web.Lists.GetByTitle($ListTitle)
@@ -76,90 +69,75 @@ function Get-SPOListItems
7669
$ctx.ExecuteQuery()
7770
$i=0
7871

72+
$spqQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
73+
# $spqQuery.ViewAttributes = "Scope='Recursive'"
74+
$spqQuery.ViewXml = "<Where><Eq><FieldRef Name='Attachments' /><Value Type='Boolean'>1</Value></Eq></Where>";
7975

80-
81-
$spqQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
82-
# $spqQuery.ViewAttributes = "Scope='Recursive'"
83-
$spqQuery.ViewXml = "<Where><Eq><FieldRef Name='Attachments' /><Value Type='Boolean'>1</Value></Eq></Where>";
84-
if($Recursive)
85-
{
86-
$spqQuery.ViewXml +="<View Scope='RecursiveAll' />";
87-
}
88-
$bobo=Get-SPOListFields -ListTitle $ListTitle
76+
if($Recursive){
77+
$spqQuery.ViewXml +="<View Scope='RecursiveAll' />";
78+
}
79+
80+
$bobo=Get-SPOListFields -ListTitle $ListTitle
8981

9082

9183
$itemki=$ll.GetItems($spqQuery)
9284
$ctx.Load($itemki)
9385
$ctx.ExecuteQuery()
9486

95-
96-
9787
$objArray=@()
9888

99-
for($j=0;$j -lt $itemki.Count ;$j++)
100-
{
89+
for($j=0;$j -lt $itemki.Count ;$j++){
10190

10291
$attache=$itemki[$j].AttachmentFiles
10392
$ctx.Load($attache)
10493
$ctx.ExecuteQuery()
10594
Write-Host $itemki[$j]["Title"] -BackgroundColor DarkCyan
106-
foreach($att in $attache)
107-
{
95+
96+
foreach($att in $attache){
10897
#Write-Output $att
10998
$file =
110-
$ctx.Web.GetFileByServerRelativeUrl($att.ServerRelativeUrl);
111-
$ctx.Load($file)
112-
$ctx.ExecuteQuery()
99+
$ctx.Web.GetFileByServerRelativeUrl($att.ServerRelativeUrl);
100+
$ctx.Load($file)
101+
$ctx.ExecuteQuery()
113102

114103
$NewName=$file.Name
115104

116-
117-
if($DestinationLibrary.EndsWith("/")){}
118-
else {$DestinationLibrary=$DestinationLibrary+"/"}
119-
Write-Host "Processing " $file.Name
120-
$file.CopyTo($DestinationLibrary+$NewName, $Overwrite)
121-
try
122-
{
123-
$ctx.ExecuteQuery()
105+
if($DestinationLibrary.EndsWith("/")){}
106+
else {$DestinationLibrary=$DestinationLibrary+"/"}
124107

125-
Write-Host $file.Name " has been copied to" $DestinationLibrary -ForegroundColor DarkGreen
126-
}
127-
catch [Net.WebException]
128-
{
129-
Write-Host $_.Exception.ToString()
130-
}
108+
Write-Host "Processing " $file.Name
109+
$file.CopyTo($DestinationLibrary+$NewName, $Overwrite)
110+
111+
try{
112+
$ctx.ExecuteQuery()
113+
Write-Host $file.Name " has been copied to" $DestinationLibrary -ForegroundColor DarkGreen
114+
}
115+
catch [Net.WebException]{
116+
Write-Host $_.Exception.ToString()
117+
}
131118
}
132-
133-
134119
}
120+
}
135121

136122

137-
}
138-
139-
140-
function Connect-SPOCSOM
141-
{
142-
143-
<#
144-
.link
145-
http://social.technet.microsoft.com/wiki/contents/articles/32334.sharepoint-online-spomod-cmdlets-resources.aspx
146-
123+
function Connect-SPOCSOM{
124+
<#
125+
.link
126+
http://social.technet.microsoft.com/wiki/contents/articles/32334.sharepoint-online-spomod-cmdlets-resources.aspx
147127
#>
148128

149-
param (
150-
[Parameter(Mandatory=$true,Position=1)]
151-
[string]$Username,
152-
[Parameter(Mandatory=$true,Position=3)]
153-
[string]$Url
154-
155-
156-
)
157-
158-
$password = Read-Host "Password" -AsSecureString
159-
$ctx=New-Object Microsoft.SharePoint.Client.ClientContext($Url)
160-
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $password)
161-
$ctx.ExecuteQuery()
162-
$global:ctx=$ctx
129+
param (
130+
[Parameter(Mandatory=$true,Position=1)]
131+
[string]$Username,
132+
[Parameter(Mandatory=$true,Position=3)]
133+
[string]$Url
134+
)
135+
136+
$password = Read-Host "Password" -AsSecureString
137+
$ctx=New-Object Microsoft.SharePoint.Client.ClientContext($Url)
138+
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $password)
139+
$ctx.ExecuteQuery()
140+
$global:ctx=$ctx
163141
}
164142

165143

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,26 @@
1-
Description
1+
# Description
22
The following script copies list attachments to another library within the same site.
33

4-
54

6-
75

8-
Scenario:
6+
# Scenario:
97

10-
118
List with attachments:
129

13-
14-
15-
10+
<img src="../Copy list attachments to a separate library/Capture.PNG">
1611

1712
sometimes several attachments per item:
1813

19-
20-
21-
14+
<img src="../Copy list attachments to a separate library/Capture2.PNG">
2215

2316
The attachments are to be copied to another library:
2417

18+
<img src="../Copy list attachments to a separate library/Capture3.PNG">
2519

26-
27-
28-
29-
Requirements
20+
# Requirements
3021
The script requires SharePoint Online SDK.
3122

32-
33-
34-
How to use?
23+
# How to use?
3524
1. Download and open the file.
3625

3726
2. Enter correct paths to the SDK:
@@ -52,4 +41,8 @@ Connect-SPOCSOM -Username "user@tenant.onmicrosoft.com" -Url "https://tenant.sha
5241

5342

5443
<br/><br/>
55-
<b>Enjoy and please share feedback!</b>
44+
45+
<b>Enjoy and please share feedback!</b>
46+
47+
<b>Enjoy and please share feedback!</b>
48+
Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,41 @@
1-
1+
22
#
33
# Created by Arleta Wanat, 2015
44
#
55

6-
function Set-SPOListsAttachments
7-
{
8-
param (
9-
[Parameter(Mandatory=$true,Position=1)]
10-
[string]$Username,
11-
[Parameter(Mandatory=$true,Position=2)]
12-
[string]$AdminPassword,
6+
function Set-SPOListsAttachments{
7+
param (
8+
[Parameter(Mandatory=$true,Position=1)]
9+
[string]$Username,
10+
[Parameter(Mandatory=$true,Position=2)]
11+
[string]$AdminPassword,
1312
[Parameter(Mandatory=$true,Position=3)]
14-
[string]$Url,
13+
[string]$Url,
1514
[Parameter(Mandatory=$true,Position=4)]
16-
[bool]$Attachments
17-
)
15+
[bool]$Attachments
16+
)
1817

19-
$password = ConvertTo-SecureString -string $AdminPassword -AsPlainText -Force
18+
$password = ConvertTo-SecureString -string $AdminPassword -AsPlainText -Force
2019
$ctx=New-Object Microsoft.SharePoint.Client.ClientContext($Url)
2120
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $password)
2221
$ctx.ExecuteQuery()
2322

24-
$Lists=$ctx.Web.Lists
25-
$ctx.Load($Lists)
26-
$ctx.ExecuteQuery()
27-
28-
Foreach($ll in $Lists)
29-
{
30-
$ll.EnableAttachments = $Attachments
31-
$ll.Update()
32-
33-
34-
try
35-
{
36-
$ctx.ExecuteQuery()
37-
Write-Host $ll.Title " Done" -ForegroundColor Green
38-
}
39-
40-
catch [Net.WebException]
41-
{
42-
43-
Write-Host "Failed" $_.Exception.ToString() -ForegroundColor Red
23+
$Lists=$ctx.Web.Lists
24+
$ctx.Load($Lists)
25+
$ctx.ExecuteQuery()
26+
27+
Foreach($ll in $Lists){
28+
$ll.EnableAttachments = $Attachments
29+
$ll.Update()
30+
31+
try{
32+
$ctx.ExecuteQuery()
33+
Write-Host $ll.Title " Done" -ForegroundColor Green
4434
}
45-
46-
}
35+
catch [Net.WebException] {
36+
Write-Host "Failed" $_.Exception.ToString() -ForegroundColor Red
37+
}
38+
}
4739
}
4840

4941

@@ -68,4 +60,4 @@ $Attachments=$false
6860

6961

7062

71-
Set-SPOListsAttachments -Username $Username -AdminPassword $AdminPassword -Url $Url -Attachments $Attachments
63+
Set-SPOListsAttachments -Username $Username -AdminPassword $AdminPassword -Url $Url -Attachments $Attachments

0 commit comments

Comments
 (0)