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?
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.
Benchmarking performance
First Load using GetVersions - when data is not from cache
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.
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.
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: