How to find broken links in Sitecore.

Well, recently i take it clone of website and found too much broken links so i want to listed all broken links and fix it one by one.

I search about that and read so many articles so i found a easiest way to identify broken link.

a. First one we can go directly on control panel under reports > Scan the data for broken links.



just click on that and it will open a new popup window.





choose data base and press scan button it will take few seconds or minutes as per database and will show you a list of broken links. for fix it just click on item and fix it.


b. Second way i found by power shell. just go on launchpad and click on PowerShell ISE.







it will open a new popup looks like below images. just copy below script and paste there and press execute button. it will take few seconds to generate broken list.




















#Script Copied..


$database = "master"
$root = Get-Item -Path (@{$true="$($database):\content"; $false="$($database):\content"}[(Test-Path -Path "$($database):\content\home")])

$versionOptions = [ordered]@{
    "Latest"="1"
}

$props = @{
    Parameters = @(
        @{Name="root"; Title="Choose the report root"; Tooltip="Only items in this branch will be returned."; Columns=9},
        @{Name="searchVersion"; Value="1"; Title="Version"; Options=$versionOptions; Tooltip="Choose a version."; Columns="3"; Placeholder="All"}
    )
    Title = "Broken Internal Links Report With Additional Info"
    Description = "Choose the criteria for the report."
    Width = 550
    Height = 300
    ShowHints = $true
}

$result = Read-Variable @props

if($result -eq "cancel"){
    exit
}

filter HasBrokenLink {
    param(
        [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
        [Sitecore.Data.Items.Item]$Item,
        
        [Parameter()]
        [bool]$IncludeAllVersions
    )
    
    if($Item) {
        $brokenLinks = $item.Links.GetBrokenLinks($IncludeAllVersions)
        if($brokenlinks -ne $null -and $brokenlinks.Length -gt 0) {
            $num = 0
            foreach($brokenLink in $brokenLinks) {
                $num = $num + 1
    $brokenRenderingDefinition = Get-Rendering -Item $item -Datasource $brokenLink.TargetPath
    if ($brokenRenderingDefinition) {
        $statusCode = "Missing Target Item"
        $brokenRendering = Get-Item . -Database ($root.Database) -ID $brokenRenderingDefinition.ItemID
        $brokenAdditionalInfo = "Placeholder: " + $brokenRenderingDefinition.Placeholder + " (" +  $brokenRendering.ItemPath + ")"
    }
    else {
        $sourceFieldName = (Get-Item . -Database ($root.Database) -ID $brokenLink.SourceFieldID).Name
        $statusCode = "Unknown " + $sourceFieldName
        $brokenAdditionalInfo = "Unknown " + $sourceFieldName + ": " + $brokenLink.TargetPath
    }
    
                $brokenItem = $brokenLink.GetSourceItem() | Initialize-Item
                
                if (!$brokenItem."Broken Link Field") {
                    Add-Member -InputObject $brokenItem -NotePropertyName "Broken Link Field" -NotePropertyValue ($num.ToString() + ") " + ((Get-Item . -Database ($root.Database) -ID $brokenLink.SourceFieldID).Name))
                }
                else {
                    $brokenItem."Broken Link Field" = $brokenItem."Broken Link Field" + "<br />" + $num.ToString() + ") " + ((Get-Item . -Database ($root.Database) -ID $brokenLink.SourceFieldID).Name)
                }
                
                if (!$brokenItem."Target Url") {
                    Add-Member -InputObject $brokenItem -NotePropertyName "Target Url" -NotePropertyValue ($num.ToString() + ") " + $brokenLink.TargetPath)
                }
                else {
                    $brokenItem."Target Url" = $brokenItem."Target Url" + "<br />" + $num.ToString() + ") " + $brokenLink.TargetPath
                }
                
                if (!$brokenItem."Status Code") {
                    Add-Member -InputObject $brokenItem -NotePropertyName "Status Code" -NotePropertyValue ($num.ToString() + ") " + $statusCode)
                }
                else {
                    $brokenItem."Status Code" = $brokenItem."Status Code" + "<br />" + $num.ToString() + ") " + $statusCode
                }
                
                if (!$brokenItem."Additional Info") {
           Add-Member -InputObject $brokenItem -NotePropertyName "Additional Info" -NotePropertyValue ($num.ToString() + ") " + $brokenAdditionalInfo)
           $brokenItem
                }
                else {
                    $brokenItem."Additional Info" = $brokenItem."Additional Info" + "<br />" + $num.ToString() + ") " + $brokenAdditionalInfo
                }
            }
        }
    }
}

$items = Get-ChildItem -Path $root.ProviderPath -Recurse | HasBrokenLink -IncludeAllVersions (!$searchVersion)

if($items.Count -eq 0){
    Show-Alert "There are no items found which have broken internal links in the current language."
} else {
    $props = @{
        Title = "Broken Internal Links Report With Additional Info"
        InfoTitle = "$($items.Count) items with broken internal links found!"
        InfoDescription = "The report checked for internal links in $(@('all versions','latest versions')[[byte]($searchVersion='1')]) of items."
        MissingDataMessage = "There are no items found which have broken internal links in the current language."
        PageSize = 25
    }
    
    $items |
        Show-ListView @props -Property "Status Code", "Broken Link Field","Target Url", "Additional Info",
            @{Label="Name"; Expression={$_.DisplayName} },
            @{Label="Path"; Expression={$_.ItemPath} },
            "Version",
            "Language",
            @{Label="Updated"; Expression={$_.__Updated} },
            @{Label="Updated by"; Expression={$_."__Updated by"} }
   
}
Close-Window



References:
https://buoctrenmay.com/2018/04/15/sitecore-powershell-extensions-playing-with-broken-links-report/


Happy Coding!

Comments

Popular posts from this blog

Difference between shared, Versioned and Unversioned fileds in sitecore

Setup First Sitecore Helix Example From Scratch (Blank solution)