Check if items exist in the current language?

2019-06-16 04:01发布

问题:

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?

回答1:

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


回答2:

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


回答3:

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.



回答4:

This works like charm for me:

item.Versions.GetVersions(false).Any();


回答5:

have a look at this post for a method which returns a list of languages an item has versions in: https://stackoverflow.com/a/31351810/551811



回答6:

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();
}


回答7:

don't forget about Fallback option sometimes

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

would work better



标签: sitecore