Downloading all attachments for a SharePoint task list Tasks can have attachments. In fact, they can have multiple attachments.
However, these are stored in an “AttachmentCollection”. We can iterate through all items in the task list to download all attachments.
What we do is create a folder for each of the items and name the folder by the ID of the task.
$webUrl = "http:.." # this is the URL of the SPWeb
$library = "Compliance Tasks" # this is the SPList display name
$tempLocation = "D:\PROD" # Local Folder to dump files
$s = new-object Microsoft.SharePoint.SPSite($webUrl)
$w = $s.OpenWeb()
$l = $w.Lists[$library]
foreach ($listItem in $l.Items)
{
Write-Host " Content: " $listItem.ID
$destinationfolder = $tempLocation + "\" + $listItem.ID
if($listItem.Attachments.Count -gt 0)
{
if (!(Test-Path -path $destinationfolder))
{
$dest = New-Item $destinationfolder -type directory
}
foreach ($attachment in $listItem.Attachments)
{
$file = $w.GetFile($listItem.Attachments.UrlPrefix + $attachment)
$bytes = $file.OpenBinary()
$path = $destinationfolder + "\" + $attachment
Write "Saving $path"
$fs = new-object System.IO.FileStream($path, "OpenOrCreate")
$fs.Write($bytes, 0 , $bytes.Length)
$fs.Close()
}
}
}
A folder for each task was created to allow for multiple attachments. The ID was applied to each folder to allow a subsequent script to traverse and upload the attachments by ID or for any linkage preservation.
For how to upload attachments from a task list, please see: Uploading attachments to tasks.
Additional Read
Secure Store Master Key Error
7 thoughts on “How to Download All Attachments for All Tasks in a List”
Leave a Reply
Want to talk?
Drop us a line. We are here to answer your questions 24*7.
Wow, you are a lifesaver!! This script is perfect. Thx a lot!
I pasted the code to the powershell and it didn’t work.
This looks like something I can use but where do I put the code to run the script? Is this behind a button on an Access form
is it possible to download all the attachments of splist , using SPSERVICES OR client side JSOM/ javascript?
you saved me a lot of time. thank you.
Do you have a script for SharePoint?
The PowerShell capabilities in SharePoint Online have always lagged SharePoint on-premises. The need for Microsoft to prevent one tenant from incurring an undue performance laod on other tenants is one factor. Other options include CSOM based solutions to achieve your goal.