Broken Internal Links Report With Additional Info.ps1 5.83 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
<#
    .SYNOPSIS
        Lists the items with broken internal links searching all or the latest version in the current language.
        
    .NOTES
        Adam Najmanowicz, Michael West, Daniel Scherrer
        Adapted from the Advanced System Reporter module & Daniel Scherrer's external links checker: 
        https://gist.github.com/daniiiol/143db3e2004afe9a55c1dd3e33048940
		
		Viet Hoang clones /sitecore/system/Modules/PowerShell/Script Library/Content Reports/Reports/Content Audit/Broken Links and updates it for his need
#>

$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) {
Viet Hoang's avatar
Viet Hoang committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
        try {
            $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
                    }
96 97 98
                }
            }
        }
Viet Hoang's avatar
Viet Hoang committed
99 100 101 102
        catch {
            Write-Warning $Error[0]
            Write-Host "Problematic item ID = " $Item.Id
        }
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    }
}

$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