Sitecore – CopyTo() not working?

Are you getting the below error while copying Sitecore Media Item(s) from one node to another. For example – from Canada Site to Australia Site?
13664 19:28:32 ERROR Project.WCMS.Extension.Shell.Applications.CopyProductData.SitecoreExtensions.CopyField: Error while saving copiedMediaItem in Copy Product Data Wizard [System.InvalidOperationException: An instance of Sitecore.Data.Items.Item was null. Additional information: Failed to find the root item "/".
at Sitecore.Diagnostics.Assert.IsNotNull(Object value, String message)
at Sitecore.Web.UI.HtmlControls.DataContext.GetState(Item& root, Item& folder, Item[]& selected)
at Sitecore.Web.UI.HtmlControls.DataContext.GetState(Item& root, Item& folder)
at Sitecore.Web.UI.HtmlControls.DataContext.ItemCreatedNotification(Object sender, ItemCreatedEventArgs args)
at Sitecore.Data.Events.ItemCreatedDelegate.Invoke(Object sender, ItemCreatedEventArgs args)
at Sitecore.Events.Event.RaiseItemCreated(Object sender, ItemCreatedEventArgs args)
at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
at Sitecore.Data.Engines.DataEngine.RaiseCreated(EventHandler`1 handlers, Item item, SafeDictionary`2 visited)
at Sitecore.Data.Engines.DataEngine.DataEngine_CopiedItem(Object sender, ExecutedEventArgs`1 e)
at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
at Sitecore.Data.Engines.EngineCommand`2.RaiseEvent[TArgs](EventHandler`1 handlers, Func`2 argsCreator)
at Sitecore.Data.Engines.DataCommands.CopyItemCommand.Executed()
at Sitecore.Data.Engines.EngineCommand`2.Execute()
at Sitecore.Data.Engines.DataEngine.CopyItem(Item source, Item destination, Boolean deep, String copyName, ID copyId)
at Sitecore.Data.Managers.ItemProvider.CopyItem(Item source, Item destination, Boolean deep, String copyName, ID copyId)
at Sitecore.Data.Managers.ItemManager.CopyItem(Item source, Item destination, Boolean deep, String copyName, ID copyId)
at Project.WCMS.Extension.Shell.Applications.CopyProductData.SitecoreExtensions.CopyField(Item item, Field sourceField) in \Project.WCMS.Extension\Shell\Applications\CopyProductData\CopyProductDataWizard.cs:line 982]

Solution:

It is very simple but it took some time for me to figure out what actually is causing CopyTo fail. It was working for most of items but not for few Media Item(s). After looking at the code of CopyTo with a very good friend .NET Reflector. We came to know that it is calling root item of the Context Site and we were using CopyTo in the Content Editor Mode where the context site is Shell.

So, the solution is simple — You just need to switch the context site — We didn’t dig deeper into why it isn’t working for few because of timing constraints but we got the problem and a solution to proceed. If you know the reason then please share in comment — By that it will help us to understand and hopefully it might help anyone.
This worked for you then don’t forget do share to help others.

Happy Sitecoring!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.