Simple. Not easy – Copy/Move Children – Content Editor

Today i thought to write the first blog in “Simple. Not easy” series. It may seem very basic for the Sitecore experts but i personally found it so much helpful so the Content Editors will be.

Why Copy/Move Children is  really helpful?

Generally Content Editor comes up with full loaded utility commands like Copy, Move, Delete, Delete Sub Items, Duplicate etc. But while working with one of the migration task in a project, I ran into a situation where i was required to copy all child items of particular item into another location. For ex: /sitecore/content/home1 has 50 child items and i need to move it to something /sitecore/content/global/pages. Now only option i find is to copy individual items from /home1 to /pages.

That is a very tedious task. For developers, there might be good options to do it Programmatically or by using Powershell script. But if content editor needs to perform these steps than it is a good idea to create a custom command CopyChildrenTo/MoveChildrenTo similar of Delete Sub Items. What you need to do is extract the code for copyto and moveto and apply the simple logic to copy children instead of selected item.

Create a command

Copy Children

using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Shell.Framework.Commands;
using System;
using System.Collections.Generic;

namespace Demo.POC.Extension
{
    [Serializable]
    public class CopyChildrenTo : Command
    {
        ///
<summary>Executes the command in the specified context.</summary>

        /// <param name="context">The context.</param>
        public override void Execute(CommandContext context)
        {
            CopyChildren(context.Items);
        }

        ///
<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, "context");
            if (context.Items.Length != 1)
                return CommandState.Disabled;
            Item obj = context.Items[0];
            if (obj.Appearance.ReadOnly || !obj.Access.CanRead() || !context.Items[0].Access.CanWriteLanguage())
                return CommandState.Disabled;
            return base.QueryState(context);
        }

        ///
<summary>Copy children.</summary>

        /// <param name="items">The items.</param>
        /// <param name="message">The message.</param>
        public static void CopyChildren(Item[] items)
        {
            Assert.ArgumentNotNull((object)items, "items");
            if (items.Length == 0)
                return;
            List<Item> objList = new List<Item>();
            foreach (Item obj in items)
            {
                objList.AddRange((IEnumerable<Item>)obj.Children.ToArray());
            }
            Sitecore.Shell.Framework.Items.CopyTo(objList.ToArray());
        }
    }
}

Move Children

using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Shell.Framework.Commands;
using System;
using System.Collections.Generic;

namespace Demo.POC.Extension
{
    [Serializable]
    public class MoveChildrenTo : Command
    {
        ///
<summary>Executes the command in the specified context.</summary>

        /// <param name="context">The context.</param>
        public override void Execute(CommandContext context)
        {
            MoveChildren(context.Items);
        }

        ///
<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, "context");
            if (context.Items.Length != 1)
                return CommandState.Disabled;
            Item obj = context.Items[0];
            if (obj.Appearance.ReadOnly || !obj.Access.CanRead() || !context.Items[0].Access.CanWriteLanguage())
                return CommandState.Disabled;
            return base.QueryState(context);
        }

        ///
<summary>Move children.</summary>

        /// <param name="items">The items.</param>
        public static void MoveChildren(Item[] items)
        {
            Assert.ArgumentNotNull((object)items, "items");
            if (items.Length == 0)
                return;
            List<Item> objList = new List<Item>();
            foreach (Item obj in items)
            {
                objList.AddRange((IEnumerable<Item>)obj.Children.ToArray());
            }
            Sitecore.Shell.Framework.Items.MoveTo(objList.ToArray());
        }
    }
}

Config patch
Create a custom config patch file to include these new commands as shown below. Copy this config to anywhere in the website/App_Config/Include folder.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <commands>
      <command  patch:after="*[@name=copyto]" name="item:copychildrento" type="Demo.POC.Extension.CopyChildrenTo,Demo.POC.Extension" />
      <command  patch:after="*[@name=moveto]" name="item:movechildrento" type="Demo.POC.Extension.MoveChildrenTo,Demo.POC.Extension" />
    </commands>
  </sitecore>
</configuration>

Context Menu Item
Create a Context Menu items for both the commands as shown in below image in Core database under /sitecore/content/Applications/Content Editor/Context Menues/Default/Copying.

Copychildren-1

Copychildren-2

In Action

Copychildren-5

Copychildren-6

Any content editor there, Did you find this helpful? Or share other such issues you face in day to day activity.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s