Home » Clones
Category Archives: Clones
Clone Items using the Sitecore Item Web API
Yesterday, I had the privilege to present with Ben Lipson and Jamie Michalski, both of Velir, on the Sitecore Item Web API at the New England Sitecore User Group — if you want to see us in action, check out the recording of our presentation!
Plus, my slides are available here!
During my presentation, I demonstrated how easy it is to customize the Sitecore Item API by adding a custom <itemWebApiRequest> pipeline processor, and a custom pipeline to handle a cloning request — for another example on adding a custom <itemWebApiRequest> pipeline processor, and another pipeline to execute a different custom operation, have a look at this post where I show how to publish Items using the Sitecore Item Web API.
For any custom pipeline you build for the Sitecore Item Web API, you must define a Parameter Object that inherits from Sitecore.ItemWebApi.Pipelines.OperationArgs:
using System.Collections.Generic; using Sitecore.Data.Items; using Sitecore.ItemWebApi.Pipelines; namespace Sitecore.Sandbox.ItemWebApi.Pipelines.Clone { public class CloneArgs : OperationArgs { public CloneArgs(Item[] scope) : base(scope) { } public IEnumerable<Item> Destinations { get; set; } public bool IsRecursive { get; set; } public IEnumerable<Item> Clones { get; set; } } }
I added three properties to the class above: a property to hold parent destinations for clones; another indicating whether all descendants should be cloned; and a property to hold a collection of the clones.
I then created a base class for processors of my custom pipeline for cloning:
using Sitecore.ItemWebApi.Pipelines; namespace Sitecore.Sandbox.ItemWebApi.Pipelines.Clone { public abstract class CloneProcessor : OperationProcessor<CloneArgs> { protected CloneProcessor() { } } }
The above class inherits from Sitecore.ItemWebApi.Pipelines.OperationProcessor which is the base class for most Sitecore Item Web API pipelines.
The following class serves as one processor of my custom cloning pipeline:
using System.Collections.Generic; using Sitecore.Data.Items; using Sitecore.Diagnostics; namespace Sitecore.Sandbox.ItemWebApi.Pipelines.Clone { public class CloneItems : CloneProcessor { public override void Process(CloneArgs args) { Assert.ArgumentNotNull(args, "args"); Assert.ArgumentNotNull(args.Scope, "args.Scope"); Assert.ArgumentNotNull(args.Destinations, "args.Destinations"); IList<Item> clones = new List<Item>(); foreach (Item itemToClone in args.Scope) { foreach (Item destination in args.Destinations) { clones.Add(CloneItem(itemToClone, destination, args.IsRecursive)); } } args.Clones = clones; } private Item CloneItem(Item item, Item destination, bool isRecursive) { Assert.ArgumentNotNull(item, "item"); Assert.ArgumentNotNull(destination, "destination"); return item.CloneTo(destination, isRecursive); } } }
The class above iterates over all Items in scope — these are the Items being cloned — and clones all to the specified destinations (parent Items of the clones).
I then spun up the following class to serve as another processor in my custom cloning pipeline:
using System.Linq; using Sitecore.Diagnostics; using Sitecore.Pipelines; using Sitecore.ItemWebApi.Pipelines.Read; namespace Sitecore.Sandbox.ItemWebApi.Pipelines.Clone { public class SetResult : CloneProcessor { public override void Process(CloneArgs args) { Assert.ArgumentNotNull(args, "args"); Assert.ArgumentNotNull(args.Clones, "args.Clones"); if (args.Result == null) { ReadArgs readArgs = new ReadArgs(args.Clones.ToArray()); CorePipeline.Run("itemWebApiRead", readArgs); args.Result = readArgs.Result; } } } }
The above class delegates to the <itemWebApiRead> pipeline which retrieves the clones from Sitecore, and stores these in the Parameter Object instance for the custom cloning pipeline.
In order to handle custom requests in the Sitecore Item Web API, you must create a custom <itemWebApiRequest> pipeline processor. I put together the following class to handle my cloning operation:
using System; using System.Collections.Generic; using System.Linq; using Sitecore.Data.Items; using Sitecore.Diagnostics; using Sitecore.ItemWebApi; using Sitecore.ItemWebApi.Pipelines.Request; using Sitecore.Pipelines; using Sitecore.Text; using Sitecore.Web; using Sitecore.Sandbox.ItemWebApi.Pipelines.Clone; namespace Sitecore.Sandbox.ItemWebApi.Pipelines.Request { public class ResolveCloneAction : RequestProcessor { public override void Process(RequestArgs args) { Assert.ArgumentNotNull(args, "args"); Assert.ArgumentNotNullOrEmpty(RequestMethod, "RequestMethod"); Assert.ArgumentNotNullOrEmpty(MultipleItemsDelimiter, "MultipleItemsDelimiter"); if (!ShouldProcessRequest(args)) { return; } IEnumerable<Item> destinations = GetDestinationItems(); if (!destinations.Any()) { Logger.Warn("Cannot process clone action: there are no destination items!"); return; } CloneArgs cloneArgs = new CloneArgs(args.Scope) { Destinations = destinations, IsRecursive = DoRecursiveCloning() }; CorePipeline.Run("itemWebApiClone", cloneArgs); args.Result = cloneArgs.Result; } private bool ShouldProcessRequest(RequestArgs args) { // Is this the request method we care about? if (!AreEqualIgnoreCase(args.Context.HttpContext.Request.HttpMethod, RequestMethod)) { return false; } // are multiple axes supplied? if (WebUtil.GetQueryString("scope").Contains(MultipleItemsDelimiter)) { Logger.Warn("Cannot process clone action: multiple axes detected!"); return false; } // are there any items in scope? if (!args.Scope.Any()) { Logger.Warn("Cannot process clone action: there are no items in Scope!"); return false; } return true; } private static bool AreEqualIgnoreCase(string one, string two) { return string.Equals(one, two, StringComparison.CurrentCultureIgnoreCase); } private IEnumerable<Item> GetDestinationItems() { char delimiter; Assert.ArgumentCondition(char.TryParse(MultipleItemsDelimiter, out delimiter), "MultipleItemsDelimiter", "MultipleItemsDelimiter must be a single character!"); ListString destinations = new ListString(WebUtil.GetQueryString("destinations"), delimiter); return (from destination in destinations let destinationItem = GetItem(destination) where destinationItem != null select destinationItem).ToList(); } private Item GetItem(string path) { try { return Sitecore.ItemWebApi.Context.Current.Database.Items[path]; } catch (Exception ex) { Logger.Error(ex); } return null; } private bool DoRecursiveCloning() { bool recursive; if (bool.TryParse(WebUtil.GetQueryString("recursive"), out recursive)) { return recursive; } return false; } private string RequestMethod { get; set; } private string MultipleItemsDelimiter { get; set; } } }
The above class ascertains whether it should handle the request: is the RequestMethod passed via configuration equal to the request method detected, and are there any Items in scope? I also built this processor to handle only one axe in order to keep the code simple.
Once the class determines it should handle the request, it grabs all destination Items from the context database — this is Sitecore.ItemWebApi.Context.Current.Database which is populated via the sc_database query string parameter passed via the request.
Further, the class above detects whether the cloning operation is recursive: should we clone all descendants of the Items in scope? This is also passed by a query string parameter.
I then glued everything together using the following Sitecore configuration file:
<?xml version="1.0" encoding="utf-8"?> <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> <sitecore> <pipelines> <itemWebApiClone> <processor type="Sitecore.Sandbox.ItemWebApi.Pipelines.Clone.CloneItems, Sitecore.Sandbox" /> <processor type="Sitecore.Sandbox.ItemWebApi.Pipelines.Clone.SetResult, Sitecore.Sandbox" /> </itemWebApiClone> <itemWebApiRequest> <processor patch:before="*[@type='Sitecore.ItemWebApi.Pipelines.Request.ResolveAction, Sitecore.ItemWebApi']" type="Sitecore.Sandbox.ItemWebApi.Pipelines.Request.ResolveCloneAction, Sitecore.Sandbox"> <RequestMethod>clone</RequestMethod> <MultipleItemsDelimiter>|</MultipleItemsDelimiter> </processor> </itemWebApiRequest> </pipelines> </sitecore> </configuration>
Let’s clone the following Sitecore Item with descendants to two folders:
In order to make this happen, I spun up the following HTML page using jQuery — no doubt the front-end gurus reading this are cringing when seeing the following code, but I am not much of a front-end developer:
<!DOCTYPE html> <html lang="en"> <head> <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/json3/3.3.2/json3.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/prettify/r224/prettify.js"></script> <link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/prettify/r224/prettify.css" /> </head> <body> <img width="400" style="display: block; margin-left: auto; margin-right: auto" src="/assets/img/clone-all-the-things.jpg" /> <input type="button" id="button" value="Clone" style="width:100px;height:50px;font-size: 24px;" /> <h2 id="confirmation" style="display: none;">Whoa! Something happened!</h2> <div id="working" style="display: none;"><img style="display: block; margin-left: auto; margin-right: auto" src="/assets/img/arrow-working.gif" /></div> <pre id="responseContainer" class="prettyprint" style="display: none;"><code id="response" class="language-javascript"></code></pre> <script type="text/javascript"> $('#button').click(function() { $('#confirmation').hide(); $('#responseContainer').hide(); $('#working').show(); $.ajax({ type:'clone', url: "http://sandbox7/-/item/v1/sitecore/content/Home/Landing Page One?scope=s&destinations=/sitecore/content/Home/Clones|/sitecore/content/Home/Some More Clones&recursive=true&sc_database=master", headers:{ "X-Scitemwebapi-Username":"extranet\\ItemWebAPI", "X-Scitemwebapi-Password":"1t3mW3bAP1"} }).done(function(response) { $('#confirmation').show(); $('#response').html(JSON.stringify(response, null, 4)); $('#working').hide(); $('#responseContainer').show(); }); }); </script> </body> </html>
Plus, please pardon the hard-coded Sitecore credentials — I know you would never store a username and password in front-end code, right? 😉
The above HTML page looks like this on initial load:
I then clicked the ‘Clone’ button, and saw the following:
As you can see, the target Item with descendants were cloned to the destination folders set in the jQuery above:
If you have any thoughts on this, or have other ideas around customizing the Sitecore Item Web API, please share in a comment.
Chain Source and Clone Items Together in Sitecore Workflow
Two months ago, I worked on a project where I had to find a solution to chain source Items and their clones together in Sitecore workflow — don’t worry, the clone Items were “locked down” by being protected so content authors cannot make changes to content on the clones — the clones serve as content copies of their source Items for a multi-site solution in a single Sitecore instance.
After some research, a few mistakes — well, maybe more than a few 😉 — and massive help from Oleg Burov, Escalation Engineer at Sitecore USA, I put together a subclass of Sitecore.Workflows.Simple.Workflow — this lives in Sitecore.Kernel.dll — similar to the following:
using Sitecore.Data.Items; using Sitecore.Workflows; using Sitecore.Workflows.Simple; namespace Sitecore.Sandbox.Workflows.Simple { public class ChainSourceClonesWorkflow : Workflow { public ChainSourceClonesWorkflow(string workflowID, WorkflowProvider owner) : base(workflowID, owner) { } public override WorkflowResult Execute(string commandID, Item item, string comments, bool allowUI, params object[] parameters) { WorkflowResult result = base.Execute(commandID, item, comments, allowUI, parameters); foreach (Item clone in item.GetClones()) { base.Execute(commandID, clone, comments, allowUI, parameters); } return result; } } }
The Execute() method above basically moves the passed Item through to the next workflow state by calling the base class’ Execute() method, and grabs all clones for the passed Item — each are also pushed through to the next workflow state via the base class’ Execute() method.
Workflow instances are created by Sitecore.Workflows.Simple.WorkflowProvider. I created the following class to return an instance of the ChainSourceClonesWorkflow class above:
using Sitecore.Workflows; using Sitecore.Workflows.Simple; namespace Sitecore.Sandbox.Workflows.Simple { public class ChainSourceClonesWorkflowProvider : WorkflowProvider { public ChainSourceClonesWorkflowProvider(string databaseName, HistoryStore historyStore) : base(databaseName, historyStore) { } protected override IWorkflow InstantiateWorkflow(string workflowId, WorkflowProvider owner) { return new ChainSourceClonesWorkflow(workflowId, owner); } } }
I then replaced the “out of the box” WorkflowProvider with the one defined above using the following configuration file:
<?xml version="1.0" encoding="utf-8" ?> <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> <sitecore> <databases> <database id="master"> <workflowProvider type="Sitecore.Workflows.Simple.WorkflowProvider, Sitecore.Kernel"> <patch:attribute name="type">Sitecore.Sandbox.Workflows.Simple.ChainSourceClonesWorkflowProvider, Sitecore.Sandbox</patch:attribute> </workflowProvider> </database> </databases> </sitecore> </configuration>
Let’s take this for a spin!
I first started with a source and clone in a “Draft” workflow state:
Let’s push the source — and hopefully clone 😉 — through to the next workflow state by submitting it:
As you can see, both are “Awaiting Approval”:
Let’s approve them:
As you can see, both are approved:
If you have any thoughts or comments on this, or know of ways to improve the code above, please drop a comment.
Also, keep in mind the paradigm above is not ideal when content authors are able to make content changes to clones which differ from their source Items. In that scenario, it would be best to let source and clone Items’ workflow be independent.
Accept All Notifications on Clones of an Item using a Custom Command in Sitecore
As I was walking along a beach near my apartment tonight, I thought “wouldn’t it be nifty to have a button in the Sitecore ribbon to accept all notifications on clones of an Item instead of having to accept these manually on each clone?”
I immediately returned home, and whipped up the following command class:
using System.Collections.Generic; using System.Linq; using Sitecore.Data.Clones; using Sitecore.Data.Items; using Sitecore.Diagnostics; using Sitecore.Shell.Framework.Commands; namespace Sitecore.Sandbox.Shell.Framework.Commands { public class AcceptAllNotificationsOnClones : Command { public override CommandState QueryState(CommandContext context) { Assert.ArgumentNotNull(context, "context"); IEnumerable<Item> clones = GetClonesWithNotifications(GetItem(context)); if(!clones.Any()) { return CommandState.Hidden; } return CommandState.Enabled; } public override void Execute(CommandContext context) { Assert.ArgumentNotNull(context, "context"); Item item = GetItem(context); IEnumerable<Item> clones = GetClonesWithNotifications(item); if(!clones.Any()) { return; } foreach (Item clone in clones) { AcceptAllNotifications(item.Database.NotificationProvider, clone); } } protected virtual Item GetItem(CommandContext context) { Assert.ArgumentNotNull(context, "context"); return context.Items.FirstOrDefault(); } protected virtual IEnumerable<Item> GetClonesWithNotifications(Item item) { Assert.ArgumentNotNull(item, "item"); IEnumerable<Item> clones = item.GetClones(); if(!clones.Any()) { return new List<Item>(); } IEnumerable<Item> clonesWithNotifications = GetClonesWithNotifications(item.Database.NotificationProvider, clones); if(!clonesWithNotifications.Any()) { return new List<Item>(); } return clonesWithNotifications; } protected virtual IEnumerable<Item> GetClonesWithNotifications(NotificationProvider notificationProvider, IEnumerable<Item> clones) { Assert.ArgumentNotNull(notificationProvider, "notificationProvider"); Assert.ArgumentNotNull(clones, "clones"); return (from clone in clones let notifications = notificationProvider.GetNotifications(clone) where notifications.Any() select clone).ToList(); } protected virtual void AcceptAllNotifications(NotificationProvider notificationProvider, Item clone) { Assert.ArgumentNotNull(notificationProvider, "notificationProvider"); Assert.ArgumentNotNull(clone, "clone"); foreach (Notification notification in notificationProvider.GetNotifications(clone)) { notification.Accept(clone); } } } }
The code in the command above ensures the command is only visible when the selected Item in the Sitecore content tree has clones, and those clones have notifications — this visibility logic is contained in the QueryState() method.
When the command is invoked — this happens through the Execute() method — all clones with notifications of the selected Item are retrieved, and iterated over — each are passed to the AcceptAllNotifications() method which contains logic to accept all notifications on them via the Accept() method on a NotificationProvider instance: this NotificationProvider instance comes from the source Item’s Database property.
I then registered the above command class in Sitecore using the following configuration file:
<?xml version="1.0" encoding="utf-8" ?> <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> <sitecore> <commands> <command name="item:AcceptAllNotificationsOnClones" type="Sitecore.Sandbox.Shell.Framework.Commands.AcceptAllNotificationsOnClones, Sitecore.Sandbox"/> </commands> </sitecore> </configuration>
We need a way to invoke this command. I created a new button to go into the ‘Item Clones’ chunk in the ribbon:
Let’s take this for a test drive!
I first created some clones:
I then changed a field value on one of those clones:
On the clone’s source Item, I changed the same field’s value with something completely different, and added a new child item — the new button appeared after saving the Item:
Now, the clone has notifications on it:
I went back to the source Item, clicked the ‘Accept Notifications On Clones’ button in the ribbon, and navigated back to the clone:
As you can see, the notifications were accepted.
If you have any thoughts on this, please share in a comment.
Rename Sitecore Clones When Renaming Their Source Item
Earlier today I discovered that clones in Sitecore are not renamed when their source Items are renamed — I’m baffled over how I have not noticed this before since I’ve been using Sitecore clones for a while now
I’ve created some clones in my Sitecore instance to illustrate:
I then initiated the process for renaming the source item:
As you can see the clones were not renamed:
One might argue this is expected behavior for clones — only source Item field values are propagated to its clones when there are no data collisions (i.e. a source Item’s field value is pushed to the same field in its clone when that data has not changed directly on the clone — and the Item name should not be included in this process since it does not live in a field.
Sure, I see that point of view but one of the requirements of the project I am currently working on mandates that source Item name changes be pushed to the clones of that source Item.
So what did I do to solve this? I created an item:renamed event handler similar to the following (the one I built for my project is slightly different though the idea is the same):
using System; using System.Collections.Generic; using Sitecore.Data.Items; using Sitecore.Diagnostics; using Sitecore.Events; using Sitecore.Links; using Sitecore.SecurityModel; namespace Sitecore.Sandbox.Data.Clones { public class ItemEventHandler { protected void OnItemRenamed(object sender, EventArgs args) { Item item = GetItem(args); if (item == null) { return; } RenameClones(item); } protected virtual Item GetItem(EventArgs args) { if (args == null) { return null; } return Event.ExtractParameter(args, 0) as Item; } protected virtual void RenameClones(Item item) { Assert.ArgumentNotNull(item, "item"); using (new LinkDisabler()) { using (new SecurityDisabler()) { using (new StatisticDisabler()) { Rename(GetClones(item), item.Name); } } } } protected virtual IEnumerable<Item> GetClones(Item item) { Assert.ArgumentNotNull(item, "item"); IEnumerable<Item> clones = item.GetClones(); if (clones == null) { return new List<Item>(); } return clones; } protected virtual void Rename(IEnumerable<Item> items, string newName) { Assert.ArgumentNotNull(items, "items"); Assert.ArgumentNotNullOrEmpty(newName, "newName"); foreach (Item item in items) { Rename(item, newName); } } protected virtual void Rename(Item item, string newName) { Assert.ArgumentNotNull(item, "item"); Assert.ArgumentNotNullOrEmpty(newName, "newName"); if (!item.Access.CanRename()) { return; } item.Editing.BeginEdit(); item.Name = newName; item.Editing.EndEdit(); } } }
The handler above retrieves all clones for the Item being renamed, and renames them using the new name of the source Item — I borrowed some logic from the Execute method in Sitecore.Shell.Framework.Pipelines.RenameItem in Sitecore.Kernel.dll (this serves as a processor of the <uiRenameItem> pipeline).
If you would like to learn more about events and their handlers, I encourage you to check out John West‘s post about them, and also take a look at this page on the
Sitecore Developer Network (SDN).
I then registered the above event handler in Sitecore using the following configuration file:
<?xml version="1.0" encoding="utf-8" ?> <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> <sitecore> <events> <event name="item:renamed"> <handler type="Sitecore.Sandbox.Data.Clones.ItemEventHandler, Sitecore.Sandbox" method="OnItemRenamed"/> </event> </events> </sitecore> </configuration>
Let’s take this for a spin.
I went back to my source item, renamed it back to ‘My Cool Item’, and then initiated another rename operation on it:
As you can see all clones were renamed:
If you have any thoughts/concerns on this approach, or ideas on other ways to accomplish this, please share in a comment.
Automagically Clone New Child Items to Clones of Parent Items In the Sitecore CMS
“Out of the box”, content authors are prompted via a content editor warning to take action on clones of a source item when a new subitem is added under the source item:
Content authors have a choice to either clone or not clone the new subitem, and such an action must be repeated on all clones of the source item — such might be a daunting task, especially when there are multiple clones for a given source item.
In a recent project, I had a requirement to automatically clone newly added subitems under clones of their parents, and remove any content editor warnings by programmatically accepting the Sitecore notifications driving these warnings.
Although I cannot show you that solution, I did wake up from a sound sleep early this morning with another solution to this problem — it was quite a dream — and this post captures that idea.
In a previous post, I built a utility object that gathers things, and decided to reuse this basic concept. Here, I define a contract for what constitutes a general gatherer:
using System.Collections.Generic; namespace Sitecore.Sandbox.Utilities.Gatherers.Base { public interface IGatherer<T, U> { T Source { get; set; } IEnumerable<U> Gather(); } }
In this post, we will be gathering clones — these are Sitecore items:
using Sitecore.Data.Items; namespace Sitecore.Sandbox.Utilities.Gatherers.Base { public interface IItemsGatherer : IGatherer<Item, Item> { } }
The following gatherer grabs all clones for a given item as they are cataloged in an instance of the LinkDatabase — in this solution we are using Globals.LinkDatabase as the default instance:
using System.Collections.Generic; using System.Linq; using Sitecore.Data.Items; using Sitecore.Diagnostics; using Sitecore.Links; using Sitecore.Sandbox.Utilities.Gatherers.Base; namespace Sitecore.Sandbox.Utilities.Gatherers { public class ItemClonesGatherer : IItemsGatherer { private LinkDatabase LinkDatabase { get; set; } private Item _Source; public Item Source { get { return _Source; } set { Assert.ArgumentNotNull(value, "Source"); _Source = value; } } private ItemClonesGatherer() : this(GetDefaultLinkDatabase()) { } private ItemClonesGatherer(LinkDatabase linkDatabase) { SetLinkDatabase(linkDatabase); } private ItemClonesGatherer(LinkDatabase linkDatabase, Item source) { SetLinkDatabase(linkDatabase); SetSource(source); } private void SetLinkDatabase(LinkDatabase linkDatabase) { Assert.ArgumentNotNull(linkDatabase, "linkDatabase"); LinkDatabase = linkDatabase; } private void SetSource(Item source) { Source = source; } public IEnumerable<Item> Gather() { return (from itemLink in GetReferrers() where IsClonedItem(itemLink) select itemLink.GetSourceItem()).ToList(); } private IEnumerable<ItemLink> GetReferrers() { Assert.ArgumentNotNull(Source, "Source"); return LinkDatabase.GetReferrers(Source); } private static bool IsClonedItem(ItemLink itemLink) { return IsSourceField(itemLink) && !IsSourceItemNull(itemLink); } private static bool IsSourceField(ItemLink itemLink) { Assert.ArgumentNotNull(itemLink, "itemLink"); return itemLink.SourceFieldID == FieldIDs.Source; } private static bool IsSourceItemNull(ItemLink itemLink) { Assert.ArgumentNotNull(itemLink, "itemLink"); return itemLink.GetSourceItem() == null; } private static LinkDatabase GetDefaultLinkDatabase() { return Globals.LinkDatabase; } public static IItemsGatherer CreateNewItemClonesGatherer() { return new ItemClonesGatherer(); } public static IItemsGatherer CreateNewItemClonesGatherer(LinkDatabase linkDatabase) { return new ItemClonesGatherer(linkDatabase); } public static IItemsGatherer CreateNewItemClonesGatherer(LinkDatabase linkDatabase, Item source) { return new ItemClonesGatherer(linkDatabase, source); } } }
Next, we need a way to remove the content editor warning I alluded to above. The way to do this is to accept the notification that is triggered on the source item’s clones.
I decided to use the decorator pattern to accomplish this:
using Sitecore.Data.Clones; using Sitecore.Data.Items; using Sitecore.Diagnostics; namespace Sitecore.Sandbox.Data.Clones { public class AcceptAsIsNotification : Notification { private Notification InnerNotification { get; set; } private AcceptAsIsNotification(Notification innerNotification) { SetInnerNotification(innerNotification); SetProperties(); } private void SetInnerNotification(Notification innerNotification) { Assert.ArgumentNotNull(innerNotification, "innerNotification"); InnerNotification = innerNotification; } private void SetProperties() { ID = InnerNotification.ID; Processed = InnerNotification.Processed; Uri = InnerNotification.Uri; } public override void Accept(Item item) { base.Accept(item); } public override Notification Clone() { return InnerNotification.Clone(); } public static Notification CreateNewAcceptAsIsNotification(Notification innerNotification) { return new AcceptAsIsNotification(innerNotification); } } }
An instance of the above AcceptAsIsNotification class would wrap an instance of a notification, and accept the wrapped notification without any other action.
Notifications we care about in this solution are instances of Sitecore.Data.Clones.ChildCreatedNotification — in Sitecore.Kernel — for a given clone parent, and this notification clones a subitem during acceptance, a behavior we do not want to leverage since we have already cloned the subitem.
I then added an item:added event handler to use an instance of our gatherer defined above to get all clones of the newly added subitem’s parent; clone the subitem under these clones; and accept all instances of Sitecore.Data.Clones.ChildCreatedNotification on the parent item’s clones:
using System; using System.Collections.Generic; using System.Linq; using Sitecore.Data; using Sitecore.Data.Clones; using Sitecore.Data.Items; using Sitecore.Diagnostics; using Sitecore.Events; using Sitecore.Sandbox.Data.Clones; using Sitecore.Sandbox.Utilities.Gatherers; using Sitecore.Sandbox.Utilities.Gatherers.Base; namespace Sitecore.Sandbox.Events.Handlers { public class AutomagicallyCloneChildItemEventHandler { private static readonly IItemsGatherer ClonesGatherer = ItemClonesGatherer.CreateNewItemClonesGatherer(); public void OnItemAdded(object sender, EventArgs args) { CloneItemIfApplicable(GetItem(args)); } private static void CloneItemIfApplicable(Item item) { if (item == null) { return; } ChildCreatedNotification childCreatedNotification = CreateNewChildCreatedNotification(); childCreatedNotification.ChildId = item.ID; IEnumerable<Item> clones = GetClones(item.Parent); foreach (Item clone in clones) { Item clonedChild = item.CloneTo(clone); RemoveChildCreatedNotifications(Context.ContentDatabase, clone); } } private static void RemoveChildCreatedNotifications(Database database, Item clone) { Assert.ArgumentNotNull(database, "database"); Assert.ArgumentNotNull(clone, "clone"); foreach (Notification notification in GetChildCreatedNotifications(database, clone)) { AcceptNotificationAsIs(notification, clone); } } private static void AcceptNotificationAsIs(Notification notification, Item item) { Assert.ArgumentNotNull(notification, "notification"); Assert.ArgumentNotNull(item, "item"); Notification acceptAsIsNotification = AcceptAsIsNotification.CreateNewAcceptAsIsNotification(notification); acceptAsIsNotification.Accept(item); } private static IEnumerable<Notification> GetChildCreatedNotifications(Database database, Item clone) { Assert.ArgumentNotNull(database, "database"); Assert.ArgumentNotNull(clone, "clone"); return GetNotifications(database, clone).Where(notification => notification.GetType() == typeof(ChildCreatedNotification)).ToList(); } private static IEnumerable<Notification> GetNotifications(Database database, Item clone) { Assert.ArgumentNotNull(database, "database"); Assert.ArgumentNotNull(clone, "clone"); return database.NotificationProvider.GetNotifications(clone); } private static IEnumerable<Item> GetClones(Item item) { ClonesGatherer.Source = item; return ClonesGatherer.Gather(); } private static Item GetItem(EventArgs args) { return Event.ExtractParameter(args, 0) as Item; } private static ChildCreatedNotification CreateNewChildCreatedNotification() { return new ChildCreatedNotification(); } } }
I then plugged in the above in a patch include configuration file:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <sitecore> <events> <event name="item:added"> <handler type="Sitecore.Sandbox.Events.Handlers.AutomagicallyCloneChildItemEventHandler, Sitecore.Sandbox" method="OnItemAdded"/> </event> </events> </sitecore> </configuration>
Let’s try this out, and see how we did.
I created a new child item under my source item:
As you can see, clones were added under all clones of the source item.
Plus, the notification about a new child item being added under the source item is not present:
This notification was automatically accepted in code via an instance of AcceptAsIsNotification within the item:added event handler defined above.
If you can think of any other interesting ways to leverage Sitecore’s item cloning capabilities, please leave a comment.