June 18, 2018

Sitecore 8.2 : System.FormatException: Unrecognized Guid format

In Sitecore 8.2.6 and later, you could have a message like: System.FormatException: Unrecognized Guid format. Actual value: ---> System.FormatException: Unrecognized Guid format when you try to use the link database.
For example, if you rebuild the link database. This message is due to some general links not correctly formatted. In the previous versions of Sitecore it could happend that a general link contains an emtpy id attribute or no id at all like this:
<link text="Description" linktype="internal" class="" title="myTitle" target="" querystring="" id="" />
But this is not valid anymore in Sitecore 8.2.6+. To detect those link the easiest solution is running the following Powershell script:
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). Value: $($field.Value)"
                }
            }
        }
    }
}

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

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

ExecOnPath -Path "/sitecore/content/Home"

Write-Host "Done!"
If you also want to fix the empty links at the same time you can add those extra lines into the catch clause:
if($field.Value -eq '<link linktype="internal" />') {                
    $item.Editing.BeginEdit()
    $item[$field.Name] = ""
    $item.Editing.EndEdit()

    Write-Host "Fixed automatically!"
} else {
    Write-Host "Cannot fix it automatically..."
}

You can find the original thread who helped me to develop this script here: https://sitecore.stackexchange.com/questions/11282/rebuilding-link-database-system-formatexception-unrecognized-guid-format

No comments:

Post a Comment