Sitecore is an extendable platform, no doubt about it. For example it allows you to add an external (.aspx) page, to an item:
The page will then show up on your item. But what if you wish to modify the actual Sitecore item from the page? Like in this case:
This is one of the editors for the new Advanced Sitecore Google Maps (WCAG Edition) module. The page allows a user to apply a Google Maps map to a Sitecore item. When the user press “Save map”, the map center and zoom-level is saved to the Sitecore item:
This can only be achieved by hooking into some of Sitecore’s Javascript classes. When the “Save map” button is pressed I hook into the window.parent.scForm Javascript object and call item:load and item:refreshchildren.
All of this can be achieved from code-behind. On the button’s On_Click event I modify the current Sitecore item, and then use RegisterStartupScript to register the Javascript events:
protected void btnSave_Click(object sender, EventArgs e)
{
// Get the Sitecore item to store map center and zoom level
Sitecore.Data.Database db = Sitecore.Configuration.Factory.GetDatabase(Request.QueryString["database"]);
Item item = db.GetItem(Request.QueryString["id"]);
item.Fields["zoom"] = myZoomLevel;
item.Fields["center"] = myCenter;
// Now register the client startup script to be executed
// When page posts back to save the modified Sitecore item
SitecoreHelper.RegisterRefreshScript(Page, Request.QueryString["id"]);
}
The final function, SitecoreHelper.RegisterRefreshScript, is my own helper function that will register the startup script:
public static void RegisterRefreshScript(Page page, string id)
{
StringBuilder message = new StringBuilder();
message.Append("var parentScForm = window.parent.scForm;" + Environment.NewLine);
message.Append(string.Format("parentScForm.postRequest('','','','item:load(id={0})');", id) + Environment.NewLine);
message.Append(string.Format("parentScForm.postRequest('','','','item:refreshchildren(id={0})');", id) + Environment.NewLine);
if (!page.ClientScript.IsStartupScriptRegistered("RefreshItem"))
{
page.ClientScript.RegisterStartupScript(typeof(string), "RefreshItem", message.ToString(), true);
}
}
Thanks to the Sitecore support team to help me out with this one. BTW: It works with both Sitecore 5 and Sitecore 6.



