internal static void CreateRequiredFields() { // Creation of IsOptimized boolean field Type contentType = typeof(Image); string fieldName = ImageOptimizationConstants.IsOptimizedFieldName; UserFriendlyDataType userFriendlyDataType = UserFriendlyDataType.YesNo; MetadataManager metadataManager = MetadataManager.GetManager(); MetaType metaType = metadataManager.GetMetaType(contentType); if (metaType != null) { MetaField metaField = metaType.Fields.SingleOrDefault(f => string.Compare(f.FieldName, fieldName, true, CultureInfo.InvariantCulture) == 0); if (metaField == null) { var metaProperty = FieldHelper.GetFields(contentType).FirstOrDefault(f => string.Compare(f.Name, fieldName, true, CultureInfo.InvariantCulture) == 0); if (metaProperty == null) { WcfField wcfField = BuildBooleanWcfField(contentType, fieldName, true); metaField = metadataManager.CreateMetafield(fieldName); CustomFieldsContext.SetFieldDatabaseMappings(metaField, metaType, userFriendlyDataType, wcfField, metadataManager); metaField.Title = fieldName; metaField.MetaAttributes.Add(new MetaFieldAttribute { Name = "UserFriendlyDataType", Value = userFriendlyDataType.ToString() }); metaField.MetaAttributes.Add(new MetaFieldAttribute { Name = "IsCommonProperty", Value = "true" }); metaField.Origin = Assembly.GetExecutingAssembly().GetName().Name; // needed to override deletion from Export for deployment metaType.Fields.Add(metaField); metadataManager.SaveChanges(); } } } }
/// <summary> /// Adds a custom field to Press article /// </summary> /// <param name="fieldname">Name of the field</param> /// <param name="isHierarchicalTaxonomy">is hierarchical taxonomy</param> public void AddCustomTaxonomyToContext(string fieldname, bool isHierarchicalTaxonomy) { Type pressArticleType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.PressRelease.PressArticle"); if (pressArticleType == null) { throw new ArgumentException("PressArticle type can't be resolved."); } var context = new CustomFieldsContext(pressArticleType); TaxonomyManager manager = TaxonomyManager.GetManager(); Guid taxonomyId; if (isHierarchicalTaxonomy == true) { var taxonomy = manager.GetTaxonomies <HierarchicalTaxonomy>().Where(t => t.Title == fieldname).SingleOrDefault(); if (taxonomy != null) { taxonomyId = taxonomy.Id; } else { throw new ArgumentException("The taxonomy '" + fieldname + "' does not exist in the system"); } } else { var taxonomy = manager.GetTaxonomies <FlatTaxonomy>().Where(t => t.Title == fieldname).SingleOrDefault(); if (taxonomy != null) { taxonomyId = taxonomy.Id; } else { throw new ArgumentException("The taxonomy '" + fieldname + "' does not exist in the system"); } } UserFriendlyDataType userFriendlyDataType = UserFriendlyDataType.Classification; var field = new WcfField() { Name = fieldname, ContentType = "Telerik.Sitefinity.DynamicTypes.Model.PressRelease.PressArticle", FieldTypeKey = userFriendlyDataType.ToString(), IsCustom = true, // Field definition Definition = new WcfFieldDefinition() { Title = fieldname, FieldName = fieldname.ToLower(), FieldType = isHierarchicalTaxonomy ? typeof(HierarchicalTaxonField).FullName : typeof(FlatTaxonField).FullName, TaxonomyId = taxonomyId.ToString(), AllowMultipleSelection = true, } }; var fields = new Dictionary <string, WcfField>(); fields.Add(field.FieldTypeKey, field); context.AddOrUpdateCustomFields(fields, pressArticleType.Name); context.SaveChanges(); }
public static void CreateCustomFieldFluentAPI(string fieldName, Type nodeType, Type fieldType, UserFriendlyDataType dataType) { // checking if custom field exists PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(nodeType); PropertyDescriptor property = properties[fieldName]; if (property == null) { String dTypeString = dataType.ToString(); App.WorkWith() .DynamicData() .Type(nodeType) .Field() .TryCreateNew(fieldName, fieldType) .Do((metaField) => { metaField.MetaAttributes.Add(new MetaFieldAttribute() { Name = "UserFriendlyDataType", Value = dTypeString }); }) .SaveChanges(); } }