private void ImportNodes(XDocument xml, ZipFile zipFile) { var root = xml.Root; if (root != null) { foreach (var node in root.Elements().OrderBy(x => int.Parse(x.Attribute("level").Value))) { var key = new Guid(node.Attribute("guid").Value); var nodeType = node.Attribute("objectType").Value; var xRoot = new XmlRootAttribute { ElementName = node.Name.ToString(), IsNullable = true }; var xmlSerialiser = new XmlSerializer(typeof(Content), xRoot); if (nodeType == ObjectTypes.Document.ToString()) { CreateOrUpdateContent(zipFile, key, xmlSerialiser, node); } else if (nodeType == ObjectTypes.Media.ToString()) { CreateOrUpdateMedia(zipFile, key, node); } } var nodesWithSpecialProperties = root.Descendants().Where(x => x.Attribute("dataTypeGuid") != null && !string.IsNullOrWhiteSpace(x.Attribute("dataTypeGuid").Value) && SpecialDataTypes.ContainsKey(new Guid(x.Attribute("dataTypeGuid").Value))) .GroupBy(x => x.Parent.Attribute("guid").Value) .Select( y => new { Guid = y.Key, Properties = y.Select(x => x).ToList(), IsPublished = bool.Parse(y.FirstOrDefault().Parent.Attribute("published").Value) }); foreach (var node in nodesWithSpecialProperties) { var iContent = Services.ContentService.GetById(new Guid(node.Guid)); foreach (var prop in node.Properties) { var dataTypeGuid = new Guid(prop.Attribute("dataTypeGuid").Value); var value = DataTypeConverterImport(prop, dataTypeGuid); iContent.SetValue(prop.Name.ToString(), value); } SaveContent(iContent, node.IsPublished); } } }
/// <summary> /// Serialize IContent to XElement and adds dependent nodes /// </summary> /// <param name="content">Umbraco IContent object</param> /// <param name="dependantNodes">this function will add dependent nodes to this collection</param> /// <returns>returns serialized version of IContent as XElement</returns> public XElement SerializeContent(IContent content, Dictionary <int, ObjectTypes> dependantNodes = null) { dependantNodes = dependantNodes ?? new Dictionary <int, ObjectTypes>(); var nodeName = content.ContentType.Alias.ToSafeAliasWithForcingCheck(); var currentContent = new XElement(nodeName, new XAttribute("nodeName", content.Name), new XAttribute("nodeType", content.ContentType.Id), new XAttribute("creatorName", content.GetCreatorProfile().Name), new XAttribute("writerName", content.GetWriterProfile().Name), new XAttribute("writerID", content.WriterId), new XAttribute("templateID", content.Template == null ? "0" : content.Template.Id.ToString(CultureInfo.InvariantCulture)), new XAttribute("nodeTypeAlias", content.ContentType.Alias), new XAttribute("id", content.Id), new XAttribute("parentID", content.Level > 1 ? content.ParentId : -1), new XAttribute("level", content.Level), new XAttribute("creatorID", content.CreatorId), new XAttribute("sortOrder", content.SortOrder), new XAttribute("createDate", content.CreateDate.ToString("s")), new XAttribute("updateDate", content.UpdateDate.ToString("s")), new XAttribute("path", content.Path), new XAttribute("isDoc", string.Empty), new XAttribute("releaseDate", content.ReleaseDate != null ? content.ReleaseDate.Value.ToString("s") : DateTime.MinValue.ToString("s")), new XAttribute("expireDate", content.ExpireDate != null ? content.ExpireDate.Value.ToString("s") : DateTime.MinValue.ToString("s")), new XAttribute("parentGuid", content.Level > 1 ? content.Parent().Key.ToString() : string.Empty), new XAttribute("guid", content.Key), new XAttribute("objectType", ObjectTypes.Document), new XAttribute("published", content.Published)); var propertyTypes = content.PropertyTypes.ToArray(); var count = 0; foreach (var property in content.Properties) { var tag = property.ToXml(); var propertyType = propertyTypes.ElementAt(count); tag.Add(new XAttribute("dataTypeGuid", propertyType.DataTypeId)); tag.Add(new XAttribute("dataTypeName", Services.DataTypeService.GetDataTypeDefinitionById(propertyType.DataTypeDefinitionId).Name)); var guid = propertyTypes.ElementAt(count).DataTypeId; if (SpecialDataTypes.ContainsKey(guid)) { DataTypeConverterExport(property, tag, dependantNodes, SpecialDataTypes[guid]); } currentContent.Add(tag); count++; } return(currentContent); }
/// <summary> /// Serialize IMedia to XElement and adds dependent nodes /// </summary> /// <param name="media">Umbraco IMedia object</param> /// <param name="dependantNodes">this function will add dependent nodes to this collection</param> /// <returns>returns serialized version of IMedia as XElement</returns> public XElement SerialiseMedia(IMedia media, Dictionary <int, ObjectTypes> dependantNodes = null) { var nodeName = media.ContentType.Alias.ToSafeAliasWithForcingCheck(); var node = new XElement(nodeName, new XAttribute("name", media.Name), new XAttribute("nodeTypeAlias", media.ContentType.Alias), new XAttribute("guid", media.Key), new XAttribute("sortOrder", media.SortOrder), new XAttribute("parentGuid", media.Parent() == null ? "-1" : media.Parent().Key.ToString()), new XAttribute("level", media.Level), new XAttribute("objectType", ObjectTypes.Media)); var propertyTypes = media.PropertyTypes.Where(x => !Constants.MediaDefaultProperties.Contains(x.Alias)).ToArray(); var count = 0; foreach (var property in media.Properties.Where(x => !Constants.MediaDefaultProperties.Contains(x.Alias))) { var tag = property.ToXml(); var propertyType = propertyTypes.ElementAt(count); var dt = Services.DataTypeService.GetDataTypeDefinitionById(propertyType.DataTypeDefinitionId); var propertyEditorAlias = dt.PropertyEditorAlias; tag.Add(new XAttribute("propertyEditorAlias", propertyEditorAlias)); tag.Add(new XAttribute("dataTypeName", dt.Name)); if (propertyEditorAlias == Umbraco.Core.Constants.PropertyEditors.UploadFieldAlias) { var umbracoFile = property.Value.ToString(); tag.Add( new XAttribute("umbracoFile", umbracoFile), new XAttribute("fileName", umbracoFile.Split('/').Last()), new XAttribute("objectType", ObjectTypes.File)); } else if (SpecialDataTypes.ContainsKey(propertyEditorAlias)) { DataTypeConverterExport(property, tag, dependantNodes, SpecialDataTypes[propertyEditorAlias]); } node.Add(tag); count++; } return(node); }
private void SaveMedia(XElement node, IMedia media, ZipFile zip) { media.Name = node.Attribute("name").Value; media.ParentId = GetMediaParentId(node); media.Key = new Guid(node.Attribute("guid").Value); int sortOrder; if (int.TryParse(node.Attribute("sortOrder").Value, out sortOrder)) { media.SortOrder = sortOrder; } foreach (var propertyTag in node.Elements()) { var propertyEditorAlias = propertyTag.Attribute("propertyEditorAlias").Value.ToString(); //var dataType = Services.DataTypeService.GetDataTypeDefinitionByName(propertyTag.Attribute("dataTypeName").Value.ToString()); if (propertyEditorAlias == Umbraco.Core.Constants.PropertyEditors.UploadFieldAlias) { var fileName = propertyTag.Attribute("fileName").Value; var umbracoFile = propertyTag.Attribute("umbracoFile").Value; if (!string.IsNullOrWhiteSpace(umbracoFile)) { media.SetValue(propertyTag.Name.ToString(), fileName, GetFileStream(umbracoFile, zip)); } else { media.SetValue(propertyTag.Name.ToString(), string.Empty); } } else if (!SpecialDataTypes.ContainsKey(propertyEditorAlias)) { media.SetValue(propertyTag.Name.ToString(), propertyTag.Value); } } Services.MediaService.Save(media); }
private void SaveMedia(XElement node, IMedia media, ZipFile zip) { media.Name = node.Attribute("name").Value; media.ParentId = GetMediaParentId(node); media.Key = new Guid(node.Attribute("guid").Value); int sortOrder; if (int.TryParse(node.Attribute("sortOrder").Value, out sortOrder)) { media.SortOrder = sortOrder; } foreach (var propertyTag in node.Elements()) { var dataTypeGuid = new Guid(propertyTag.Attribute("dataTypeGuid").Value); if (propertyTag.Attribute("fileName") != null && propertyTag.Attribute("dependentAsset") != null) { var fileName = propertyTag.Attribute("fileName").Value; var file = propertyTag.Attribute("dependentAsset").Value; if (!string.IsNullOrWhiteSpace(file)) { media.SetValue(propertyTag.Name.ToString(), fileName, GetFileStream(file, zip)); } else { media.SetValue(propertyTag.Name.ToString(), string.Empty); } } else if (!SpecialDataTypes.ContainsKey(dataTypeGuid)) { media.SetValue(propertyTag.Name.ToString(), propertyTag.Value); } } Services.MediaService.Save(media); }
/// <summary> /// Serialize IMedia to XElement and adds dependent nodes /// </summary> /// <param name="media">Umbraco IMedia object</param> /// <param name="dependantNodes">this function will add dependent nodes to this collection</param> /// <returns>returns serialized version of IMedia as XElement</returns> public XElement SerializeMedia(IMedia media, Dictionary <int, ObjectTypes> dependantNodes = null) { var nodeName = media.ContentType.Alias.ToSafeAliasWithForcingCheck(); var node = new XElement(nodeName, new XAttribute("name", media.Name), new XAttribute("nodeTypeAlias", media.ContentType.Alias), new XAttribute("guid", media.Key), new XAttribute("sortOrder", media.SortOrder), new XAttribute("parentGuid", media.Parent() == null ? "-1" : media.Parent().Key.ToString()), new XAttribute("level", media.Level), new XAttribute("objectType", ObjectTypes.Media)); var propertyTypes = media.PropertyTypes.Where(x => !Constants.MediaDefaultProperties.Contains(x.Alias)).ToArray(); var count = 0; foreach (var property in media.Properties.Where(x => !Constants.MediaDefaultProperties.Contains(x.Alias))) { var tag = property.ToXml(); var propertyType = propertyTypes.ElementAt(count); tag.Add(new XAttribute("dataTypeGuid", propertyType.DataTypeId)); tag.Add(new XAttribute("dataTypeName", Services.DataTypeService.GetDataTypeDefinitionById(propertyType.DataTypeDefinitionId).Name)); var guid = propertyTypes.ElementAt(count).DataTypeId; if (SpecialDataTypes.ContainsKey(guid)) { DataTypeConverterExport(property, tag, dependantNodes, SpecialDataTypes[guid]); } node.Add(tag); count++; } return(node); }