October 18, 2011

Retreive the content editor language and item version

Here is a small tip to retreive the language and the version of the item selected in the sitecore content editor when you create a custom field for example.

public class MyCustomField : CustomDropListField
 public string ItemLanguage { get; set; }
 public string ItemVersion { get; set; }  

Thank you to Mister Tony Locatelli from LBi Belgium for this tip :)


  1. Aren't there also properties for the ID of the selected item as well as the ID of the field?

    Remember to retrieve the item from Sitecore.Context.ContentDatabase if it is not null, Sitecore.Context.Database otherwise.

  2. I think the entire list of properties you can expose for automatic setting is as follows (from Sitecore.Shell.Applications.ContentEditor.EditorFormatter):

    ReflectionUtil.SetProperty(editor, "ID", field.ControlID);
    ReflectionUtil.SetProperty(editor, "ItemID", field.ItemField.Item.ID.ToString());
    ReflectionUtil.SetProperty(editor, "ItemVersion", field.ItemField.Item.Version.ToString());
    ReflectionUtil.SetProperty(editor, "ItemLanguage", field.ItemField.Item.Language.ToString());
    ReflectionUtil.SetProperty(editor, "FieldID", field.ItemField.ID.ToString());
    ReflectionUtil.SetProperty(editor, "Source", field.ItemField.Source);
    ReflectionUtil.SetProperty(editor, "ReadOnly", readOnly);
    ReflectionUtil.SetProperty(editor, "Disabled", readOnly);