Sharepoint Attachment Download

Woodward, Austin P - DHS1 1 Reputation point
2022-10-31T13:56:10.29+00:00

I have a sharepoint list in Sharepoint 365 and it can have as many as 50 attachments per list item and most of the attachments will be in word documents and pdf formats. I would like to download all of the attachments from the list item at the same time so I do not have to waste time clicking them one by one, is there a way to achieve this?

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,073 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. AllenXu-MSFT 17,581 Reputation points Microsoft Vendor
    2022-11-01T02:05:02.997+00:00

    Hi @Woodward, Austin P - DHS1 ,

    In this case you can download attachments from list item(s) using PowerShell instead of clicking them one by one on UI.

    PowerShell to download attachments from a List Item:

    #Load SharePoint CSOM Assemblies  
    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"  
       
    Function Download-ListItemAttachments()  
    {  
        param  
        (  
            [Parameter(Mandatory=$true)] [string] $SiteURL,  
            [Parameter(Mandatory=$true)] [string] $ListName,  
            [Parameter(Mandatory=$true)] [string] $ListItemID,  
            [Parameter(Mandatory=$true)] [string] $DownloadPath  
        )     
       Try {  
            #Setup Credentials to connect  
            $Cred = Get-Credential  
            $Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)  
           
            #Setup the context  
            $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)  
            $Ctx.Credentials = $Cred  
       
            #Get the List Item  
            $List = $Ctx.Web.Lists.GetByTitle($ListName)  
            $ListItem= $List.GetItemByID($ListItemID)  
           
            #Get All attachments from the List Item  
            $AttachmentsColl = $ListItem.AttachmentFiles  
            $Ctx.Load($AttachmentsColl)  
            $Ctx.ExecuteQuery()  
           
            #Get each attachment  
            ForEach($Attachment in $AttachmentsColl)  
            {  
                #Download attachment  
                $FileContent = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($Ctx, $Attachment.ServerRelativeUrl)  
                $FileStream = [System.IO.File]::Create($DownloadPath+$Attachment.FileName)  
                $FileContent.Stream.CopyTo($FileStream)  
                $FileStream.Close()  
           }  
       
            write-host  -f Green "Total List Attachments Downloaded : $($AttachmentsColl.count)"  
        }  
        Catch {  
            write-host -f Red "Error Downloading List Item Attachments!" $_.Exception.Message  
        }  
    }  
       
    #Set Parameters  
    $SiteURL= "https://contoso.sharepoint.com/"  
    $ListName="Projects"  
    $ListItemID="1"  
    $DownloadPath="C:\Temp\"  
       
    #Call the function to copy list items  
    Download-ListItemAttachments -SiteURL $SiteURL -ListName $ListName -ListItemID $ListItemID -DownloadPath $DownloadPath  
    

    Download Attachments from All Items in the List using PowerShell:

    #Load SharePoint CSOM Assemblies  
    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"  
       
    Function Download-ListAttachments()  
    {  
        param  
        (  
            [Parameter(Mandatory=$true)] [string] $SiteURL,  
            [Parameter(Mandatory=$true)] [string] $ListName,  
            [Parameter(Mandatory=$true)] [string] $DownloadDirectory  
        )     
       Try {  
            #Setup Credentials to connect  
            $Cred = Get-Credential  
            $Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)  
           
            #Setup the context  
            $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)  
            $Ctx.Credentials = $Cred  
       
            #Get All Items from the List  
            $List = $Ctx.Web.Lists.GetByTitle($ListName)  
            $ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())  
            $Ctx.Load($ListItems)  
            $Ctx.ExecuteQuery()  
               
            #Create download directory if it doesn't exist  
            If (!(Test-Path -path $DownloadDirectory))         
            {             
                New-Item $DownloadDirectory -type directory           
            }  
               
            #Iterate through each list item  
            Foreach($Item in $ListItems)  
            {  
                #Get All attachments from the List Item  
                $AttachmentsColl = $Item.AttachmentFiles  
                $Ctx.Load($AttachmentsColl)  
                $Ctx.ExecuteQuery()  
       
                #Get attachment for each list item  
                ForEach($Attachment in $AttachmentsColl)  
                {  
                    #Download attachment  
                    $FileContent = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($Ctx, $Attachment.ServerRelativeUrl)  
                    $FileName= $DownloadDirectory+$Item.id.ToString()+"_"+$Attachment.FileName  
                    $FileStream = [System.IO.File]::Create($FileName)  
                    $FileContent.Stream.CopyTo($FileStream)  
                    $FileStream.Close()  
               }  
            }  
       
            write-host  -f Green "List Attachments Downloaded Successfully!"  
        }  
        Catch {  
            write-host -f Red "Error Downloading List Attachments!" $_.Exception.Message  
        }  
    }  
       
    #Set Parameters  
    $SiteURL= "https://contoso.sharepoint.com/"  
    $ListName="Projects"  
    $DownloadDirectory="C:\Temp\"  
       
    #Call the function to copy list items  
    Download-ListAttachments -SiteURL $SiteURL -ListName $ListName -DownloadDirectory $DownloadDirectory  
    

    Download Attachment from SharePoint Online List using PnP PowerShell

    #Set Parameters  
    $SiteURL = "https://contoso.sharepoint.com/sites/Projects"  
    $ListName = "Project Docs"  
    $DownloadPath = "C:\Temp\"  
       
    #Connect to SharePoint Online site  
    Connect-PnPOnline -Url $SiteURL -Interactive  
       
    #Get all List Items  
    $Listitems = Get-PnPListItem -List $ListName -PageSize 500  
       
    #Iterate through List Items  
    ForEach($Item in $Listitems)  
    {  
        #Get Attachments from List Item  
        $Attachments = Get-PnPProperty -ClientObject $Item -Property "AttachmentFiles"  
       
        #Download All Attachments from List item  
        Write-host "Downloading Attachments from List item '$($Item.ID)', Attachments Found: $($Attachments.Count)"  
       
        #Create directory for each list item  
        $DownloadLocation = $DownloadPath+$Item.ID  
        If (!(Test-Path -path $DownloadLocation)) { New-Item $DownloadLocation -type Directory | Out-Null }  
       
        $Attachments | ForEach-Object {  
            Get-PnPFile -Url $_.ServerRelativeUrl -FileName $_.FileName -Path $DownloadLocation -AsFile -Force  
        }  
    }  
    

    ----------

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments