Rename and Delete SPE scripts for SXA Custom Module/Folder Structure
Recently, working with Sitecore SXA, as a complement to Clone rendering script, I felt it would be good to have a couple of additional Sitecore PowerShell Extensions scripts. These scripts are for Renaming/deleting custom component folder structure rather than performing manually and missing a couple of places:
5. Next time, open the needed script from library and execute to see the result of rename/delete in SXA toolbox/content tree:
Sitecore 10.3.1 uses SXA 10.3.0 but this script is purely Sitecore and PowerShell so, must work in older versions of Sitecore/SXA too - looks for a specific path and renames or deletes if it is there and doesn't disturb other items.
1. Open PowerShell ISE from Sitecore launchpad:
2. Copy each script separately (below, one for rename and another for delete) to the window
3. Use Save As option in ribbon and provide credential:
Custom Renderings renamed to Uplift Renderings and visible in SXA toolbox:
#Rename-Item
############################################################
$findValue = "Custom Renderings"
$replaceValue = "Uplift Renderings"
$sourcePath1 = Get-Item "/sitecore/layout/Renderings/Feature/$findValue" -ErrorAction SilentlyContinue
RenameItem -item $sourcePath1 -find $findValue -replace $replaceValue
$sourcePath2 = Get-Item "/sitecore/templates/Branches/Feature/$findValue" -ErrorAction SilentlyContinue
RenameItem -item $sourcePath2 -find $findValue -replace $replaceValue
$sourcePath3 = Get-Item "/sitecore/templates/Feature/$findValue" -ErrorAction SilentlyContinue
RenameItem -item $sourcePath3 -find $findValue -replace $replaceValue
$sourcePath4 = Get-Item "/sitecore/layout/Layouts/Feature/$findValue" -ErrorAction SilentlyContinue
RenameItem -item $sourcePath4 -find $findValue -replace $replaceValue
$sourcePath5 = Get-Item "/sitecore/layout/Placeholder Settings/Feature/$findValue" -ErrorAction SilentlyContinue
RenameItem -item $sourcePath5 -find $findValue -replace $replaceValue
$sourcePath6 = Get-Item "/sitecore/media library/Feature/$findValue" -ErrorAction SilentlyContinue
RenameItem -item $sourcePath6 -find $findValue -replace $replaceValue
#adjust site and tenant name below as per need
$sourcePath7 = Get-Item "/sitecore/content/my tenant/mysite/Presentation/Available Renderings/$findValue" -ErrorAction SilentlyContinue
RenameItem -item $sourcePath7 -find $findValue -replace $replaceValue
$sourcePath8 = Get-Item "/sitecore/system/Settings/Feature/$findValue" -ErrorAction SilentlyContinue
RenameItem -item $sourcePath8 -find $findValue -replace $replaceValue
function RenameItem($item,$find,$replace)
{
if ($item.Name -ne $null){
$name = $item.Name
$newName = $name.Replace($find,$replace)
$ItemPathOnly = $item.FullPath.Substring(0, $item.FullPath.LastIndexOf("/"))
$destinationPath = "master:$ItemPathOnly/$newName"
if(Test-Path -Path $destinationPath)
{
Write-Host "master:$ItemPathOnly/$name exists already - cannot rename";
}
else
{
$item.Editing.BeginEdit()
$item.Name = $newName
$item.Editing.EndEdit()
Write-Host "Item renamed to $destinationPath";
return $item;
}
}
else
{
Write-Host "Item not found to replace"
}
}
#############################################################
#Delete-Item
#############################################################
$findValue = "Uplift Renderings"
$sourcePath1 = Get-Item "/sitecore/layout/Renderings/Feature/$findValue" -ErrorAction SilentlyContinue
Delete-Item -item $sourcePath1
$sourcePath2 = Get-Item "/sitecore/templates/Branches/Feature/$findValue" -ErrorAction SilentlyContinue
Delete-Item -item $sourcePath2
$sourcePath3 = Get-Item "/sitecore/templates/Feature/$findValue" -ErrorAction SilentlyContinue
Delete-Item -item $sourcePath3
$sourcePath4 = Get-Item "/sitecore/layout/Layouts/Feature/$findValue" -ErrorAction SilentlyContinue
Delete-Item -item $sourcePath4
$sourcePath5 = Get-Item "/sitecore/layout/Placeholder Settings/Feature/$findValue" -ErrorAction SilentlyContinue
Delete-Item -item $sourcePath5
$sourcePath6 = Get-Item "/sitecore/media library/Feature/$findValue" -ErrorAction SilentlyContinue
Delete-Item -item $sourcePath6
#adjust site and tenant name below as per need
$sourcePath7 = Get-Item "/sitecore/content/my tenant/mysite/Presentation/Available Renderings/$findValue" -ErrorAction SilentlyContinue
Delete-Item -item $sourcePath7
$sourcePath8 = Get-Item "/sitecore/system/Settings/Feature/$findValue" -ErrorAction SilentlyContinue
Delete-Item -item $sourcePath8
function Delete-Item($item)
{
$folderItem = $item
Get-ChildItem -Path $folderItem.FullPath -ErrorAction SilentlyContinue |
ForEach-Object {
Write-Host "Deleting: " $_.Name
$_ | Remove-Item -Recurse -ErrorAction SilentlyContinue
}
if($folderItem -ne $null) {Remove-Item $folderItem.FullPath}
}
#############################################################
#Delete-Items
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Originally written to clear item footprint from different folders like, SXA clone rendering script. | |
#Generic script, could be case-sensitive, you might want to get rid of Contains check | |
$path="/sitecore" | |
$match='Copy of Carousel' | |
$items = Get-ChildItem -Path $path -Recurse | |
foreach($item in $items) { | |
if ($item.Name.Equals($match) -or $item.Name.Contains($match)) { | |
#Write-Host("Removing " + $item.FullPath) | |
$confirmation = Read-Host "Are you sure You want To delete? (y/n)" $item.FullPath | |
if ($confirmation -eq 'y') { | |
$item | Remove-Item -Recurse | |
} | |
} | |
} | |
Write-Host("Done") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#reference: https://sitecore.stackexchange.com/questions/33414/rename-items-with-specific-name-in-my-sitecore-content-tree-using-powershell-scr | |
$path="/sitecore" | |
$find='FeatureBlock' | |
$replace='CarouselSlider' | |
$items = Get-ChildItem -Path $path -Recurse | |
foreach($item in $items) { | |
if ($item.Name.Equals($find) -or $item.Name.Contains($find)) { | |
if ($item.Name -ne $null){ | |
$name = $item.Name | |
$newName = $name.Replace($find,$replace) | |
$ItemPathOnly = $item.FullPath.Substring(0, $item.FullPath.LastIndexOf("/")) | |
$destinationPath = "master:$ItemPathOnly/$newName" | |
if(Test-Path -Path $destinationPath) | |
{ | |
Write-Host "master:$ItemPathOnly/$name exists already - cannot rename"; | |
} | |
else | |
{ | |
$item.Editing.BeginEdit() | |
$item.Name = $newName | |
$item.Editing.EndEdit() | |
Write-Host "Item renamed to $destinationPath"; | |
} | |
} | |
else | |
{ | |
Write-Host "Item not found to replace" | |
} | |
} | |
} | |
Write-Host("Done") |
Error:
RenameItem : The term 'RenameItem' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:7 char:1
+ RenameItem -item $sourcePath1 -find $findValue -replace $replaceValue
+ ~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (RenameItem:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Resolution:
SPE did not seem ready although the function was there and name was correct in the PowerShell ISE window. Retry clicking the execute button worked the consecutive time.
Reference:
https://sitecore.stackexchange.com/questions/33414/rename-items-with-specific-name-in-my-sitecore-content-tree-using-powershell-scr
https://blogs.perficient.com/2020/11/17/mass-deleting-items-in-sitecore/
Comments
Post a Comment