Item Links
Examples for managing item referrers maintained by the Link Database.

Relink Item

Example: The following changes the image linked on an item to a new image. Originally posted here.
1
$item = Get-Item -Path "master:\media library\images\koala"
2
$itemNew = Get-Item -Path "master:\media library\images\penguins"
3
$links = Get-ItemReferrer -Item $item -ItemLink
4
foreach($link in $links) {
5
$linkedItem = Get-Item -Path master:\ -ID $link.SourceItemID
6
$itemField = $linkedItem.Fields[$link.SourceFieldID]
7
$field = [Sitecore.Data.Fields.FieldTypeManager]::GetField($itemField)
8
9
$linkedItem.Editing.BeginEdit()
10
$field.Relink($link, $itemNew)
11
$linkedItem.Editing.EndEdit() | Out-Null
12
}
Copied!

Remove Item Link

Example: The following removes an item link followed by removing the item. Originally posted here.
1
# Crafted by Dylan
2
3
function Remove-ItemLink {
4
param([Item]$item)
5
6
$linkDb = [Sitecore.Globals]::LinkDatabase
7
8
$links = Get-ItemReferrer -Item $item -ItemLink
9
10
foreach($link in $links) {
11
$linkedItem = Get-Item -Path master:\ -ID $link.SourceItemID
12
$itemField = $linkedItem.Fields[$link.SourceFieldID]
13
$field = [Sitecore.Data.Fields.FieldTypeManager]::GetField($itemField)
14
15
$linkedItem.Editing.BeginEdit()
16
$field.RemoveLink($link)
17
$linkedItem.Editing.EndEdit()
18
}
19
}
20
21
# Example usage: delete items along with their references that have passed a certain date defined by a 'date' field
22
$today = Get-Date
23
$todayIsoDate = [Sitecore.DateUtil]::ToIsoDate($today)
24
$query = "/sitecore/system/Modules/Mysite/Service Schedules/*[@date < '$($todayIsoDate)']"
25
$itemsToDelete = Get-Item -Path master: -Query $query
26
27
foreach($item in $itemsToDelete) {
28
Write-Host "Cleaning up $($itemsToDelete.Paths.Path)"
29
Remove-ItemLink -Item $item
30
Remove-Item -Path $item.Paths.Path
31
}
Copied!
Last modified 2yr ago
Export as PDF
Copy link