Skip to content
June 4, 2012 / kiranpatils

Would you like to retrieve Item related information from your custom field?

Challenge:

We faced nice challenge, when we were working on creating our Custom field and during development of that field; we wanted to have Content Editor’s current selected ItemLanguage.

We tried a couple of options from our side. But all in vain. Then we raised it with Sitecore Support and they gave us a solution. This solved our issue. But so fewer people in this Sitecore world will know about it and it will amaze you how it works! (That’s why I love Sitecore mainly due to its architecture!)

Solution:

Please try to do the following:

1) Add the following property to your class:

public string ItemLanguage { get; set; }

2) Change the string

Item itemSource = Sitecore.Context.ContentDatabase.Items[this.ItemID, Language.Parse(ItemLanguage)];

You are amazed right? And not able to belive right? How this code will work? Belive me try it out and it will work! What you tried? It works, right? Eager to know why?

Same eagerness we had, and Sitecore support guys answered our question. Basically this property is set using reflection by the Content Editor when it renders the field control, as well as ID, ItemID, ItemVersion, ItemLanguage, FieldID, Source and other preoperties, if they are included in your field control.

Then found this link — http://sitecoreblog.blogspot.in/2011/10/retreive-content-editor-language-and.html which says that same thing. And from comment section of this post. Found that someone posted code from reflector which makes our understanding more clear, how it works!

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

We all learnt Reflection in college days right? Have you thought it can be used in such a way? Honestly, I’ve never thought of it! (I’m sure so as you!)

Happy Item Information reading! 🙂

PS : Special thanks to Roman Chernyk (Sitecore Support) for providing this suggestion and Devashri who did all this investigation and worked on this challenge!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: