How to Download All Attachments for All Tasks in a List

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.

Additional Read
Secure Store Master Key Error

Share this entry

5 Responses

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

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

Leave a Reply

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

Table of Contents

Categories

Categories