Exemple #1
0
        public virtual ISourceItem DeserializeItem(ISerializedItem serializedItem, bool ignoreMissingTemplateFields)
        {
            Assert.ArgumentNotNull(serializedItem, "serializedItem");

            var typed = serializedItem as SitecoreSerializedItem;

            if (typed == null)
            {
                throw new ArgumentException("Serialized item must be a SitecoreSerializedItem", "serializedItem");
            }

            try
            {
                var options = new LoadOptions {
                    DisableEvents = true, ForceUpdate = true, UseNewID = false
                };

                return(new SitecoreSourceItem(ItemSynchronization.PasteSyncItem(typed.InnerItem, options, true)));
            }
            catch (ParentItemNotFoundException ex)
            {
                string error = "Cannot load item from path '{0}'. Probable reason: parent item with ID '{1}' not found.".FormatWith(serializedItem.ProviderId, ex.ParentID);

                throw new DeserializationException(error, ex);
            }
            catch (ParentForMovedItemNotFoundException ex2)
            {
                string error = "Item from path '{0}' cannot be moved to appropriate location. Possible reason: parent item with ID '{1}' not found.".FormatWith(serializedItem.ProviderId, ex2.ParentID);

                throw new DeserializationException(error, ex2);
            }
        }
        public virtual ISourceItem Deserialize(ISerializedItem serializedItem, bool ignoreMissingTemplateFields)
        {
            if (ignoreMissingTemplateFields)
            {
                throw new NotSupportedException("The Sitecore serialization engine does not support ignoring missing fields.");
            }

            try
            {
                var options = new LoadOptions {
                    DisableEvents = true, ForceUpdate = true, UseNewID = false
                };

                return(new SitecoreSourceItem(ItemSynchronization.PasteSyncItem(((SitecoreSerializedItem)serializedItem).InnerItem, options, true)));
            }
            catch (ParentItemNotFoundException ex)
            {
                string error = "Cannot load item from path '{0}'. Probable reason: parent item with ID '{1}' not found.".FormatWith(serializedItem.ProviderId, ex.ParentID);

                throw new DeserializationException(error, ex);
            }
            catch (ParentForMovedItemNotFoundException ex2)
            {
                string error = "Item from path '{0}' cannot be moved to appropriate location. Possible reason: parent item with ID '{1}' not found.".FormatWith(serializedItem.ProviderId, ex2.ParentID);

                throw new DeserializationException(error, ex2);
            }
        }
Exemple #3
0
 private static Item CreateNewItem(Item parent, LoadOptions loadOptions, SyncItem syncItem, Sitecore.ItemWebApi.Context context)
 {
     Item itm = null;
     Assert.ArgumentNotNull(parent, "parent");
     Assert.ArgumentNotNull(loadOptions, "loadOptions");
     Assert.ArgumentNotNull(syncItem, "syncItem");
     try
     {
         itm = ItemSynchronization.PasteSyncItem(syncItem, loadOptions, true);
         if (itm != null)
         {
             if (Common.Functions.IsMediaItem(context) && context.HttpContext != null && context.HttpContext.Request.Files != null)
             {
                 MediaItem mediaItem = Common.Functions.UpdateMediaItem(itm, context.HttpContext);
                 if (mediaItem != null)
                 {
                     itm = mediaItem.InnerItem;
                 }
             }
         }
     }
     catch(Exception ex)
     {
         throw ex;
     }
     return itm;
 }
Exemple #4
0
        private void Parse(DirectoryInfo directoryInfo)
        {
            var fileInfos = directoryInfo.GetFiles("*.item");

            foreach (var fileInfo in fileInfos)
            {
                var streamReader = new StreamReader(fileInfo.FullName);

                var syncItem = SyncItem.ReadItem(new Tokenizer(streamReader), true);
                var options  = new LoadOptions {
                    DisableEvents = true, ForceUpdate = true, UseNewID = false
                };

                ItemSynchronization.PasteSyncItem(syncItem, options, true);
            }

            foreach (var info in directoryInfo.GetDirectories())
            {
                Parse(info);
            }
        }
        /// <summary>
        /// Loads a specific item from disk
        /// </summary>
        private Item DoLoadItem(string path, AdvancedLoadOptions options, out ItemLoadResult loadResult)
        {
            Assert.ArgumentNotNullOrEmpty(path, "path");
            Assert.ArgumentNotNull(options, "options");

            if (File.Exists(path))
            {
                using (TextReader fileReader = new StreamReader(File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read)))
                {
                    LogLocalized("Loading item from path {0}.", new object[]
                    {
                        PathUtils.UnmapItemPath(path, options.Root)
                    });

                    bool disabledLocally = ItemHandler.DisabledLocally;
                    try
                    {
                        ItemHandler.DisabledLocally = true;
                        Item result = null;
                        try
                        {
                            var serializedItem = SyncItem.ReadItem(new Tokenizer(fileReader));

                            _itemsProcessed++;
                            if (_itemsProcessed % 500 == 0 && _itemsProcessed > 1)
                            {
                                options.Progress.ReportStatus(string.Format("Processed {0} items", _itemsProcessed), MessageType.Debug);
                            }

                            if (options.Preset.Exclude.MatchesTemplate(serializedItem.TemplateName))
                            {
                                options.Progress.ReportStatus(string.Format("[SKIPPED] {0}:{1} because the preset excluded its template name, but the item was on disk", serializedItem.DatabaseName, serializedItem.ItemPath), MessageType.Warning);

                                loadResult = ItemLoadResult.Skipped;
                                return(null);
                            }

                            if (options.Preset.Exclude.MatchesTemplateId(serializedItem.TemplateID))
                            {
                                options.Progress.ReportStatus(string.Format("[SKIPPED] {0}:{1} because the preset excluded its template ID, but the item was on disk", serializedItem.DatabaseName, serializedItem.ItemPath), MessageType.Warning);

                                loadResult = ItemLoadResult.Skipped;
                                return(null);
                            }

                            if (options.Preset.Exclude.MatchesId(serializedItem.ID))
                            {
                                options.Progress.ReportStatus(string.Format("[SKIPPED] {0}:{1} because the preset excluded it by ID, but the item was on disk", serializedItem.DatabaseName, serializedItem.ItemPath), MessageType.Warning);


                                loadResult = ItemLoadResult.Skipped;
                                return(null);
                            }

                            if (options.Preset.Exclude.MatchesPath(path))
                            {
                                options.Progress.ReportStatus(string.Format("[SKIPPED] {0}:{1} because the preset excluded it by path, but the item was on disk", serializedItem.DatabaseName, serializedItem.ItemPath), MessageType.Warning);

                                loadResult = ItemLoadResult.Skipped;
                                return(null);
                            }

                            var newOptions = new LoadOptions(options);

                            // in some cases we want to force an update for this item only
                            if (!options.ForceUpdate && ShouldForceUpdate(serializedItem, options.Progress))
                            {
                                options.Progress.ReportStatus(string.Format("[FORCED] {0}:{1}", serializedItem.DatabaseName, serializedItem.ItemPath), MessageType.Info);
                                newOptions.ForceUpdate = true;
                            }

                            result = ItemSynchronization.PasteSyncItem(serializedItem, newOptions, true);

                            loadResult = ItemLoadResult.Success;
                        }
                        catch (ParentItemNotFoundException ex)
                        {
                            result     = null;
                            loadResult = ItemLoadResult.Error;
                            string error =
                                "Cannot load item from path '{0}'. Probable reason: parent item with ID '{1}' not found.".FormatWith(
                                    PathUtils.UnmapItemPath(path, options.Root), ex.ParentID);

                            options.Progress.ReportStatus(error, MessageType.Error);

                            LogLocalizedError(error);
                        }
                        catch (ParentForMovedItemNotFoundException ex2)
                        {
                            result     = ex2.Item;
                            loadResult = ItemLoadResult.Error;
                            string error =
                                "Item from path '{0}' cannot be moved to appropriate location. Possible reason: parent item with ID '{1}' not found."
                                .FormatWith(PathUtils.UnmapItemPath(path, options.Root), ex2.ParentID);

                            options.Progress.ReportStatus(error, MessageType.Error);

                            LogLocalizedError(error);
                        }
                        return(result);
                    }
                    finally
                    {
                        ItemHandler.DisabledLocally = disabledLocally;
                    }
                }
            }

            loadResult = ItemLoadResult.Error;

            return(null);
        }