public override void FillField(IDataMap map, ref Item newItem, object importRow) { var importValue = string.Join(Delimiter, map.GetFieldValues(ExistingDataNames, importRow)); if (string.IsNullOrWhiteSpace(Component) || !ID.IsID(Component)) { var path = importRow is Item ? ((Item)importRow).Paths.FullPath : "N/A"; Logger.Log($"The Component value is empty or is not an id", path, LogType.FieldToComponent, "Component", Component); return; } var deviceItem = PresentationService.FindDeviceDefinition(newItem, Device); var dsName = DatasourcePath.Contains("/") ? DatasourcePath.Substring(DatasourcePath.LastIndexOf("/") + 1) : DatasourcePath; if (!OverwriteExisting) { var datasource = PresentationService.CreateDatasource(map, newItem, deviceItem, dsName, DatasourceFolder, DatasourcePath, Component, OverwriteExisting); if (datasource == null) { return; } var isSet = SetField(map, datasource, importRow, importValue); if (!isSet) { datasource.Delete(); } else { PresentationService.AddComponent(newItem, datasource, Placeholder, Component, Device, Parameters, IsSXA); } } else { var rendering = PresentationService.FindRendering(deviceItem, Placeholder, Component); if (rendering == null) { Logger.Log($"There was no rendering matching device:{Device} - placeholder:{Placeholder} - component:{Component}", newItem.Paths.FullPath, LogType.MultilistToComponent, "device xml", deviceItem.ToXml()); return; } var datasource = PresentationService.FindDatasourceByName(map, newItem, rendering, dsName); if (datasource == null) { Logger.Log($"There was no datasource found matching name:{dsName}", newItem.Paths.FullPath, LogType.MultilistToComponent, "rendering xml", rendering.ToXml()); return; } var isSet = SetField(map, datasource, importRow, importValue); if (!isSet) { datasource.Delete(); PresentationService.RemoveComponent(newItem, rendering, Device); } } }
public void ProcessComponents(Item newItem, Item importItem) { var components = new List <IComponentMapping>(); components.AddRange(ComponentMappings); TemplateMapping tm = GetTemplateMapping(newItem); if (tm != null) { components.AddRange(tm.ComponentMappings); } foreach (var cm in components) { using (new LanguageSwitcher(ImportToLanguage)) { try { var dsName = cm.ToDatasourcePath.Contains("/") ? cm.ToDatasourcePath.Substring(cm.ToDatasourcePath.LastIndexOf("/") + 1) : cm.ToDatasourcePath; var oldDeviceDef = PresentationService.FindDeviceDefinition(importItem, cm.FromDevice); if (oldDeviceDef == null) { continue; } var oldRendering = PresentationService.FindRendering(oldDeviceDef, cm.FromPlaceholder, cm.FromComponent); if (oldRendering == null) { Logger.Log($"There was no rendering matching device:{cm.FromDevice} - placeholder:{cm.FromPlaceholder} - component:{cm.FromComponent}", importItem.Paths.FullPath, LogType.MultilistToComponent, "device xml", oldDeviceDef.ToXml()); continue; } var oldDatasource = PresentationService.FindDatasource(this, importItem, oldRendering); if (oldDatasource == null) { Logger.Log($"There was no datasource found matching name:{dsName}", importItem.Paths.FullPath, LogType.MultilistToComponent, "rendering xml", oldRendering.ToXml()); continue; } var newDeviceDef = PresentationService.FindDeviceDefinition(newItem, cm.ToDevice); if (newDeviceDef == null) { continue; } if (!cm.OverwriteExisting) { var createDatasource = !string.IsNullOrWhiteSpace(cm.ToDatasourcePath); var newDatasource = createDatasource ? PresentationService.CreateDatasource(this, newItem, newDeviceDef, dsName, cm.ToDatasourceFolder, cm.ToDatasourcePath, cm.ToComponent, cm.OverwriteExisting) : null; if (newDatasource == null && createDatasource) { Logger.Log($"There was no datasource created for device:{cm.ToDevice} - placeholder:{cm.ToPlaceholder} - component:{cm.ToComponent}", newItem.Paths.FullPath, LogType.MultilistToComponent, "device xml", newDeviceDef.ToXml()); continue; } SetDatasourceFields(oldDatasource, newDatasource, cm); PresentationService.AddComponent(newItem, newDatasource, cm.ToPlaceholder, cm.ToComponent, cm.ToDevice, cm.ToParameters, cm.IsSXA); } else { var newRendering = PresentationService.FindRendering(newDeviceDef, cm.ToPlaceholder, cm.ToComponent); if (newRendering == null) { Logger.Log($"There was no rendering matching device:{cm.ToDevice} - placeholder:{cm.ToPlaceholder} - component:{cm.ToComponent}", importItem.Paths.FullPath, LogType.MultilistToComponent, "device xml", newDeviceDef.ToXml()); continue; } var newDatasource = PresentationService.FindDatasource(this, newItem, newRendering); if (newDatasource == null) { Logger.Log($"There was no datasource found matching name:{dsName}", newItem.Paths.FullPath, LogType.MultilistToComponent, "rendering xml", newRendering.ToXml()); continue; } SetDatasourceFields(oldDatasource, newDatasource, cm); } } catch (Exception ex) { Logger.Log("SitecoreDataMap.ProcessComponents", string.Format("failed to import component {0} on item {1}", cm.FromComponent, importItem.Paths.FullPath)); } } } }
public override void FillField(IDataMap map, ref Item newItem, object importRow) { var importValue = string.Join(Delimiter, map.GetFieldValues(ExistingDataNames, importRow)); if (string.IsNullOrEmpty(importValue)) { var path = importRow is Item ? ((Item)importRow).Paths.FullPath : "N/A"; Logger.Log($"There was no import value", path, LogType.MultilistToComponent); return; } var layoutField = newItem.Fields[FieldIDs.FinalLayoutField]; if (layoutField == null) { return; } var layout = LayoutDefinition.Parse(layoutField.Value); var deviceItem = layout.GetDevice(Device); var entries = importValue.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries); var sitecoreMap = (SitecoreDataMap)map; var itemList = entries.Select(a => sitecoreMap.FromDB.GetItem(new ID(a))).Where(b => b != null).ToList(); foreach (var i in itemList) { var itemTemplate = i.TemplateID.ToString(); if (!ComponentMaps.ContainsKey(itemTemplate)) { Logger.Log($"There was no component mapping found", i.Paths.FullPath, LogType.MultilistToComponent, i.TemplateName, itemTemplate); continue; } var cm = ComponentMaps[itemTemplate]; var dsName = cm.DatasourcePath.Contains("/") ? cm.DatasourcePath.Substring(cm.DatasourcePath.LastIndexOf("/") + 1) : cm.DatasourcePath; if (!cm.OverwriteExisting) { var datasource = PresentationService.CreateDatasource(map, newItem, deviceItem, dsName, DatasourceFolder, cm.DatasourcePath, cm.Component, cm.OverwriteExisting); if (datasource == null) { Logger.Log($"There was no datasource created for matching device:{Device} - placeholder:{cm.Placeholder} - component:{cm.Component}", i.Paths.FullPath, LogType.MultilistToComponent, "device xml", deviceItem.ToXml()); continue; } SetFields(i, datasource, cm, sitecoreMap); PresentationService.AddComponent(newItem, datasource, cm.Placeholder, cm.Component, Device, cm.Parameters, IsSXA); } else { var rendering = PresentationService.FindRendering(deviceItem, cm.Placeholder, cm.Component); if (rendering == null) { Logger.Log($"There was no rendering matching device:{Device} - placeholder:{cm.Placeholder} - component:{cm.Component}", i.Paths.FullPath, LogType.MultilistToComponent, "device xml", deviceItem.ToXml()); continue; } var datasource = PresentationService.FindDatasourceByName(map, newItem, rendering, dsName); if (datasource == null) { Logger.Log($"There was no datasource found matching name:{dsName}", i.Paths.FullPath, LogType.MultilistToComponent, "rendering xml", rendering.ToXml()); continue; } SetFields(i, datasource, cm, sitecoreMap); } } }