1

Recently I customized Copy item pipeline to run it as a job processor. Everything works fine but one issue is that after copy item is completed, source item still keeps selected instead of target copy item. And copied item is visible only after manually refreshing content tree node.

Can someone suggest what value should I update to fix this issue.

Below is the code which I used to copy item.

var item = args.GetItem();

var destinationId = (args as CopyItemsArgs)?.Parameters["destination"];
var destination = args.GetDatabase().GetItem(destinationId);

item.CopyTo(destination, ItemUtil.GetCopyOfName(destination, item.Name));

3 Answers 3

0

From what I understood, you're copying an item in Content Editor using custom code and in the end, the old item is not selected in the content tree.

I'm not sure exactly how you execute your custom code.

What you can do is to try to send the following messages to Context.ClientPage:

var newItem = item.CopyTo(destination, ItemUtil.GetCopyOfName(destination, item.Name));

// make sure that parent is updated
Context.ClientPage.SendMessage(this, 
  string.Format("item:updated(id={0})", newItem.Parent.ID));

// refresh children of the parent to make sure that new item is there
Context.ClientPage.SendMessage(this, 
  string.Format("item:refreshchildren(id={0})", newItem.Parent.ID));

// select new item
Context.ClientPage.ClientResponse.Timer(
  string.Format("item:load(id={0})", newItem.ID), 1);
1
  • Thanks for your reply. I tried this but I am getting exception- "An error occured System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.AggregateException: One or more exceptions occurred while processing the subscribers to the 'item:copied' event. ---> System.NullReferenceException: Object reference not set to an instance of an object. at Sitecore.Web.UI.Sheer.ClientPage..ctor(HttpContextBase httpContext) at Sitecore.Context.get_ClientPage() at Commented Feb 2, 2021 at 13:12
0

It looks like you're using the CopyTo method to copy the item to the destination. To ensure that the content tree is updated and the focus shifts to the copied item, you can try the following approach:

After the CopyTo method, you can use the Editing.BeginEdit() and Editing.EndEdit() methods to simulate an edit operation and force a refresh of the Content Editor. Here's an updated version of your code:

var item = args.GetItem();
var destinationId = (args as CopyItemsArgs)?.Parameters["destination"];
var destination = args.GetDatabase().GetItem(destinationId);

// Copy the item to the destination
var copiedItem = item.CopyTo(destination, ItemUtil.GetCopyOfName(destination, item.Name));

// Begin and End Edit to refresh the Content Editor 
using (new EditContext(copiedItem))
{
// No need to make any actual changes, just the act of beginning and ending the edit will trigger a refresh
}

// Optionally, select the copied item
Sitecore.Context.ClientPage.SendMessage(this, "item:load(id=" + copiedItem.ID + ")");

In this updated code:

The EditContext is used to simulate an edit operation on the copied item. This should trigger a refresh in the Content Editor, and you should see the copied item selected. Optionally, the item:load message is sent to select the copied item explicitly.

Could you please try this and let me know if it helps?

0

Another approach to ensure that the copied item is selected in the Sitecore content tree after the copy operation is completed is to trigger a client-side refresh of the content tree, which will update the tree to reflect the newly copied item

public class CopyItemProcessor
{
    public void Process(PipelineArgs args)
    {
        Assert.ArgumentNotNull(args, nameof(args));

        if (args is CopyItemsArgs copyItemsArgs)
        {
            var item = copyItemsArgs.GetItem();
            var destinationId = copyItemsArgs.Parameters["destination"];
            var destination = copyItemsArgs.GetDatabase().GetItem(destinationId);

            item.CopyTo(destination, ItemUtil.GetCopyOfName(destination, item.Name));

            // Refresh the content tree to ensure the copied item is visible and selected
            RefreshContentTree();
        }
    }

    private void RefreshContentTree()
    {
        // Execute JavaScript to refresh the content tree
        SheerResponse.Eval("window.top.location.reload()");
    }
}

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.