2

Hey I can't get my head around this and haven't worked to much in Sitecore but can anyone help? The code is getting the children of a item putting them into an array and then running another section depending on the items in the array. What I want is to remove any items that doesn't have the current language version. Here is what I've got. Even telling me I'm on the wrong track will help Thanks.

    @{
    var detailPages = Sitecore.Context.Item.Children.AsStronglyTypedCollectionOf<IDetailPageItem>().ToArray();

    @foreach (var detailPage in detailPages)
    {
        if (Sitecore.Context.Item.Versions.Count == 0)
        {
            Array.remove(detailPages, detailPage);
        }
    }


    if (!RenderingContext.IsEditing && detailPages.Length == 0)
    {
        return;
    }
}

<div class="cardsmenu">
    <div class="wrapper">
        @if (RenderingContext.IsEditing && detailPages.Length == 0)
        {
            <p class="edit-only">No detail page children exist. This list will be invisible when viewed outside edit mode.</p>
        }
        @foreach (var detailPage1 in detailPages)
        {
            @Html.Partial("/Areas/mytestsite/Views/Shared/DetailPageLink.cshtml", detailPage1)
        }
    </div>
</div>

1 Answer 1

2

You're on the right track. The problem you have is; you're checking Item for Versions. Any versions - not related to your current Context.Language.

While this is still not ideal, you could loop around your array like this:

@foreach (var detailPage in detailPages)
{
    var currentItem = Sitecore.Context.Database.GetItem(detailPage.ID, Sitecore.Context.Language);

    if (currentItem == null || !currentItem.Versions.Any())
    {
        Array.remove(detailPages, detailPage);
    }
}

(may need minor syntax adjustment, I'm typing this as I go).

See also: https://stackoverflow.com/questions/8231987/check-if-items-exist-in-the-current-language

(but please don't use Count > 0, use Any())

Edited to add

I just noticed you tagged your question with language-fallback. This further complicates things a bit since - depending on how your solution is set up - Sitecore might actually always give you an item version (using fallback) and therefore mess up the above code.

If this is the case, you may need to wrap your code in a LanguageFallbackItemSwitcher. I've never used this one, but it would look something like this:

    using(new LanguageFallbackItemSwitcher(false)) {
        var currentItem = Sitecore.Context.Database.GetItem(detailPage.ID, Sitecore.Context.Language);
    }

Again, with apologies for possible minor syntax adjustments needed.

See also: Items with fallback language missing in Content Search index

4
  • Thanks so much! I'll give this a go and sort any syntax errors and get back to you.
    – Cmasterd
    Jul 14, 2017 at 11:27
  • I have been clearing up the sytax errors but for LanguageFallbackItemSwitcher what namespace do you us for that?
    – Cmasterd
    Jul 24, 2017 at 11:23
  • It sits in Sitecore.Data.LanguageFallback
    – Mark Cassidy
    Jul 24, 2017 at 11:39
  • After clearing it up works grand thanks for your help.
    – Cmasterd
    Jul 26, 2017 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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