Check if items exist in the current language?
To see if there is a version of the current item you can do this: Sitecore.Context.Item.Versions.Count > 0
[updated for comment]
I don't claim that this is the most efficient way to determine if an item has a version in a language, but this will work:
bool hasVersion = HasLanguageVersion(Sitecore.Context.Item, "en");
private bool HasLanguageVersion(Sitecore.Data.Items.Item item, string languageName)
{
var language = item.Languages.FirstOrDefault(l => l.Name == languageName);
if (language != null)
{
var languageSpecificItem = global::Sitecore.Context.Database.GetItem(item.ID, language);
if (languageSpecificItem != null && languageSpecificItem.Versions.Count > 0)
{
return true;
}
}
return false;
}
You can retrieve a collection (LanguageCollection
) of an items content languages (ie. the languages for which the item has content).
LanguageCollection collection = ItemManager.GetContentLanguages(Sitecore.Context.Item);
foreach (var lang in collection)
{
var itm = Sitecore.Context.Database.GetItem(Sitecore.Context.Item.ID,lang);
if(itm.Versions.Count > 0)
{
Response.Write("Found language " + lang + "<br />");
}
}
Hope this helps :)
NB: Add a comment dude.. please dont just make random edits to my answer. This is the height of rudeness.
Edit: Correcting .. Turns out the method doesn't take into account versions of that language existing.---
to clarify, ItemManager.GetContentLanguages does not get you the list of languages on a given item. It gives the list of all languages you have opted to include in your environment. Under the hood, it does 2 things (based on decompiled code for sitecore 7.2):
- it calls LanguageManager.GetLanguages(item.Database));
- it adds to this any languages not already added by step 1 by calling item.Database.DataManager.DataSource.GetLanguages(item.ID);