The Daily unexplained: LinkField.TargetItem

Following is a ticket I have written to Sitecore support.  I have been working with Sitecore for six years, and have not run into this behavior.  If I get a resolution, I will update this post.


I was investigating a bug for our client today, and saw a behavior I have not seen before.  I have been able to find no results through Google, or the Sitecore community forums.  Luckily, I was able to provide a content workaround, so this is not a pressing issue.

With the following code
var field = (LinkField)item.Fields[fieldName];
if (field == null)
{
    throw new ArgumentOutOfRangeException($"Field {fieldName} doesn't exist in item {item.Name}");
}

if (string.IsNullOrWhiteSpace(field.Value))
{
    return null;
}

if (!field.IsInternal && !field.IsMediaLink)
    return field.Url;

if (field.TargetItem == null)
{
    return string.Empty;
}

item.Fields[fieldName] is a valid linkField name. The content of that field is a valid Internal Link to a valid Sitecore Item.

In language FR, the code works as expected. In language EN, field.TargetItem returns null. The strange part, is field.TargetID resolves correctly to the item ID, even though . If I change to internal link to link to any other item, it works well in all languages. field is a shared field, so it has the same value in all languages. When I debug, field.Value is identical whether I am in FR or EN ("<link text="" anchor="" linktype="internal" class="" title=""  querystring="" id="{502E222B-CC9F-428B-83EE-54834DA73041}" /> ")

As best I can tell, the problem must be with the target item. I have tried creating/removing new versions of that item in language EN and FR. Same behavior. Only when I link to another item do the code work as expected. I supect it could the result of a custom pipeline somewhere, but I have tried disabling every customized pipeline I could find that the behavior did not change. I expect there is something corrupted about the target item itself.

I have given the client a workaround of deleting the target item and creating a new one and that fixes the problem. I am just submitting this because I have never seen this issue before, and hopefully there is an answer that makes sense I can give the client.
 

Comments