Quantcast
Channel: SharePoint Diary
Viewing all articles
Browse latest Browse all 1058

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

$
0
0
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 = "http://sharepoint.crescent.com/pmo/GIS/"

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

Viewing all articles
Browse latest Browse all 1058

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>