LibrarySites.Banner

Load or Reload an Item in the Sitecore ASP.NET CMS

The Change item's icon from item:saved event and Redirect to item via custom editor threads on the Sitecore Developer Network (SDN) SDN prompted me to write this blog post about techniques for causing the Content Editor user interface in the Sitecore ASP.NET CMS to load an item. This isn’t meant to be a comprehensive example, but to provide some general pointers to developers working trying to meet the common objective in a variety of contexts.

There are several types of situations where you may want the content editor to load or reload an item or refresh the content tree:

  • When you programmatically change the value of a field in the background
  • When you programmatically move, delete, rename or otherwise manipulate an item
  • When the user takes an action in a custom user interface such as a custom editor
  • When relatives of the item have changed

Whether loading or reloading an item, you take the same approach: send a item:load or item:refreshchildren message to the client to load an item or refresh the tree. For example, the NewsMover Sitecore Shared Source project sends the item:refreshchildren message after moving an item.

You can construct and send an item:load or item:refreshchildren message as follows. In some cases you might need to use the Sitecore.Context.ClientPage.ClientResponse.Timer() method rather than the Sitecore.Context.ClientPage.SendMessage() method.

  Sitecore.Data.Items.Item myItem = Sitecore.Context.Item; //TODO: set to the appropriate item
  string load = String.Concat( new object[] { "item:load(id=", myItem.ID, ",language=", myItem.Language, ",version=", myItem.Version, ")" });
  Sitecore.Context.ClientPage.SendMessage(this, load);
  String refresh = String.Format("item:refreshchildren(id={0})", myItem.Parent.ID);
  Sitecore.Context.ClientPage.ClientResponse.Timer(refresh, 2);

To get this to work in some user interfaces such as custom editors without loading the Content Editor recursively into what is probably an iframe within the Content Editor, you might need to set the onclick attribute of the anchor ( tag) that the user clicks to something like:

  javascript:scForm.getParentForm().postRequest(\"\",\"\",\"\",\"item:load(id="")\")

I'm actually not sure if all of that quote escaping is necessary. For more information about custom editors, see the Client Configuration Cookbook on SDN .

The Redirect to item via custom editor thread on the SDN forums links to http://sdn.sitecore.net/SDN5/Forum/PostAttachment.aspx?PostID=33783 , which provides a working example. Note also Mark Ursino 's suggestion that you might sometimes want to open a new content editor window by constructing a URL with a template such as:

  /sitecore/shell/sitecore/content/Applications/Content Editor.aspx
  ?id={0}&fo={0}&la={1}&ver={2}

That's a single line, the same value for id and fo, and I'm not exactly sure about the ampersand escaping.

If you have any additional information about loading or reloading items or the content tree, such as information about how this works in the Page Editor rather than the Content Editor, please comment on this blog post.

  • Hi, John.   I've been trying to find a way to reload an item while in the Page Editor. We have an article item that changes the URL according to the title of the article. Whenever the title is changed and saved, we get a 404 page. We tried the techniques described in this post, but it doesn't seem to work in the Page Editor. How do we make this work in the Page Editor?

  • @Karl: I think you would want some way to cause URLs to include IDs rather than paths when this happens, but I assume  you would in general want to use friendly URLs rather than paths except in this case. To always use IDs, it should be possible to override the LinkProvider, but this seems overkill. Maybe you could trap save and refresh to the ID URL rather than the current URL. I think this would need to be in JavaScript rather than on the server, but I am not exactly sure where to look. I would suggest filing a support case, but I will investigate if I find time.

  • @Karl: It looks like something already reloads the page when you save in the Page Editor, it just reloads the old URL. I couldn't easily find where this happens, but it might be the thing to override. If you can't find it (which I couldn't), you might try adding a processor to the saveUI pipeline - something like "if the user is in the Page Editor, and if that field changed, redirect the browser to the new path" (and terminate the response, assuming the Sitecore reload of the old URL would occur after that pipeline executes). Just a thought; not sure that would work.

  • @John: I've added a processor in the SaveUI pipline. I can't seem to find a way to detect if a user is Page Editor inside this pipeline. Sitecore.Context.PageMode.IsPageEditor = false. Is it possible to do the desired action in the webedit:save event?

  • @Karl: Sorry, I do not have an explanation for that or time to investigate, but it is not what I would expect. I am not aware of a webedit:save event; I wonder if you could use item:moved instead. If the info is not available to the pipeline, I would not expect it in the event either though. I would try Sitecore.Context.PageMode.IsPageEditorEditing

  • @John:I really enjoy your post,but i would like to confirm related to one thing that,i have one requirement"i need to move an item from one parent node to another parent node at certain condition(by verifying  date) on that particular item in side same database like master or web".how could i proceed  with code customization.Thanks.

  • @Bansidhar: I would not move an item in a publishing target such as the Web database - I would move it in the Master database and then publish that change to the target.  For these types of things, I like to use the rules engine. If an item save operation is the only thing that should trigger the logic, then I think you could use an ItemSaved rule. If other things should trigger the logic, then you may need other types of rules, but hopefully they could use the same conditions and actions. One nice thing about the rules engine is that it encapsulates the logic and allows for configuration, so that you can use that logic from other places.  www.sitecore.net/.../intercepting-item-updates-with-sitecore.aspx  This is relatively old, but could also provide some pointers:  marketplace.sitecore.net/.../News_mover.aspx  Whichever way you choose, please let me know if you need any assistance getting started.

  • Hi John, I have been trying your code to reload the item after I have done certain modification to it. The item reloads but also give Save confirm popup "Do you want to save the changes to the item". the changes have already been saved ,but it still pops me this warning message. is there any way to stop it.

  • @Asif: Are you showing the Sitecore standard template fields, and if so, do you get the same when you hide those fields? I have seen this issue pop up and disappear for years, mostly when fields defined in the standard template are visible. What version of Sitecore? Do you have any custom field types in the data template for that item? Does it happen for all items, or only some items?

  • /sitecore/shell/sitecore/content/Applications/Content Editor.aspx   ?id={0}&fo={0}&la={1}&ver={2} what's parameter  "fo"   meaning ?