private void UpdateContentTypesProperties(IContentType contentType, XElement genericPropertiesElement) { var properties = genericPropertiesElement.Elements("GenericProperty"); foreach (var property in properties) { var dataTypeId = new Guid(property.Element("Type").Value); //The DataType's Control Id var dataTypeDefinitionId = new Guid(property.Element("Definition").Value); //Unique Id for a DataTypeDefinition var dataTypeDefinition = _dataTypeService.GetDataTypeDefinitionById(dataTypeDefinitionId); //If no DataTypeDefinition with the guid from the xml wasn't found OR the ControlId on the DataTypeDefinition didn't match the DataType Id //We look up a DataTypeDefinition that matches if (dataTypeDefinition == null || dataTypeDefinition.ControlId != dataTypeId) { var dataTypeDefinitions = _dataTypeService.GetDataTypeDefinitionByControlId(dataTypeId); if (dataTypeDefinitions != null && dataTypeDefinitions.Any()) { dataTypeDefinition = dataTypeDefinitions.First(); } } // For backwards compatibility, if no datatype with that ID can be found, we're letting this fail silently. // This means that the property will not be created. if (dataTypeDefinition == null) { LogHelper.Warn <PackagingService>(string.Format("Packager: Error handling creation of PropertyType '{0}'. Could not find DataTypeDefintion with unique id '{1}' nor one referencing the DataType with control id '{2}'. Did the package creator forget to package up custom datatypes?", property.Element("Name").Value, dataTypeDefinitionId, dataTypeId)); continue; } var propertyType = new PropertyType(dataTypeDefinition) { Alias = property.Element("Alias").Value, Name = property.Element("Name").Value, Description = property.Element("Description").Value, Mandatory = property.Element("Mandatory").Value.ToLowerInvariant().Equals("true"), ValidationRegExp = property.Element("Validation").Value }; var helpTextElement = property.Element("HelpText"); if (helpTextElement != null) { propertyType.HelpText = helpTextElement.Value; } var tab = property.Element("Tab").Value; if (string.IsNullOrEmpty(tab)) { contentType.AddPropertyType(propertyType); } else { contentType.AddPropertyType(propertyType, tab); } } }
private IDataTypeDefinition GetDataTypeDefinition(XElement property) { var dataTypeDefinitionId = new Guid(property.Element("Definition").Value); var dataTypeDefinition = dataTypeService.GetDataTypeDefinitionById(dataTypeDefinitionId); var legacyPropertyEditorId = Guid.Empty; Guid.TryParse(property.Element("Type").Value, out legacyPropertyEditorId); var propertyEditorAlias = property.Element("Type").Value.Trim(); if (dataTypeDefinition == null) { var dataTypeDefinitions = dataTypeService.GetDataTypeDefinitionByPropertyEditorAlias(propertyEditorAlias); if (dataTypeDefinitions != null && dataTypeDefinitions.Any()) { dataTypeDefinition = dataTypeDefinitions.FirstOrDefault(); } } else { #pragma warning disable CS0618 // Type or member is obsolete if (legacyPropertyEditorId != Guid.Empty && dataTypeDefinition.ControlId != legacyPropertyEditorId) #pragma warning restore CS0618 // Type or member is obsolete { #pragma warning disable CS0618 // Type or member is obsolete var dataTypeDefinitions2 = dataTypeService.GetDataTypeDefinitionByControlId(legacyPropertyEditorId); #pragma warning restore CS0618 // Type or member is obsolete if (dataTypeDefinitions2 != null && dataTypeDefinitions2.Any()) { dataTypeDefinition = dataTypeDefinitions2.FirstOrDefault(); } } else { if (dataTypeDefinition.PropertyEditorAlias != propertyEditorAlias) { var dataTypeDefinitions3 = dataTypeService.GetDataTypeDefinitionByPropertyEditorAlias(propertyEditorAlias); if (dataTypeDefinitions3 != null && dataTypeDefinitions3.Any()) { dataTypeDefinition = dataTypeDefinitions3.FirstOrDefault(); } } } } if (dataTypeDefinition == null) { dataTypeDefinition = dataTypeService.GetDataTypeDefinitionByPropertyEditorAlias("Umbraco.NoEdit").FirstOrDefault(); } return(dataTypeDefinition); }
public static void ImportRemoveMissingProps(this IContentType item, XElement node) { // don't do this if the setting is set to false if (!uSyncSettings.docTypeSettings.DeletePropertyValues) { return; } List <string> propertiesToRemove = new List <string>(); Dictionary <string, string> propertiesToMove = new Dictionary <string, string>(); // go through the properties in the item foreach (var property in item.PropertyTypes) { // is this property in the xml ? XElement propertyNode = node.Element("GenericProperties") .Elements("GenericProperty") .Where(x => x.Element("Alias").Value == property.Alias) .SingleOrDefault(); if (propertyNode == null) { LogHelper.Info <uSync>("Removing {0} from {1}", () => property.Alias, () => item.Name); propertiesToRemove.Add(property.Alias); } else { // at this point we write our properties over those // in the db - because the import doesn't do this // for existing items. LogHelper.Debug <uSync>("Updating prop {0} for {1}", () => property.Alias, () => item.Alias); var legacyEditorId = Guid.Empty; var editorAlias = string.Empty; Guid.TryParse(propertyNode.Element("Type").Value, out legacyEditorId); if (legacyEditorId == Guid.Empty) { // new style id...? } var dataTypeDefinitionId = new Guid(propertyNode.Element("Definition").Value); IDataTypeService _dataTypeService = ApplicationContext.Current.Services.DataTypeService; IDataTypeDefinition dataTypeDefinition = null; try { dataTypeDefinition = _dataTypeService.GetDataTypeDefinitionById(dataTypeDefinitionId); if (dataTypeDefinition == null) { editorAlias = propertyNode.Element("Type").Value; // try to match on guid as alias is not unique dataTypeDefinition = _dataTypeService.GetDataTypeDefinitionByPropertyEditorAlias(editorAlias).Any(x => x.Key == dataTypeDefinitionId) ? _dataTypeService.GetDataTypeDefinitionByPropertyEditorAlias(editorAlias).First(x => x.Key == dataTypeDefinitionId) : _dataTypeService.GetDataTypeDefinitionByPropertyEditorAlias(editorAlias).FirstOrDefault(); } if (dataTypeDefinition != null && dataTypeDefinition.Key == dataTypeDefinitionId) { // all good, we are here.. } else { // we need to do even more looking... var dataTypeDefinitions = _dataTypeService.GetDataTypeDefinitionByControlId(legacyEditorId); if (dataTypeDefinition != null && dataTypeDefinitions.Any()) { dataTypeDefinition = dataTypeDefinitions.First(); } } } catch (Exception ex) { // getDataTypeDefinition can throw exceptions - when you have issues with legacy ids' // so we capture them, so we can carry on. LogHelper.Info <SyncDocType>("Error looking for the datatype {0}, you might be missing a package?", () => dataTypeDefinitionId); } if (dataTypeDefinition != null) { // now we set it in the DB // property.DataTypeDefinitionId = dataTypeDefinition.Id; // we can set both the id and alias, but we can't directly change the // underling database type, not sure if you can to be honest. if (property.DataTypeDefinitionId != dataTypeDefinition.Id) { LogHelper.Info <SyncDocType>("Updating the datatype Definition"); property.DataTypeDefinitionId = dataTypeDefinition.Id; property.PropertyEditorAlias = dataTypeDefinition.PropertyEditorAlias; } // TODO: make changes to the datatype import/export properly. } property.Name = propertyNode.Element("Name").Value; property.Description = propertyNode.Element("Description").Value; property.Mandatory = propertyNode.Element("Mandatory").Value.ToLowerInvariant().Equals("true"); property.ValidationRegExp = propertyNode.Element("Validation").Value; XElement sortOrder = propertyNode.Element("SortOrder"); if (sortOrder != null) { property.SortOrder = int.Parse(sortOrder.Value); } var tab = propertyNode.Element("Tab").Value; if (!string.IsNullOrEmpty(tab)) { var propGroup = item.PropertyGroups.First(x => x.Name == tab); if (!propGroup.PropertyTypes.Any(x => x.Alias == property.Alias)) { // if it's not in this prop group - we can move it it into it LogHelper.Info <uSync>("Moving {0} in {1} to {2}", () => property.Alias, () => item.Name, () => tab); propertiesToMove.Add(property.Alias, tab); } } } } foreach (string alias in propertiesToRemove) { LogHelper.Debug <uSync>("Removing {0}", () => alias); item.RemovePropertyType(alias); // if slow - save after every remove // on big sites, this increases the chances of your SQL server completing the operation in time... // /* * if (uSyncSettings.SlowUpdate) * { * LogHelper.Debug<uSync>("SlowMode: saving now {0}", () => item.Name); * _contentTypeService.Save(item); * } */ } foreach (KeyValuePair <string, string> movePair in propertiesToMove) { item.MovePropertyType(movePair.Key, movePair.Value); } if (propertiesToRemove.Count > 0 || propertiesToMove.Count > 0) { LogHelper.Debug <uSync>("Saving {0}", () => item.Name); _contentTypeService.Save(item); } }