public override bool CopyItem(ItemDefinition source, ItemDefinition destination, string copyName, ID copyID, CallContext context) { #if DEBUG this.Trace(true, null, source.ID, destination.ID, copyName, copyID, context); #endif // source item is in head provider if (HeadProvider.GetItemDefinition(source.ID, context) != null) { if (HeadProvider.CopyItem(source, destination, copyName, copyID, context)) { return(true); } } var database = context.DataManager.Database; var itemId = source.ID; using (new SecurityDisabler()) { var item = database.GetItem(itemId); Assert.IsNotNull(item, nameof(item)); using (var limit = new RecursionLimit($"{nameof(CopyItem)}-{item.ID}-{destination.ID}", 1)) { if (limit.Exceeded) { return(true); } var defaultOptions = ItemSerializerOptions.GetDefaultOptions(); defaultOptions.AllowDefaultValues = false; // TODO: needs checking defaultOptions.AllowStandardValues = false; defaultOptions.IncludeBlobFields = true; defaultOptions.ProcessChildren = true; // TODO: slow, needs optimization var outerXml = item.GetOuterXml(defaultOptions); var target = database.GetItem(destination.ID); Assert.IsNotNull(target, nameof(target)); target.Paste(outerXml, true, PasteMode.Overwrite); Log.Audit(this, "Default item {0} ({1}) was copied to {2} in head provider", item.Name, item.ID.ToString(), destination.ID.ToString()); return(true); } } }
private Item TransferItem(Item sourceItem, Item destinationItem, string leafName, bool recurse) { using (new ProxyDisabler()) { if (destinationItem.Database.GetTemplate(sourceItem.TemplateID) == null) { WriteError(new ErrorRecord(new TemplateNotFoundException( $"The data contains a reference to a template \"{sourceItem.Template.FullName}\" that does not exist in the destination database.\nYou must transfer the template first."), ErrorIds.TemplateNotFound.ToString(), ErrorCategory.InvalidData, sourceItem)); return(null); } var dic = DynamicParameters as RuntimeDefinedParameterDictionary; var transferOptions = TransferOptions.ChangeId; if (dic != null && dic[TransferOptionsParam].IsSet) { transferOptions = (TransferOptions)dic[TransferOptionsParam].Value; } var outerXml = string.Empty; SitecoreVersion.V72.OrNewer(() => { var options = ItemSerializerOptions.GetDefaultOptions(); options.AllowDefaultValues = transferOptions.HasFlag(TransferOptions.AllowDefaultValues); options.AllowStandardValues = transferOptions.HasFlag(TransferOptions.AllowStandardValues); options.ProcessChildren = recurse; outerXml = sourceItem.GetOuterXml(options); }).Else(() => { outerXml = sourceItem.GetOuterXml(recurse); }); var transferedItem = destinationItem.PasteItem(outerXml, transferOptions.HasFlag(TransferOptions.ChangeId), Force ? PasteMode.Overwrite : PasteMode.Undefined); Event.RaiseEvent("item:transferred", sourceItem, destinationItem); PowerShellLog.Audit("Transfer from database: {0}, to:{1}", AuditFormatter.FormatItem(sourceItem), AuditFormatter.FormatItem(destinationItem)); if (transferedItem.Name != leafName) { transferedItem.Edit(args => transferedItem.Name = leafName); } return(transferedItem); } }
private bool MigrateDefaultItem([NotNull] ItemDefinition itemDefinition, [NotNull] Item item, [NotNull] CallContext context) { Assert.ArgumentNotNull(itemDefinition, nameof(itemDefinition)); Assert.ArgumentNotNull(item, nameof(item)); Assert.ArgumentNotNull(context, nameof(context)); using (var limit = new RecursionLimit($"{nameof(MigrateDefaultItem)}-{item.ID}", 1)) { if (limit.Exceeded) { return(true); } using (new SecurityDisabler()) { var defaultOptions = ItemSerializerOptions.GetDefaultOptions(); defaultOptions.AllowDefaultValues = false; defaultOptions.AllowStandardValues = false; defaultOptions.IncludeBlobFields = true; defaultOptions.ProcessChildren = false; var outerXml = item.GetOuterXml(defaultOptions); var parent = item.Parent; Assert.IsNotNull(parent, nameof(parent)); parent.Paste(outerXml, false, PasteMode.Overwrite); Log.Audit(this, "Default item {0} ({1}) was migrated to head provider", item.Name, item.ID.ToString()); var result = MoveItem(itemDefinition, GetItemDefinition(parent.ID, context), context); if (!result) { Log.Error($"Cannot migrate default item {item.Name} ({item.ID}) to head data provider", this); } else { item.Database.RemoveFromCaches(item.ID); } return(result); } } }
public void TransferMediaLibrary() { var sourcePath = ImportItem.Fields[MediaSourcePath].Value; var destinationPath = ImportItem.Fields[MediaDestinationPath].Value; var sourceItem = FromDB.GetItem(sourcePath); var destinationItem = Database.GetDatabase("master").GetItem(destinationPath); if (sourceItem != null && destinationItem != null) { using (new ProxyDisabler()) { var defaultOptions = ItemSerializerOptions.GetDefaultOptions(); defaultOptions.AllowDefaultValues = false; defaultOptions.AllowStandardValues = false; defaultOptions.ProcessChildren = true; TransferMediaItem(sourceItem, destinationItem, defaultOptions); } } }