Search code examples
sitecoresitecore6

How to display a popup when saving a Sitecore item?


When saving a Sitecore item I am trying to display a popup to interact with the user. Depending on data that they have changed I may display a series of 1 or 2 popups asking them if they want to continue. I have figured out how to tap in to the OnItemSaving pipeline. That is simple. What I can't figure out is how to display a popup and react to the input from the user. Right now I am thinking that I should be using the Sitecore.Context.ClientPage.ClientResponse object somehow. Here is some code that shows what I am trying to do:

public class MyCustomEventProcessor
{
    public void OnItemSaving(object sender, EventArgs args)
    {
      if([field criteria goes here])
      {
        Sitecore.Context.ClientPage.ClientResponse.YesNoCancel("Are you sure you want to continue?", "500", "200");
        [Based on results from the YesNoCancel then potentially cancel the Item Save or show them another dialog]
      }
    }
}

Should I be using a different method? I see that there is also ShowModalDialog and ShowPopUp and ShowQuestion, etc. I can't seem to find any documentation on these. Also I am not even sure if this is the correct way to do something like this.


Solution

  • The process goes something like this (I should note that I've never tried this from the item:saving event, however, I believe it should work):

    1. Within the item:saving event, invoke a dialog processor in the client pipeline, and pass it a set of arguments.
    2. The processor does one of two things; displays the dialog, or consumes the response.
    3. When a response is received, the processor consumes it and there you can perform your actions.

    Here is an example that demonstrates the steps above:

    private void StartDialog()
    {
        // Start the dialog and pass in an item ID as an argument
        ClientPipelineArgs cpa = new ClientPipelineArgs();
        cpa.Parameters.Add("id", Item.ID.ToString());
    
        // Kick off the processor in the client pipeline
        Context.ClientPage.Start(this, "DialogProcessor", cpa);
    }
    
    protected void DialogProcessor(ClientPipelineArgs args)
    {
        var id = args.Parameters["id"];
    
        if (!args.IsPostBack)
        {
            // Show the modal dialog if it is not a post back
            SheerResponse.YesNoCancel("Are you sure you want to do this?", "300px", "100px");
    
            // Suspend the pipeline to wait for a postback and resume from another processor
            args.WaitForPostBack(true);
        }
        else
        {
            // The result of a dialog is handled because a post back has occurred
            switch (args.Result)
            {
                case "yes":
    
                    var item = Context.ContentDatabase.GetItem(new ID(id));
                    if (item != null)
                    {
                        // TODO: act on the item
    
                        // Reload content editor with this item selected...
                        var load = String.Format("item:load(id={0})", item.ID);
                        Context.ClientPage.SendMessage(this, load);
                    }
    
                    break;
    
                case "no":
    
                    // TODO: cancel ItemSavingEventArgs
    
                    break;
    
                case "cancel":
                    break;
            }
        }
    }