0

For some reason my code is allowing my version to be displayed as many time as the number of versions under a specific language.

For Example: I have 3 versions of an item under the Chinese Language. The selected version is being displayed now 3 times.

foreach (Sitecore.Globalization.Language language in myitem.Languages)
    {
        Sitecore.Data.Items.Item languageItem = Sitecore.Context.Database.GetItem(myitem.ID, language, Sitecore.Data.Version.Latest);
        foreach (Sitecore.Data.Items.Item version in languageItem.Versions.GetVersions())
        {
            var thelang = language.ToString().ToLower();

            if (thelangauge == "en" || paramamter1 == "" || paramamter1 == null)
            {
                thelangauge = "en-us";
            }
            if (thelangauge == paramamter1)
            {
                if (languageItem.Versions.Count != null)
                {
                <div class="small-12 columns large-12">

                    <h1>@Html.Sitecore().Field("Resource Title", languageItem)</h1>
                    @Html.Sitecore().Field("Description", languageItem)
                </div>
                }
            }
       }
   }

1 Answer 1

2

Your code loops over all the versions in the selected language:

foreach (Sitecore.Data.Items.Item version in languageItem.Versions.GetVersions())

If you want to show only the latest version, just remove that line.

If you want to show all the particular versions of the item, change the variable you use in the loop from languageItem to version:

foreach (Sitecore.Globalization.Language language in myitem.Languages)
{
    Sitecore.Data.Items.Item languageItem = Sitecore.Context.Database.GetItem(myitem.ID, language, Sitecore.Data.Version.Latest);
    foreach (Sitecore.Data.Items.Item version in languageItem.Versions.GetVersions())
    {
        var thelang = language.ToString().ToLower();

        if (thelangauge == "en" || paramamter1 == "" || paramamter1 == null)
        {
            thelangauge = "en-us";
        }
        if (thelangauge == paramamter1)
        {
            <div class="small-12 columns large-12">

                <h1>@Html.Sitecore().Field("Resource Title", version)</h1>
                @Html.Sitecore().Field("Description", version)
            </div>
        }
   }
}
1
  • Yeah that did it...Not sure why I was trying to do that, because I dont have many items that have more that one version. I really appreciate the quick response.
    – Ryan
    Commented Mar 11, 2020 at 16:56

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.