3

I have created a custom Ribbon button that creates a new item. However, when clicking this button the Content Editor switches over to this new item, whereas I expected it to stay on the first item.

Why is it doing this, and how can I prevent this?

For those wondering, this is how I'm creating item from the custom button, with a complete (simplified) Command implementation:

[Serializable]
public class MyCustomCommand : Command {

    public override void Execute(CommandContext context) {

        Assert.ArgumentNotNull(context, "context");
        if (!context.Items.Any())
        {
            return;
        }
        Item item = context.Items[0];

        using (new SecurityDisabler()) {
            Item newItem = GetParentItem(item).Add("New Name", templateItem);
            try {
                newItem.Editing.BeginEdit();
                newItem["Foo"] = "Bar";
                newItem.Editing.EndEdit();
            } catch (Exception) {
                newItem?.Editing.CancelEdit();
            }
        }
    }

    public override CommandState QueryState(CommandContext context)
    {
        Assert.ArgumentNotNull(context, "context");

        if (!context.Items.Any())
        {
            return CommandState.Disabled;
        }

        // Various enabled/disabled check

        return CommandState.Enabled;
    }
}
  • 1
    Can you paste the whole code of your button class? – Marek Musielak Mar 6 '19 at 18:56
  • This post should help you with selecting the correct item. Before adding the new one I would get the ID of the current item and follow the example in the link. stackoverflow.com/questions/15385543/… – RReilley Mar 6 '19 at 20:45
  • @MarekMusielak Whole code of button class added – GTHvidsten Mar 7 '19 at 9:05
2

You can solve this by introducing Progress Window.

Break your Execute method into two separate methods like below.

Execute method:

public override void Execute(CommandContext context) {

    Assert.ArgumentNotNull(context, "context");
    if (!context.Items.Any())
    {
        return;
    }
    Item item = context.Items[0];

    try
    {
        ProgressBox.Execute(
             "Creating New Item", 
             "Title",
             "Applications/32x32/refresh.png",
             CreateItem,
             new object[] { templateItem }));
    }
    catch (Exception ex)
    {
        Sitecore.Diagnostics.Log.Error("Error!", ex, this);
    }        
}

CreateItem method:

public void CreateItem(params object[] parameters) {

    using (new SecurityDisabler()) {
        Item newItem = GetParentItem(item).Add("New Name", templateItem);
        try {
            newItem.Editing.BeginEdit();
            newItem["Foo"] = "Bar";
            newItem.Editing.EndEdit();
        } 
        catch (Exception)
        {
            newItem?.Editing.CancelEdit();
        }
    }

    ///other custom logic
}

If you wish to show some message window after creation, take a look in this thread: In the Content Editor, how can I have a Sitecore alert popup code run after the ProgressBox Execute code has finished running?

  • This works, but do you know why Content Editor redirects to new items created without using ProgressBox? – GTHvidsten Mar 7 '19 at 11:53
  • It is the standard behaviour as you are working with different Context Item to currently selected one – Peter Procházka Mar 7 '19 at 12:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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