I recently added a page editor button to refresh that data being used to populate an item in Sitecore. As this is an overwriting operation, it made sense to prompt the user that their data would be overwritten before the update is done in order for them to have the opportunity to cancel and not have their data modified.
I found an excellent article showing how to prompt the user for confirmation before taking an action, but thought I’d just clarify a way of using the same code within the same self-contained command.
In Jeremy Davis’ article he uses a confirmation command that can be used with multiple other commands: here I show how to confirm within the same command. The reason for my doing this with this command is simple, the footprint of the change is tiny: we have a command that is absolutely stand-alone.
The new command button is set up as normal in the Page Editor ribbon :
The command itself can be added to config like this:
<command type="MySite.SC.Commands.WebEdit.ArticleSync, MySite"></command>
When the button is clicked (the command in core is configured as MySite:ArticleSync(id=$ItemID)) the command is executed, the “Execute” method is called.
In the context of my code, this invocation passes the ID of the current item in the CommandContext parameters, where is can be extracted and used to retrieve the current item from Sitecore, from which I can identify the data to be refreshed.
Having set up the data to be refreshed, I then call Context.ClientPage.Start, passing it the name of the method that I would like executed.
When the “Run” method is executed the first time IsPostBack must be false, and so the code falls through to pop up a modal dialog box prompting the user to confirm their action. When they Click “OK” or “Cancel” on the dialog a PostBack occurs, and the flow of control returns to the “Run” method.
On PostBack the code examines the Result property of the dialog, a “yes” means that the “OK” was clicked, and “no” would mean that “Cancel” had been chosen. If the Result is “yes” I clear the list of articles in the item, and re-populate with a new set of articles, if “no”, then no action is taken.
There you have it, a simple variation on the confirmation dialog code, but a minimal command that can be added to the ribbon at will, with practically no footprint.
Here is the code:
namespace MySite.SC.Commands.WebEdit { public class ArticleSync : Command { private Item _currentItem; private List _articleLists; private SiteContext _siteContext; public override void Execute(CommandContext context) { if (_currentItem == null) _currentItem = context.Items[0]; var parameters = new NameValueCollection(); parameters["id"] = _currentItem.ID.ToString(); _articleLists = GetArticleLists(_currentItem); _siteContext = SitecoreManager.ResolveSiteContext(); Context.ClientPage.Start(this, "Run", parameters); } protected void Run(ClientPipelineArgs args) { if (args.IsPostBack) { if (args.Result == "yes") { ClearArticleList(); foreach (var articleList in _articleLists) { var articles = GetArticles(articleList); if (articles.Any()) { PopulateArticles(articles, articleList); } } } } else { const string msg = @"Sync-ing Articles, will refresh Article Lists. Are you sure?"; SheerResponse.Confirm(msg); args.WaitForPostBack(); } } } }