Dec 8, 2014

Create a Managed Copy from PowerShell

If you've ever tried to create a managed copy from PowerShell, you might have had some trouble updating the source CopyDestinations collection.  I certainly did until I learned I had to use an Assignment collection when opening the source SPWeb object.  Go figure. 

Credit goes to Ryan Spletzer: http://sharepoint.stackexchange.com/questions/79294/spcopydestinationcollection-add-method-does-not-persist-to-the-database

Anyways, here's the code:
function CreateManagedCopy($sourceWebUrl,$sourceRelativeFileUrl,$destinationUrl) {
 Start-SPAssignment -AssignmentCollection $current
 $web = get-spweb $sourceWebUrl -AssignmentCollection $current
 $updatetype = [Microsoft.SharePoint.SPCopyDestinationType]::Update  
 $sourcefile = $web.GetListItem($sourceRelativeFileUrl);
 $sourcefile.CopyTo($destinationUrl)
 $sourcefile.CopyDestinations.Add($destinationUrl, $updatetype, "Copied from PowerShell")
 $sourcefile.UpdateOverwriteVersion() 


}

# To call the function: 
# CreateManagedCopy "https://intranet/sites/test" "/sites/test/libA/test.docx" "https://intranet/sites/test/moveTo/test10.docx"

No comments:

Post a Comment