Last active
August 3, 2021 07:46
-
-
Save michaellwest/f563b0b3597f6c0a75d6 to your computer and use it in GitHub Desktop.
Demonstrates how to relink images using Sitecore PowerShell Extensions. Comment included by Dylan shows how to Remove the link and remove the item.
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
$item = Get-Item -Path "master:\media library\images\koala" | |
$itemNew = Get-Item -Path "master:\media library\images\penguins" | |
$links = Get-ItemReferrer -Item $item -ItemLink | |
foreach($link in $links) { | |
$linkedItem = Get-Item -Path master:\ -ID $link.SourceItemID | |
$itemField = $linkedItem.Fields[$link.SourceFieldID] | |
$field = [Sitecore.Data.Fields.FieldTypeManager]::GetField($itemField) | |
$linkedItem.Editing.BeginEdit() | |
$field.Relink($link, $itemNew) | |
$linkedItem.Editing.EndEdit() | Out-Null | |
} |
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
# Crafted by Dylan | |
function Remove-ItemLink { | |
param([Item]$item) | |
$linkDb = [Sitecore.Globals]::LinkDatabase | |
$links = Get-ItemReferrer -Item $item -ItemLink | |
foreach($link in $links) { | |
$linkedItem = Get-Item -Path master:\ -ID $link.SourceItemID | |
$itemField = $linkedItem.Fields[$link.SourceFieldID] | |
$field = [Sitecore.Data.Fields.FieldTypeManager]::GetField($itemField) | |
$linkedItem.Editing.BeginEdit() | |
$field.RemoveLink($link) | |
$linkedItem.Editing.EndEdit() | |
} | |
} | |
# Example usage: delete items along with their references that have passed a certain date defined by a 'date' field | |
$today = Get-Date | |
$todayIsoDate = [Sitecore.DateUtil]::ToIsoDate($today) | |
$itemsToDelete = Get-Item -Path master: -Query "/sitecore/system/Modules/Mysite/Service Schedules/*[@date < '$($todayIsoDate)']" | |
Foreach($item in $itemsToDelete) { | |
Write-Host "Cleaning up $($itemsToDelete.Paths.Path)" | |
Remove-ItemLink -Item $item | |
Remove-Item -Path $item.Paths.Path | |
} |
Updated to use Get-ItemReferrer
and Out-Null
.
Great script! Can I suggest this so that it works in multilingual scenarios:
$linkedItem = Get-Item -Path master:\ -ID $link.SourceItemID -Language $link.SourceItemLanguage
Just found this gist incredibly useful, along with this stack overflow post: https://sitecore.stackexchange.com/questions/1297/removing-all-references-to-an-item-using-ootb-tools/1307
It has resulted in this function that I'm using:
function Remove-ItemLink {
param([Item]$item)
$linkDb = [Sitecore.Globals]::LinkDatabase
$links = Get-ItemReferrer -Item $item -ItemLink
foreach($link in $links) {
$linkedItem = Get-Item -Path master:\ -ID $link.SourceItemID
$itemField = $linkedItem.Fields[$link.SourceFieldID]
$field = [Sitecore.Data.Fields.FieldTypeManager]::GetField($itemField)
$linkedItem.Editing.BeginEdit()
$field.RemoveLink($link)
$linkedItem.Editing.EndEdit()
}
}
# Example usage: delete items along with their references that have passed a certain date defined by a 'date' field
$today = Get-Date
$todayIsoDate = [Sitecore.DateUtil]::ToIsoDate($today)
$itemsToDelete = Get-Item -Path master: -Query "/sitecore/system/Modules/Mysite/Service Schedules/*[@date < '$($todayIsoDate)']"
Foreach($item in $itemsToDelete) {
Write-Host "Cleaning up $($itemsToDelete.Paths.Path)"
Remove-ItemLink -Item $item
Remove-Item -Path $item.Paths.Path
}
Thanks for sharing @dylanmccurry-sage!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think it would be super useful if we added a couple of commandlets, based on that brilliant bit of code
like Get-ItemReferrers & possibly Relink-Item. You did it again! I didn't think it would be either that succinct or even viable!
Also - great use case scenario!