Exemple #1
0
        private static OutputMap CreateOutputMap(Sitecore.Data.Items.Item item, OutputMap parentMap)
        {
            var outputMap = new OutputMap();

            outputMap.ParentMap = parentMap;
            var outputMapCustomItem = new OutputMapTemplateItem(item);

            outputMap.TemplateId     = outputMapCustomItem.TargetTemplate.ID;
            outputMap.NameInputField = outputMapCustomItem.ItemNameField.Name;
            outputMap.PathPattern    = outputMapCustomItem.PathPattern;

            var fieldsCollection =
                item.Children.FirstOrDefault(c => c.InheritsFrom(OutputFieldCollectionItem.TemplateId));

            if (fieldsCollection != null)
            {
                foreach (var field in fieldsCollection.Children.Where(c => c.InheritsFrom(OutputFieldItem.TemplateId)))
                {
                    var fieldCustomItem = new OutputFieldItem(field);
                    outputMap.Fields.Add(new OutputField
                    {
                        SourceColumn    = fieldCustomItem.InputField.Name,
                        TargetFieldName = fieldCustomItem.Name
                    });
                }
            }
            if (!outputMap.Fields.Any())
            {
                outputMap.Fields.Add(new OutputField
                {
                    SourceColumn    = outputMap.NameInputField,
                    TargetFieldName = ""
                });
            }
            if (parentMap != null &&
                !outputMap.Fields.Any(f => f.SourceColumn == parentMap.NameInputField))
            {
                outputMap.Fields.Add(new OutputField
                {
                    SourceColumn    = parentMap.NameInputField,
                    TargetFieldName = ""
                });
            }

            var childMapItems = item.Children.Where(c => c.InheritsFrom(OutputMapTemplateItem.TemplateId));

            if (childMapItems != null &&
                childMapItems.Any())
            {
                foreach (var childMapItem in childMapItems)
                {
                    outputMap.ChildMaps.Add(CreateOutputMap(childMapItem, outputMap));
                }
            }

            return(outputMap);
        }
        private static OutputMap CreateOutputMap(Sitecore.Data.Items.Item item, OutputMap parentMap)
        {
            var outputMap = new OutputMap();
            outputMap.ParentMap = parentMap;
            var outputMapCustomItem = new OutputMapTemplateItem(item);
            outputMap.TemplateId = outputMapCustomItem.TargetTemplate.ID;
            outputMap.NameInputField = outputMapCustomItem.ItemNameField.Name;
            var fieldsCollection =
                item.Children.FirstOrDefault(c => c.InheritsFrom(OutputFieldCollectionItem.TemplateId));
            if (fieldsCollection != null)
            {
                foreach (var field in fieldsCollection.Children.Where(c => c.InheritsFrom(OutputFieldItem.TemplateId)))
                {
                    var fieldCustomItem = new OutputFieldItem(field);
                    outputMap.Fields.Add(new OutputField
                    {
                        SourceColumn = fieldCustomItem.InputField.Name,
                        TargetFieldName = fieldCustomItem.Name
                    });
                }
            }
            if (!outputMap.Fields.Any())
            {
                outputMap.Fields.Add(new OutputField
                {
                    SourceColumn = outputMap.NameInputField,
                    TargetFieldName = ""
                });
            }
            if (parentMap != null &&
                !outputMap.Fields.Any(f => f.SourceColumn == parentMap.NameInputField))
            {
                outputMap.Fields.Add(new OutputField
                {
                    SourceColumn = parentMap.NameInputField,
                    TargetFieldName = ""
                });
            }

            var childMapItems = item.Children.Where(c => c.InheritsFrom(OutputMapTemplateItem.TemplateId));
            if (childMapItems != null &&
                childMapItems.Any())
            {
                foreach (var childMapItem in childMapItems)
                {
                    outputMap.ChildMaps.Add(CreateOutputMap(childMapItem, outputMap));
                }
            }

            return outputMap;
        }
 private ItemDto CreateItem(DataRow dataRow, OutputMap outputMap)
 {
     var itemName = Convert.ToString(dataRow[outputMap.NameInputField]);
     var item = new ItemDto(itemName)
     {
         TemplateId = outputMap.TemplateId
     };
     for (int i = 0; i < outputMap.Fields.Count; i++)
     {
         var mapFieldName = outputMap.Fields[i].TargetFieldName;
         if (!string.IsNullOrEmpty(mapFieldName))
         {
             var fieldValue = dataRow[outputMap.Fields[i].SourceColumn].ToString();
             item.Fields.Add(mapFieldName, fieldValue);
         }
     }
     return item;
 }
 private void ImportMapItems(ImportItemsArgs args, DataTable dataTable, OutputMap outputMap, ItemDto parentItem,
     bool rootLevel)
 {
     var groupedTable = dataTable.GroupBy(outputMap.Fields.Select(f => f.SourceColumn).ToArray());
     for (int i = 0; i < groupedTable.Rows.Count; i++)
     {
         var row = groupedTable.Rows[i];
         if (rootLevel ||
             Convert.ToString(row[outputMap.ParentMap.NameInputField]) == parentItem.Name)
         {
             var createdItem = CreateItem(row, outputMap);
             createdItem.Parent = parentItem;
             parentItem.Children.Add(createdItem);
             if (outputMap.ChildMaps != null
                 && outputMap.ChildMaps.Any())
             {
                 foreach (var childMap in outputMap.ChildMaps)
                 {
                     ImportMapItems(args, dataTable, childMap, createdItem, false);
                 }
             }
         }
     }
 }
 public OutputMap()
 {
     Fields    = new List <OutputField>();
     ChildMaps = new List <OutputMap>();
     ParentMap = null;
 }