How to download all attachments for all tasks in a list

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

1 reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *