11

How do I show an error style dialog (i.e. with red error logo and the expand text) from a content editor command?

I've tried to use SheerResponse.ShowError(). The content editor greys out like it does for regular modals / dialogs, but no error message appears. I'm using Sitecore 8.0 update 3. My code is below:

public class MyCommand: Command
{
    public override void Execute(CommandContext context)
    {
        var nameValueCollection = new NameValueCollection();
        if (context.Items.Length == 1)
        {
            Item item = context.Items[0];
            if (item != null
                && item.TemplateID.ToString() == Templates.RedirectSettings.TemplateId)
            {
                nameValueCollection["id"] = item.ID.ToString();
                Sitecore.Context.ClientPage.Start(this, "DoStuff", nameValueCollection);
            }
        }
    }

    protected void DoStuff(ClientPipelineArgs args)
    {
        var itemId = args.Parameters["id"];
        if (!args.IsPostBack)
        {
            SheerResponse.Confirm("Do stuff now?");
            args.WaitForPostBack();
        }
        else if (args.Result == "yes")
        {
            var everythingOk = //do some stuff and check if successfull..
            if (everythingOk)
            {
                SheerResponse.Alert("Everything worked ok", true, "It's ok");
            }
            else
            {
                //something went wrong
                SheerResponse.ShowError("My short description", "Detailed description...");
                args.WaitForPostBack();
            }
        }
    }

4 Answers 4

10

As far as I remember, Sitecore 8 support for Sheer has been dropped in favour of SPEAK.

Here you can find more information on how to convert a button from Sheer to SPEAK.

In essence you have to use JavaScript for that and create a function according to the template below:

define(["sitecore"], function (Sitecore) {
    Sitecore.Commands.YourCommandName = {
        canExecute: function (context) {
            return true; // check if the command can execute
        },
        execute: function (context) {
            var id = context.currentContext.itemId;
            var lang = context.currentContext.language;
            var ver = context.currentContext.version;

            // here comes your code

            alert("Error!"); // use javascript here to show info or ask for confirmaiton
        }
    };
});

Then connect it to the presentation of the button. Refer to this link for the detailed information.

More info about Speak can be found here.

3
  • So does this mean that to invoke C# code I would need a controller returning a JSON result for success/failure? Oct 5, 2016 at 10:32
  • 1
    Sitecore provides means to execute code by RequestProcessors. You can see how it's done in the linked article (first link from the answer) Oct 5, 2016 at 11:52
  • 1
    Just a small correction, Sheer has not been dropped. It's still there and can be used as needed, depending on which part of the application you're connecting into. Oct 10, 2016 at 22:21
5

I have decompile the Sitecore Code and below is an example how the popup appeared:

This screenshot is from the Sitecore.Shell.Applications.Dialogs.Upload namespace

enter image description here

So, you can make use of the Context.ClientPage.ClientResponse.Alert("Specify at least one file to upload.");

The code can take up to 3 parameters. You can specify the response and the header. For the response, you can pass 1 or blank. The 1 represent True

3

try:

Context.ClientPage.ClientResponse.Alert("Your error message");
0

For me I was trying to create yes/no/cancel dialog box on client page. One of my observation was this, When I added the command link on Sitecore control panel, it was not opening the dialog box at all. But added the command link as a button in the ribbon bar worked for me. I shared the details on this article “How to create and use Sitecore command” by Alok Bhatt https://link.medium.com/qoqctiM4Q5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.