Search code examples
sitecoresitecore7

Get Item in all languages in which it has a version


Say we have an Item Product(which has versions in en, jp, zh and 0 versions in ru). How can I get this item in en, jp and zh and not in ru.

I tried the below code.

Item tempItem = Sitecore.Context.Database.GetItem(tempID);
foreach (var itemLanguage in tempItem.Languages)
{
  //Do Something
}

Here tempItem.Languages is returning all four languages where I was expecting only three as ru does not have versions.

Is it the correct way to achieve this?


Solution

  • 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
        }
    }