Thursday, September 20, 2012

Interacting with Content Editor in Sitecore 6

I recently stumbled across an interesting challenge. The issue was to programmatically select an item from within a custom item editor tab.
In Sitecore 5.x it was quite easy to do. All you need to code was this.
Sitecore.Context.ClientPage.SendMessage(this, string.Format("load:item(id={0})", itemId));
Quite simple, huh?

In Sitecore 6 some revamping has been done to Content Editor and now aforementioned approach does not work. The “SendMessage” method does not broadcast the message called from the custom item editor all the way to parental window of Content Editor. The message is sent to an object of custom editor and gets handled by it.

Here is the way it should be done in Sitecore 6.
string msg = string.Format("window.parent.scForm.postRequest(\"\",\"\",\"\",\"item:load(id={0})\")", itemId);
Sitecore.Web.UI.Sheer.SheerResponse.Eval(msg);
Note: “window.parent” in this message is mandatory. Otherwise it won’t work.

P.S. Saving brain-power energy for whomever stumbles upon.

4 comments:

Nick Wesselman said...

Just discovered as well that if you want to do this from a Sheer UI dialog window, it gets even more complex. You need to find the opening window, which needs to be done differently in IE.

SheerResponse.CloseWindow();
var msg = string.Format("var opener = window.opener; if (!opener) opener = window.dialogArguments[0]; opener.scForm.postRequest(\"\",\"\",\"\",\"item:load(id={0})\")", newItem.ID);
SheerResponse.Eval(msg);

O_0 crazy but it works.

Ivan said...

Great find, Nick!
Thanks for sharing it! I bet it can save some folks tons of time.

Phil said...

Hours of searching for how to do this from a Sheer UI dialog finally landed on this. Thanks!

Unknown said...

One of the greatest thing which I like in this blog is the way of sharing some of the useful and usual facts, so I really like it, thanks for sharing.
Web content editor