How to Download All Attachments for All Tasks in a List How to Download All Attachments for All Tasks in a List
How-to-Download
Share:

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

  1. 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

  2. is it possible to download all the attachments of splist , using SPSERVICES OR client side JSOM/ javascript?

    1. 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.

Leave a Reply

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

Want to talk?

Drop us a line. We are here to answer your questions 24*7.