0

Is there a way to move an item in a bucket (based on some rules) after it has been renamed? Basically the following behaviour is wanted:

  1. Rename the item
  2. If bucket exists ---> move it there
  3. If bucket not exist ---> create bucket ---> move it there

The thing is that I created a command with custom button, added it in the context menu, but first I have to show the Rename modal and only after I move the item. Any help is welcome.

Thanks

LE:

I've done it with a custom item:renamed event handler, but the UI is freezing until the logic is executed, and I am unable to make it async. Is there a workaround or solution for this? My code looks like below:

public void OnItemRenamed(object sender, EventArgs args)
    {
        if (Event.ExtractParameter(args, 0) is Item item && item != null)
        {
            if (item.TemplateID.ToString().Equals(TemplateIdConstants.GlobalPageCollectionItem))
            {
                IlikeToMoveItMoveIt(item);
            }
         }
    }

    private void IlikeToMoveItMoveIt(Item itemToBeMoved)
    {
        try
        {
            var helper = new Helper();

            var destination = helper.GetDestination(itemToBeMoved);

            itemToBeMoved.MoveTo(destination);
        }

        catch (Exception ex)
        {
            SitecoreReal.Diagnostics.Log.Error(string.Format("Error in move/publish for {0} - exception: {1}", itemToBeMoved.ID, ex.Message), string.Empty);
        }
    }
2
  • 1
    Hi, have you tried to implement a custom item: renamed event handler. This will fire after an item is renamed in Sitecore. Commented Aug 30, 2021 at 13:58
  • Hi Alina. Thanks. It worked.
    – iusmar
    Commented Aug 30, 2021 at 16:02

1 Answer 1

2

By default Sitecore eventhandlers are executed synchronously. I think in your case it depends on what you are trying to achieve in sitecore content/experience editor, if you need to have the change visible immediately to the editor or if it is ok to handle it async after the item has been renamed.

If you have a long running operation you can use instead a sitecore job, you can take a look at more details on https://stackoverflow.com/questions/24693179/are-sitecore-events-handled-synchronously. If you run tasks asynchronous you also need to take into consideration updating the content tree view if the editor is on the item that will be moved.

Your Answer

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

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