Question

I've created a custom field for sitecore Item, this custom field just creates a new Item with name specified under some specific folder. I do it on save (appropriate condition in onLoad method for custom field control):

if (!Sitecore.Context.ClientPage.IsEvent)
{
  //blah-blah-blah
}
else
{
  TemplateItem template = db.GetTemplate(TemplatePath);
  Item parentItem = db.Items[ItemsRootPath];
  var newItem = parentItem.Add("item name", template);
}

Item is creating successfully, but content editor automatically opens this item when I save a parent item (item that contains this custom field).

The question is: how I can avoid it and create this item in background, without displaying it to user?

Thanks, A

Was it helpful?

Solution

You can disable Notifications which will prevent the new item being loaded like so:

Client.Site.Notifications.Disabled = true;
TemplateItem template = db.GetTemplate(TemplatePath);
Item parentItem = db.Items[ItemsRootPath];
var newItem = parentItem.Add("item name", template);
Client.Site.Notifications.Disabled = false;

I tested this by adding a child to the item that I was saving and resulted in the tree not being updated as well. I fixed that by adding this line after the notification enable.

Sitecore.Context.ClientPage.SendMessage(this, string.Format("item:refreshchildren(id={0})", ItemID));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top