Get Item in all languages in which it has a version

五迷三道 提交于 2019-12-01 04:41:10
Mark Cassidy

You need to check the versioncount of the returned items. There are probably better ways to achieve this, but following your own code example, it would look something like this:

Item tempItem = Sitecore.Context.Database.GetItem(tempID);
foreach (var itemLanguage in tempItem.Languages)
{
    var item = tempItem.Database.GetItem(tempItem.ID, itemLanguage);
    if (item.Versions.Count > 0)
    {
        // do something. If there is no "ru" version, this will be 0
    }
}

I ran an experiment to get a list of languages with the number of versions for items with varying numbers of children to check how expensive this is on the system.

The script below returns a list with the names of languages which have versions for the item.

public static List<string> LanguagesWithContent(this Item item)
{
    var result = ItemManager.GetContentLanguages(item).Select(lang => new {
        lang.Name,
        Versions = ItemManager.GetVersions(item, lang).Count
        //this is better than db.GetItem(item.ID, lang).Versions.Count      
    }).Where(t => t.Versions > 0).Select(t => t.Name).ToList();
    return result;
}

Benchmarking performance

First Load using GetVersions - when data is not from cache

  • 0.7298ms to get Version Counts with 8 language versions. Time to load item: 9.3041ms. Langs: es-ES, es-MX, sv-SE, en, it-IT, pt-BR, fr-FR, de-DE
  • 0.0448ms to get Version Counts with 7 language versions. Time to load item: 2.0039ms. Langs: es-ES, es-MX, en, it-IT, fr-FR, de-DE, ro-RO
  • 0.0334ms to get Version Counts with 6 language versions. Time to load item: 3.145ms. Langs: es-ES, es-MX, en, it-IT, fr-FR, de-DE
  • 0.0307ms to get Version Counts with 5 language versions. Time to load item: 1.5976ms. Langs: es-MX, en, it-IT, fr-FR, de-DE
  • 0.0353ms to get Version Counts with 4 language versions. Time to load item: 10.2764ms. Langs: en, de-DE, ja-JP, da
  • 0.0258ms to get Version Counts with 3 language versions. Time to load item: 1.9507ms. Langs: en, de-DE, ja-JP
  • 0.0193ms to get Version Counts with 2 language versions. Time to load item: 2.0533ms. Langs: en, de-DE
  • 0.0201ms to get Version Counts with 1 language versions. Time to load item: 4.7689ms. Langs: en

Repeat 1st Load experiment, using GetItem instead of GetVersions

conclusion: it's better to use ItemManager.GetVersions(item, lang).Count than db.GetItem(item.ID, lang).Versions.Count to get the list of language versions an item contains.

  • 3.4936ms to get Version Counts with 8 language versions. Time to load item: 8.9118ms. Langs: es-ES, es-MX, sv-SE, en, it-IT, pt-BR, fr-FR, de-DE
  • 0.9966ms to get Version Counts with 7 language versions. Time to load item: 1.6489ms. Langs: es-ES, es-MX, en, it-IT, fr-FR, de-DE, ro-RO
  • 1.0875ms to get Version Counts with 6 language versions. Time to load item: 3.1538ms. Langs: es-ES, es-MX, en, it-IT, fr-FR, de-DE
  • 0.5891ms to get Version Counts with 5 language versions. Time to load item: 1.4402ms. Langs: es-MX, en, it-IT, fr-FR, de-DE
  • 2.2096ms to get Version Counts with 4 language versions. Time to load item: 9.8701ms. Langs: en, de-DE, ja-JP, da
  • 0.9255ms to get Version Counts with 3 language versions. Time to load item: 2.5175ms. Langs: en, de-DE, ja-JP
  • 0.7606ms to get Version Counts with 2 language versions. Time to load item: 2.2407ms. Langs: en, de-DE
  • 1.9032ms to get Version Counts with 1 language versions. Time to load item: 5.0206ms. Langs: en

    Subsequent loads - item is cached already

    using ItemManager.GetVersions(item, lang) in this case. GetItem should in theory give similar results since subsequent loads leverage cache.

  • 0.569ms to get Version Counts with 8 language versions. Time to load item: 0.0372ms. Langs: es -ES, es-MX, sv-SE, en, it-IT, pt-BR, fr-FR, de-DE
  • 0.0429ms to get Version Counts with 7 language versions. Time to load item: 0.0475ms. Langs: es-ES, es-MX, en, it-IT, fr-FR, de-DE, ro-RO
  • 0.025ms to get Version Counts with 6 language versions. Time to load item: 0.0247ms. Langs: es-ES, es-MX, en, it-IT, fr-FR, de-DE
  • 0.0239ms to get Version Counts with 5 language versions. Time to load item: 0.0201ms. Langs: es-MX, en, it-IT, fr-FR, de-DE
  • 0.0342ms to get Version Counts with 4 language versions. Time to load item: 0.0216ms. Langs: en, de-DE, ja-JP, da
  • 0.0258ms to get Version Counts with 3 language versions. Time to load item: 0.0197ms. Langs: en, de-DE, ja-JP
  • 0.0228ms to get Version Counts with 2 language versions. Time to load item: 0.019ms. Langs: en, de-DE
  • 0.0228ms to get Version Counts with 1 language versions. Time to load item: 0.0178ms. Langs: en
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!