Quick note: First and foremost - this is somewhat in line with a previous post of mine found here: http://stackoverflow.com/questions/11514833/move-files-between-document-libraries-in-the-same-site-collection however I've opened a new question for this as I didn't want to update my original post and destroy the value to be gained there for future readers as this is, in essence, a spin-off question of my original one.
Goal
I have a number of document libraries (100+) that have content in them that need to be archived for the end of financial year. Essentially content will move from the source document library, to the root of the destination library.
Current Code
[reflection.assembly]::loadwithpartialname("microsoft.sharepoint") > $null
$org = "http://dev/re/ad/wf/Approval%20History%20%20Centre%20EV"
$dest = "http://dev/re/ad/wf/Approval%20History%20%20Centre%20EV1"
$site = new-object microsoft.sharepoint.spsite $org
$web = $site.openweb()
$srcLibrary = $web.Lists["Approval History - Centre EV"]
$destLibrary = $web.Lists["Approval History - Centre EV1"]
$destFiles = $destLibrary.Folders["Approval History - Centre EV"]
write-host "srcLibrary is: $srcLibrary"
write-host "destLibrary is: $destLibrary"
write-host "destFiles is: $destFiles"
foreach ($item in $srcLibrary.Items)
{
if ($item.File)
{
$curFile = $item.file.OpenBinary()
$destURL = $dest + "/" + $item.file.Name
write-host "destURL is: $destURL"
write-host "srcLibrary is: $srcLibrary"
$destFiles.Add($destURL, $curFile, $true)
}
}
Error and output
I'm currently seeing the following output:
srcLibrary is: Approval History - Centre EV
destLibrary is: Approval History - Centre EV1
destFiles is:
destURL is: http://dev/re/ad/wf/Approval%20History%20%20Centre%20EV1/EV_00249629__2011-03-15T095937077.xml
srcLibrary is: Approval History - Centre EV
You cannot call a method on a null-valued expression.
At line:30 char:23
+ $destFiles.Add <<<< ($destURL, $curFile, $true)
+ CategoryInfo : InvalidOperation: (Add:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Code Issue
Currently $destLibrary in the code above is assigning to a null value - unfortunately I lack the expertise to understand why. I've also tried creating a folder named 'Archived' in the destination library and setting this line to the following:
$destFiles = $destLibrary.Folders["Archived"]
Unfortunately this results in the same output as shown above.
A bit about the farm
Server name: dev
Site Collection: /re/
Site: /wf/, a sub-site of /ad/