Showing posts with label ListItemCollectionPosition. Show all posts
Showing posts with label ListItemCollectionPosition. Show all posts

Friday, June 24, 2016

Get ListItem Attachments using Iterating Large SharePoint Lists with PowerShell

Get ListItem Attachments using Iterating Large SharePoint Lists with PowerShell.

$SPAssignment = Start-SPAssignment
$webUrl = "http://sharepointurl:1234/"   
$listname = "ListDisplayName"  
$destinationfolder = "c:\backupfiles"    
$site = new-object Microsoft.SharePoint.SPSite($webUrl)   
$web = $site.OpenWeb()        
$list = $web.Lists[$listname] 
$spQuery = New-Object Microsoft.SharePoint.SPQuery
$query = '<Where><IsNotNull><FieldRef Name="ID" /></IsNotNull></Where>'
$spQuery.ViewAttributes = "Scope = 'Recursive'"
$spQuery.Query = $query
$spQuery.RowLimit = 2000
Write-Host "    ItemCount: " $list.ItemCount
do
{
    $spListItemCol = $list.GetItems($spQuery)
    $spQuery.ListItemCollectionPosition = $spListItemCol.ListItemCollectionPosition        
       foreach ($listItem in $spListItemCol)
       {
              Write-Host "    Item ID: " $listItem.ID      
              if (!(Test-Path -path $destinationfolder))       
              {           
                     $dest = New-Item $destinationfolder -type directory         
              }
              foreach ($attachment in $listItem.Attachments)   
              {       
                     $file = $web.GetFile($listItem.Attachments.UrlPrefix + $attachment)       
                     $bytes = $file.OpenBinary()               
                     $path = $destinationfolder + "\" + $listItem.ID + "_" + $attachment
                     Write "Saving $path"
                     $fs = new-object System.IO.FileStream($path, "OpenOrCreate")
                     $fs.Write($bytes, 0 , $bytes.Length)   
                     $fs.Close()   
              }
       }
}
while ($spQuery.ListItemCollectionPosition -ne $null)
Stop-SPAssignment $SPAssignment

Featured Post

Microsoft Copilot Studio: The Complete Beginner to Advanced Guide

Microsoft Copilot Studio: The Complete Beginner to Advanced Guide Version: July 2026 | Audience: Beginners, Citizen Developers, Busine...

Popular posts