4

Got over 200 items in various folders in one location that would like to copy to a new location while retaining the same folder structure. Each item has more than one version and would like to retain that as well.

Task 1: Copying multiple folders containing multiple items to a new location.

Task two: Updating the part of the Items' path with the new location. Example

Location A/Folder 1/subfolder 4/item_100

update it to

Location B/Folder 1/subfolder 4/item_1000

4

You have 2 ways - if you can connect up Sitecore Rocks, that has an option to multi-select items and move them around in Visual Studio and rename etc...

The second option would be to use Sitecore PowerShell Extensions (SPE) to script that out. This example will give you a good starting place:

$rootOfitemsToMove = Get-Item “/sitecore/content/Location A/subfolder 4”;
$destinationItemPath = “/sitecore/content/Location B/subfolder 4”;

$templateNameToMatch = “My Template”;

Write-Host “Moving items from: ” $rootOfitemsToMove.Paths.FullPath ” to: ” $destinationItem.Paths.FullPath ” …”;

Get-ChildItem | Where-Object { $_.TemplateName -match $templateNameToMatch } | ForEach-Object {
    $name = $_.Name
    if(![string]::IsNullOrEmpty($name))
    {
        $newName = "$($name)0"  # Add some logic here to rename the folder
        $newPath = "$($destinationItemPath)/$($newName)"

        Move-Item -Path $_.ItemPath -Destination $newPath;
        Write-Host “Item moved to: $($newPath)“;
    }
    else
    {
        Write-Host “Couldn’t move Item: ” $name;
    }
}

Write-Host “Moving items complete.”;

You would need to work out the logic of how the folders are renamed. Moving a parent folder should also move the child items with it.

  • @ Richard, one more question. Can you apply this logic to pages as well? but still updating path of the components associated. Example copying page 1 and 2 from location A to Location B. Then updating the components of page 1 and 2 with the new location B as part of their path. – AIM Dec 7 '18 at 13:43
  • 2
    The presentation is a field on the item, so that will copy with the item. If the datasource paths need updating, you can also do that with SPE - I would suggest opening a new question for that – Richard Seal Dec 7 '18 at 13:49
1

Here's a better script I used. The reasons are the original script doesn't reference the root variable, and the else clause prints the item name ... which just was found to be null or empty in the if clause.

$sourcePath = "/sitecore/media library/DEV/testmove"
$targetPath = "/sitecore/media library/DEV"
$optionalTemplateNameToMatch = ""
# optional template get children line
# Get-ChildItem $sourcePath -Recurse | Where-Object { $_.TemplateName -match $optionalTemplateNameToMatch } | ForEach-Object {   

Get-ChildItem $sourcePath -Recurse | ForEach-Object { 
    $name = $_.Name
    if(![string]::IsNullOrEmpty($name))
    {
        Move-Item -Path $_.ItemPath -Destination $targetPath;
        Write-Host "Item moved to: "$_.ItemPath;
    }
    else
    {
        Write-Host "Couldn’t move Item because empty name: " $_.Id;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.