We want to create custom item duplicate functionality just like OOTB Duplicate option provided by Sitecore.
Has anyone implemented such functionality using either powershell or .net code. How we can do it?
First of all you need to duplicate the item /sitecore/content/Applications/Content Editor/Context Menues/Default/Duplicate from core db
On Message field you have item:duplicate command.
You need to create your custom command and to set it to the new created item. You need to register you custom command in a patch config file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<sitecore>
<!-- COMMANDS -->
<commands>
<command name="item:customduplicate" type="YourNameSpace.CustomDuplicate,YourAssembly" />
</commands>
</sitecore>
</configuration>
The item:duplicate command looks like:
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using System;
namespace YourNamespace
{
/// <summary>Represents the Duplicate command.</summary>
[Serializable]
public class Duplicate : Command
{
/// <summary>Executes the command in the specified context.</summary>
/// <param name="context">The context.</param>
public override void Execute(CommandContext context)
{
if (context.Items.Length != 1 || context.Items[0] == null)
return;
Sitecore.Shell.Framework.Items.Duplicate(context.Items[0]);
}
/// <summary>Queries the state of the command.</summary>
/// <param name="context">The context.</param>
/// <returns>The state of the command.</returns>
public override CommandState QueryState(CommandContext context)
{
Error.AssertObject((object) context, nameof (context));
if (context.Items.Length != 1)
return CommandState.Disabled;
Item parent = context.Items[0].Parent;
return parent == null || !parent.Access.CanCreate() || !context.Items[0].Access.CanWriteLanguage() ? CommandState.Disabled : base.QueryState(context);
}
}
}
Based on your comments about custom duplication logic as well as duplication of items in buckets, it sounds like you might need/want to also hook into some of the events / processors related to item duplication.
<uiDuplicateItem>
<processor type="Sitecore.Shell.Framework.Pipelines.DuplicateItem,Sitecore.Kernel" method="CheckPermissions" mode="on"/>
<processor method="CustomMethodName" mode="on" type="Custom.Project.Web.Pipelines.DuplicateItem.CustomDuplicateItem, Custom.Project.Web" patch:source="Custom.config"/>
<processor type="Sitecore.Buckets.Pipelines.UI.ItemDuplicate, Sitecore.Buckets" method="Execute" mode="on" patch:source="Sitecore.Buckets.config"/>
<processor type="Sitecore.Shell.Framework.Pipelines.DuplicateItem,Sitecore.Kernel" method="Execute" mode="on"/>
</uiDuplicateItem>
More info on events can be found in this older documentation here. Inspecting your ShowConfig.aspx will also reveal other possibilities with events. For example:
<!-- Event hook after duplicating an item into an item bucket.
Args: (ClientPipelineArgs) Args -> -->
<event name="item:bucketing:duplicating" patch:source="Sitecore.Buckets.config"/>
For code examples, simply decompile the binaries mentioned in the settings.