none
Copy All Document Versions From One Library to Another in SharePoint 2010 Env. PowerShell Code

    Question

  • Would anyone know how to capture All Versions of a Document when copying documents from one Library to another.  Am trying to copy documents from a Library in one Site to a Library in a different Site (both sites reside within the same Site Collection). 

    The following code captures all document versions via the (foreach($version in $versions) command below, but the Add and Update commands error out and am unable to copy the document and its versions from the originating Libary to the destination Library.

    function copyListItems($siteFrom, $siteTo, $name)
    {
    	Write-Host "copyListItems Function"
    	Write-Host "siteFrom: $siteFrom ~~ siteTo: $siteTo ~~ name:  $name"
    	$site = Get-SPSite $hrUrl
    	Write-Host "site:  $site"
       	$fromWeb = Get-SPWeb -Identity $siteFrom
    	Write-Host "fromWeb: $fromWeb"
       	$toWeb = Get-SPWeb -Identity $siteTo
    	Write-Host "toWeb: $toWeb"
    	$fromList = $fromWeb.Lists["$name"]
    	Write-Host "fromList: $fromList"
    	$toList = $toWeb.Lists["$name"]
    	write-Host "toList: $toList"
    	if($fromList.Items.Count -gt 0)
    	{
    		#clear toList?
    		
    		Write-Host "$name :: " $fromList.Items.Count
    		$items = $fromList.Items
    		foreach($item in $items)
    		{
    			Write-Host "Adding... " $item.Name
    			$fileBytes = @($item.File.OpenBinary()) #@(items) makes an array
    			$overwrite = $true
    			Write-Host "overWrite:  $overWrite"
    			#$includeVersions = "All"
    			$versions = $item.Versions
    			foreach($version in $versions)
    			{
    				Write-Host "********Versions: $version"				
    			}
    			$toListRootUrl = $toList.RootFolder.Url
    			Write-Host "toListRootUrl: $toListRootUrl"
    			$fromListFileName = $item.Name
    			Write-Host "fromListFileName: $fromListFileName"
    			$toUrl = "$toListRootUrl/$fromListFileName"
    			Write-Host "toUrl: $toUrl" #$fileBytes
    			
    			$toFile = $toList.RootFolder.Files.Add($toUrl,$fileBytes,$includeVersions)
    			$toFile.Update()
    		}
    	}
    }

    Results of Add Command:

    Multiple ambiguous overloads found for "Add" and the argument count: "3".
    At E:\ecs\hrconf\HrDocCopyGetAllVersionsTest.ps1:117 char:42
    +             $toFile = $toList.RootFolder.Files.Add <<<< ($toUrl,$fileBytes,$i
    ncludeVersions)
        + CategoryInfo          : NotSpecified: (:) [], MethodException
        + FullyQualifiedErrorId : MethodCountCouldNotFindBest


    Cheryl

    Friday, March 30, 2012 1:28 AM

Answers

  • SharePoint has three options to move content across sites, PowerShell, web services and Content Migration API. Using the RootFolder to add documents with versions will be difficult, because you will need to check-in, check-out and publish files in the exact same order. 

    PowerShell has a couple cmdlets that you can use to migrate libraries. The following cmdlet will copy export an entire document library. You can use Import-SPWeb to import the document library to another site.

    Export-SPWeb "http://SharePointSiteCollectionUrl" -ItemUrl "/Shared Documents" -Path C:\Temp\SharedDocuments.cmp -IncludeVersions All -Force

    Moving files on the item level can only be done using the Content Migration API. Take a look at this blog post on how to setup SPExport to export specific files:

    http://sadomovalex.blogspot.com/2011/09/using-spexportspimport-for-copying.html


    SharePoint Developer | @zeemanj | Blog

    Saturday, March 31, 2012 1:03 PM
  • I think you have to use the following overload of the Files.Add method:

    SPFileCollection.Add Method (String, Stream, Hashtable, SPUser, SPUser, 
    DateTime, DateTime, String, Boolean) 

    Info can be found at MSDN: http://msdn.microsoft.com/en-us/library/ee576592.aspx

    The Hashtable has to contain metadata for the file, in this case for the version of the file fill it by using the statement:

    $item.File.Versions[$i].Properties 

    and to set the checkInComment you can use

    $item.File.Versions[$i].CheckInComment 

    By using $item.File.Versions[$i]... properties you can capture all necessary info about the file version and add it to the file by using the overload I mentioned before.

    Sunday, April 01, 2012 9:42 AM

All replies

  • SharePoint has three options to move content across sites, PowerShell, web services and Content Migration API. Using the RootFolder to add documents with versions will be difficult, because you will need to check-in, check-out and publish files in the exact same order. 

    PowerShell has a couple cmdlets that you can use to migrate libraries. The following cmdlet will copy export an entire document library. You can use Import-SPWeb to import the document library to another site.

    Export-SPWeb "http://SharePointSiteCollectionUrl" -ItemUrl "/Shared Documents" -Path C:\Temp\SharedDocuments.cmp -IncludeVersions All -Force

    Moving files on the item level can only be done using the Content Migration API. Take a look at this blog post on how to setup SPExport to export specific files:

    http://sadomovalex.blogspot.com/2011/09/using-spexportspimport-for-copying.html


    SharePoint Developer | @zeemanj | Blog

    Saturday, March 31, 2012 1:03 PM
  • I think you have to use the following overload of the Files.Add method:

    SPFileCollection.Add Method (String, Stream, Hashtable, SPUser, SPUser, 
    DateTime, DateTime, String, Boolean) 

    Info can be found at MSDN: http://msdn.microsoft.com/en-us/library/ee576592.aspx

    The Hashtable has to contain metadata for the file, in this case for the version of the file fill it by using the statement:

    $item.File.Versions[$i].Properties 

    and to set the checkInComment you can use

    $item.File.Versions[$i].CheckInComment 

    By using $item.File.Versions[$i]... properties you can capture all necessary info about the file version and add it to the file by using the overload I mentioned before.

    Sunday, April 01, 2012 9:42 AM