SharePoint 2019 document library folder attachment download based on camel query

Sajith Gopalakrishnan Hema 1,046 Reputation points
2025-02-05T16:36:48.5166667+00:00

SharePoint 2019 document library has folders. Each folder contains multiple files having attachments and without attachments. Folder has metadata like NoAttachment (boolean) etc.

How to download the file (attachments) using Power shell based on Camel Query ?

SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,633 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Emily Du-MSFT 50,411 Reputation points Microsoft Vendor
    2025-02-06T08:02:51.77+00:00

    You could try below PowerShell to download files with NoAttachment metadata as false.

    # Load the SharePoint Client Object Model
    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"
    # Define variables
    $siteUrl = "https://yoursharepointsiteurl"
    $libraryName = "Documents"  # The name of your document library
    $downloadFolder = "C:\Downloads\Files"  # Local path where the files will be downloaded
    $metadataField = "NoAttachment"  # Metadata field name
    # CAML Query to filter files based on metadata (NoAttachment = False)
    $camlQuery = @"
        <View>
            <Query>
                <Where>
                    <Eq>
                        <FieldRef Name='$metadataField' />
                        <Value Type='Boolean'>0</Value>  <!-- Only files where NoAttachment is False -->
                    </Eq>
                </Where>
            </Query>
        </View>
    "@
    # Connect to the SharePoint site using Client Context
    $context = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
    # Provide credentials
    $context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials("******@domain.com", (ConvertTo-SecureString "yourpassword" -AsPlainText -Force))
    # Get the document library
    $library = $context.Web.Lists.GetByTitle($libraryName)
    # Create a new CamlQuery object and execute it
    $query = New-Object Microsoft.SharePoint.Client.CamlQuery
    $query.ViewXml = $camlQuery
    # Get the items matching the query
    $items = $library.GetItems($query)
    $context.Load($items)
    $context.ExecuteQuery()
    # Loop through each item and download the files (attachments)
    foreach ($item in $items) {
        # Check if the item has attachments
        if ($item.Attachments.Count -gt 0) {
            Write-Host "Item with ID $($item.Id) has attachments. Downloading..."
            # Get the attachment files
            foreach ($attachment in $item.Attachments) {
                $attachmentUrl = $attachment.Url
                $fileName = [System.IO.Path]::GetFileName($attachmentUrl)
                $fileUrl = "$siteUrl$attachmentUrl"
                # Download the file
                $file = Invoke-WebRequest -Uri $fileUrl -OutFile "$downloadFolder\$fileName"
                Write-Host "Downloaded attachment: $fileName"
            }
        } else {
            Write-Host "Item with ID $($item.Id) has no attachments."
        }
    }
    
    

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.