-
Notifications
You must be signed in to change notification settings - Fork 0
/
only items with attachments.ps1
69 lines (56 loc) · 2.56 KB
/
only items with attachments.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
# Lists only items with attachments in SharePoint Online list and exports them to "c:\users\public\Attachments.csv"
function Get-Items
{
param (
[Parameter(Mandatory=$true,Position=1)]
[string]$Username,
[Parameter(Mandatory=$true,Position=2)]
[string]$Url,
[Parameter(Mandatory=$true,Position=3)]
$password,
[Parameter(Mandatory=$true,Position=4)]
[string]$ListTitle,
[Parameter(Mandatory=$true,Position=5)]
[string]$CSVPath
)
$ctx=New-Object Microsoft.SharePoint.Client.ClientContext($url)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $password)
$ctx.Load($ctx.Web)
$ctx.ExecuteQuery()
$ll=$ctx.Web.Lists.GetByTitle($ListTitle)
$ctx.Load($ll)
$ctx.ExecuteQuery()
$spqQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
$spqQuery.ViewXml ="<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='Attachments' /><Value Type='Boolean'>1</Value></Eq></Where></Query></View>";
$items=$ll.GetItems($spqQuery)
$ctx.Load($items)
$ctx.ExecuteQuery()
foreach($item in $items)
{
Write-Host "Processing item no " $item.ID " " -NoNewline
$ctx.Load($item)
$ctx.Load($item.AttachmentFiles)
$ctx.ExecuteQuery()
#The output object is customized because I am only interested in title and ID. If you want you can export the entire $item object to CSV
$Object = New-Object PSObject
$Object | add-member ID $item.ID
$Object | add-member ItemTitle $item["Title"]
$Object | add-member NumberOfAttachments $item.AttachmentFiles.Count #.AttachmentFiles.Count
Export-Csv -InputObject $object -Path $CSVPath -Append
}
Write-Host
Write-Output "Execution finished."
$ctx.Dispose()
}
#Paths to SDK
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"
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.Office.Client.Policy.dll"
#Enter the data
$username = "ana@etr56.onmicrosoft.com"
$Url = "https://etr56.sharepoint.com"
$ListTitle = "attatest"
$CSVPath = "c:\users\public\attachmentsonly.csv"
#Do not modify the lines below
$AdminPassword = Read-Host -Prompt "Enter password" -AsSecureString
Get-Items -Username $username -Url $Url -password $AdminPassword -ListTitle $ListTitle -CSVPath $CSVPath