I want to make the item B (destination) identical to item A (source), without changing the ID of B. I tried with the following:

var sourceXmlString = source.GetOuterXml(false);
var sourceXml = XElement.Parse(sourceXmlString);
sourceXml.SetAttributeValue(XName.Get("id"), destination.ID.ToString());
sourceXml.SetAttributeValue(XName.Get("parentid"), destination.Parent.ID.ToString());

// Paste the final xml to update the existing item
destination.Parent.Paste(sourceXmlString, false, PasteMode.Merge);

but this strategy does not seem to work for Checkbox fields: the source item has an unchecked checkbox field (the standard value of that checkbox filed is checked) but after running the code the destination item shows the checkbox checked.

  • 3
    Why not use powershell to copy the data from the fields of item A to item B?
    – Gatogordo
    Feb 15 '18 at 9:23
  • 1
    If my understanding is correct for your requirement then I would suggest you to use Clone for this. Here is a post that might guide you in doing so. sitecorejunkie.com/2013/04/27/…
    – Amitabh
    Feb 15 '18 at 11:29
  • I have N sites and I need a command that makes the destination site identical to the source site, doing also relink. My code is a customization of Sitecore.SmartCommands - it is executed from a content editor right click menù item (it is in a pipeline). I can't use clones, there will be two independent item instances. Feb 15 '18 at 12:11
  • @Amitabh is correct. For your purposes, cloning seems best fit Feb 15 '18 at 19:30
  • 1
    These sites will be managed by different editors, so they must be independent; if the editor of site A updates an item of site A, the same item in the site B should not be modified. Now we are developing these sites so we are working on a command to copy a site A to a site B without changing item IDs (items are serialized with Unicorn...) Feb 16 '18 at 8:48

You can use Sitecore.Data.Managers.ItemManager.CreateItem method. There is overloaded version that takes item id:

public static Item CreateItem(string itemName, Item destination, ID templateId, ID newId)

then you can copy each field values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.