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.

5 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

Anonymous said...

Searching for trusted branding companies in madison wi? Top Branding Altimeter is a full-service branding agency serving businesses throughout Wisconsin. We specialize in brand identity, logo design, brand strategy, messaging, and visual storytelling that connects with your ideal customers. Whether you're a startup building your brand from scratch or an established company ready for a rebrand, our Madison-area team brings creativity and strategy together. Great branding goes beyond a logo — it's the feeling customers get every time they interact with your business. Let's build something memorable together. Discover why Madison businesses trust us as their go-to branding partner for growth and recognition.