Check if items exist in the current language?
Asked Answered
M

7

11

I have a Sitecore solution where there are 3 different languages enabled. On top of the page, there is a link to each language. When you click this link, you get the current page you are standing on, in the selected language.

But not all pages are translated into all languages. So if I am standing on page x in English language, and this page is only available in English and German but not Chinese, then the Chinese link should not be shown.

So the question is - How do I check if the current item has a version of a specific language?

Mannose answered 22/11, 2011 at 18:39 Comment(0)
H
16

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;
}
Headmistress answered 22/11, 2011 at 18:44 Comment(5)
But say i am on the English language and i want to check if there is a german version of the current item - how would i go about that?Mannose
Use "Sitecore.Context.Item.Versions.GetLatestVersion(language)"Threadbare
@Alex, That method will return the item in the language, but you will still need to check to see if the Versions.Count > 0.Headmistress
This isn't working for me... it always tells me that the language version doesnt exist...Imprisonment
For a more efficient way check: Sitecore item languagesPostwar
G
6

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):

  1. it calls LanguageManager.GetLanguages(item.Database));
  2. it adds to this any languages not already added by step 1 by calling item.Database.DataManager.DataSource.GetLanguages(item.ID);
Goles answered 22/11, 2011 at 20:9 Comment(0)
P
1

If you have the context items in a list, use a Linq expression:

List<Item> languageContentItems = 
contentItems.Where(x=> x.Version != null && x.Versions.Count > 0).ToList();

I'm thoroughly confused as to why x.Version.Number wouldn't be the correct syntax vs. using x.Versions.Count because the x.Versions inline-documentation states that it returns all language versions of the item, which would mean that x.Versions.Count should return a count of all versions in all languages, when we really only want to see if the item has a version for the current context language.

Phlebotomy answered 26/9, 2013 at 17:6 Comment(0)
G
1

This works like charm for me:

item.Versions.GetVersions(false).Any();
Gunpowder answered 9/6, 2017 at 11:22 Comment(0)
T
1

don't forget about Fallback option sometimes

 if (item.Versions.Count > 0 && !item.IsFallback)

would work better

Tensile answered 5/6, 2019 at 11:51 Comment(1)
That's a good catch.Yorick
M
0

have a look at this post for a method which returns a list of languages an item has versions in: https://mcmap.net/q/1014465/-get-item-in-all-languages-in-which-it-has-a-version

Melodious answered 8/7, 2015 at 23:56 Comment(0)
R
0

I use the following extension method on Item. This assumes you have an item to start from of course.

  public static bool HasVersionInLanguage(this Item item, Sitecore.Globalization.Language lang)
  {
        return ItemManager.GetVersions(item, lang).Any();
  }

If you don't have the item in memory you could change this to a 'normal' method and pass the ID of the item as a second parameter..

public bool HasVersionInLanguage(ID itemId, Sitecore.Globalization.Language lang)
{
     return ItemManager.GetVersions(item, lang).Any();
}
Ramsden answered 13/11, 2015 at 8:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.