0

Is it possible to removed broken links using sitecore powershell. I checked the items link in the Navigation > Links and some of them has "Not found: master {GUID}".

enter image description here

Here is the sample code I have that will fetch the links but it will an error because the linkedItem doesn't exist.

$page = Get-Item -Path $SearchParentPath
$links = Get-ItemReferrer -Item $page -ItemLink

# Iterate through referring items
foreach ($link in $links) {
    $linkedItem = Get-Item -Path "/sitecore" -ID $link.SourceItemID
    if ($linkedItem -eq $null) {
        Write-Host "Broken Link $($link.SourceItemID)"
    }
}
1

1 Answer 1

1

Instead of calling Get-Item, use Test-Path command.

It will return either true or false depending on whether the item with given ID exists:

$page = Get-Item -Path $SearchParentPath
$links = Get-ItemReferrer -Item $page -ItemLink

# Iterate through referring items
foreach ($link in $links) {
    $exists = Test-Path $link.SourceItemID.ToString()
    if (!$exists) {
        Write-Host "Broken Link $($link.SourceItemID)"
    }
}
4
  • Thank you for this. It removed the error but I am still not sure on how to remove the link itself. I also tried looping the items from GetBrokenLinks.
    – Elaidein
    Nov 13, 2023 at 9:03
  • There are multiple scenarios here. It can be a LinkField - the easiest would be to link to another item, e.g. to Home item or remove the link completely. It can be also a multi list so you would need to remove only single item from the list then.
    – Marek Musielak
    Nov 13, 2023 at 9:19
  • Hello. I know this thread is kind of old, but have you managed to fix this Problem? I mean, how can i remove the link completely or link it to home?
    – CodeHacker
    Mar 8 at 7:48
  • Just use Rebuild Link Database from Control Panel
    – Marek Musielak
    Mar 8 at 7:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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