Saturday, February 1, 2014

Copy Attachment from SharePoint List to Document Library using SharePoint Designer - PowerShell

SharePoint keeps attachments under "List >> Attachments >> 'List Item ID'" folder path. So, if you want to copy attachment from list to document library, follow these steps:

  1. Open your SharePoint site from SharePoint Designer
  2. Navigate to "All Files" view >> Lists >> Your Source List >> Attachments folder. Here, folders are created based on list item's ID. 
    sharepoint copy attachment from list to document library
  3. Just copy attachment files from these folders and navigate to the target document library and paste there.sharepoint designer 2010 copy attachment
Well, it would be tedious to copy list attachment to document library, if you have large number of list items/attachments. So, lets use PowerShell in SharePoint to copy list attachment to document library.

PowerShell script to copy attachment from list to document library:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$WebURL = ""

$SourceListName = "External Projects"   
$TargetLibraryName = "Design Documents"

#Get the Web List and Library objects
$web = Get-SPWeb $WebURL
$SourceList = $web.Lists[$SourceListName]    
$TargetLibrary = $web.Lists[$TargetLibraryName]    

#Loop through each list item
foreach ($ListItem in $SourceList.Items)
 if($ListItem.Attachments.Count -gt 0)
  #Loop through each attachment in the list item
        foreach ($Attachment in $ListItem.Attachments)    
    #Get the attachment
              $file = $web.GetFile($ListItem.Attachments.UrlPrefix+$Attachment)        
              $bytes = $file.OpenBinary()               
    $TargetFileName = $TargetLibrary.RootFolder.Url+"/"+$Attachment
    $TargetFile = $TargetLibrary.RootFolder.Files.Add($TargetFileName, $bytes, $true)
    Write-Host "Copied to: $($TargetFilename)"
This script copies all attachments from all list items to the given library's root folder. You can tweak the script little to create sub-folders in the target library too.

You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.

Check out these SharePoint products:

1 comment :

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...