-1

I am doing internal redirects. I need to have the target item point back to fallback if no current language is available. Any insights here?

LinkField TOURL = RedirectItem.Fields["To url"];
Item ToUrl = null;
if (TOURL != null)
{
    if (TOURL.TargetItem.IsFallback && (TOURL.IsInternal || TOURL.IsMediaLink) && TOURL.TargetItem != null)
    {
        ToUrl = TOURL.TargetItem; <-- "How can I get this target item to see its fallback?"
    } 
    else if ((TOURL.IsInternal || TOURL.IsMediaLink) && TOURL.TargetItem != null)
    {
        ToUrl = TOURL.TargetItem;
    }
}
0

You could fetch the item in the original language:

Sitecore.Context.Database.GetItem(TOURL.TargetID, TOURL.TargetItem.OriginalLanguage)

OriginalLanguage is a property that returns the original language for the current item. If the item is not a fallback item, it returns the item.Language.

You can still check if it is an actual fallback item before doing this call by using is the IsFallback property you already have in the code.

var targetItem = TOURL.TargetItem;
if ((TOURL.IsInternal || TOURL.IsMediaLink) && targetItem != null)
{
    ToUrl = targetItem.IsFallback ? Sitecore.Context.Database.GetItem(TOURL.TargetID, targetItem.OriginalLanguage) : targetItem;
} 

ps: you might want to change the names "TOURL" and "ToUrl" as well ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.