Easy Fix for Broken Sitecore Link Database

If any of you are like me, you may have seen this error message before and been frustrated with trying to solve it.

This error occurs when a general link doesn't have an ID as part of the XML stored in that field. This issue is resolved in Sitecore CMS 9.0 Update-2. It can also be fixed via this patch.

If you are unable to deploy this patch, or just want a quick fix, thankfully finding and resolving this issue can be very simple. In my dealings with this error, I have found that the value stored in the field is almost always "<link linktype="internal" />".

Running a SQL Select statement to find this field value in the Shared, Versioned and Unversioned SQL tables should find the fields where this is stored. You can  then easily navigate to the appropriate Sitecore item and clear that field value.

If you have Sitecore Powershell extensions installed, you can also run this script to find the broken link fields. I found this script here, but I modified it to also check item versions.

Add-Type -AssemblyName Sitecore.Kernel

function Resolve-Error ($ErrorRecord=$Error[0])
{
   $ErrorRecord | Format-List * -Force
   $ErrorRecord.InvocationInfo |Format-List *
   $Exception = $ErrorRecord.Exception
   for ($i = 0; $Exception; $i++, ($Exception = $Exception.InnerException))
   {   "$i" * 80
       $Exception |Format-List * -Force
   }
}

function Test-Fields($item) {
    $anyState = [Sitecore.Links.ItemLinkState]::Any

    $item.Fields.ReadAll()
    for ($j = 0; $j -lt $item.Fields.Count; $j++)
    {
        $field = $item.Fields[$j];
        if ($field -ne $null)
        {
            $field2 = [Sitecore.Data.Fields.FieldTypeManager]::GetField($field)
            if ($field2 -ne $null)
            {
                Try {
                    $linksValidationResult = New-Object -TypeName "Sitecore.Links.LinksValidationResult" -ArgumentList $field, $anyState
                    $field2.ValidateLinks($linksValidationResult);
                }
                Catch {
                   Write-Host "Error on item: $($item.ID). Field: $($field.Name). Path: $($item.Paths.FullPath). Language: $($item.Language.Name). Version: $($item.Version.Number). Value: $($field.Value)"
                }
            }
        }
    }
}

function ExecOnPath($path) {
    Write-Host "Execute on: $path"

    Get-ChildItem -Path "master:$path" -Recurse -Language * -Version * | ForEach-Object { Test-Fields  $_ }
}

ExecOnPath -Path "/sitecore"

Write-Host "Done!"

So that's it. If you ever have issues rebuilding your Sitecore Link Database, I would start by querying SQL or running that Powershell Script. It only takes a few minutes and should find your broken field values right away.

Take care!

-Szymon

Add comment