Sitecore default Save command post action usage

Long time no posts about Sitecore Content Editor, but recently I got the following requirement on a project:

Creating a new Save button in Content Editor to perform synchronization – with other items in the tree – of specific fields on the selected item.

My original idea for the implementation was to create a new button based on the existing 💾 Save button, using a custom command implementation. This worked fine…

  • Copied the Save button item in the core database
  • Changed a few field values, including the Click and Key Code field
  • Did the implementation of the custom Save command inherited from the default Save class

I thought that’s it, until I tested and saw that the CommandContext object does not contain the field changes at the point when I want to synchronize with the other items. I thought, maybe the Sitecore caches has not been invalidated yet, but this was not the case. After I checked the database, I saw that the item field changes were not even the database after calling the Execute method of the default Save command.

Solution

After I digged deeper in the original implementation, I found the method which was a base for me to fix the issue – it was the SaveAndClose command. There the close command is called by the Save command postaction. So, the postaction is parameter which expects an command configuration and it’s calling that with the passed through arguments of the command after the save command is finished. So at the end, the following was the solution.

Configuration:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"&gt;
<sitecore>
<commands>
<command name="custom:syncitem" type="MyProject.Commands.SyncItem, MyProject" />
<command name="custom:syncitempostaction" type="MyProject.Commands.SyncItemPostAction, MyProject" />
</commands>
</sitecore>
</configuration>
view raw Commands.config hosted with ❤ by GitHub

The command implementation to call the save command and pass the postaction:

[Serializable]
public class SyncItem : Command
{
public override void Execute(CommandContext context)
{
Context.ClientPage.SendMessage(this, "item:save(postaction=custom:syncitempostaction)");
}
}
view raw SyncItem.cs hosted with ❤ by GitHub

Finally, the post action which is called:

[Serializable]
public class SyncItemPostAction : Command
{
public override void Execute(CommandContext context)
{
var contextItem = context.Items[0];
if (contextItem == null || contextItem.TemplateID != CommonConst.TemplateIdToCheck)
{
return;
}
var newFieldValue = contextItem["Custom Field"];
// custom code to use the new field values
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s