private void InstallAuthyProfileConfiguration(SiteInitializer initializer) { Type type = typeof(SitefinityProfile); List <DetailFormViewElement> detailFormViewElements = new List <DetailFormViewElement>(); foreach (ContentViewControlElement value in initializer.Context.GetConfig <ContentViewConfig>().ContentViewControls.Values) { if (value.ContentType != type) { continue; } detailFormViewElements.AddRange(CustomFieldsContext.GetViews(value.ViewsConfig.Values)); } foreach (DetailFormViewElement detailFormViewElement in detailFormViewElements) { ContentViewSectionElement contentViewSectionElement = detailFormViewElement.Sections.Values.First <ContentViewSectionElement>(); if (!contentViewSectionElement.Fields.ContainsKey("AuthyId")) { TextFieldDefinitionElement textFieldDefinitionElement = new TextFieldDefinitionElement(contentViewSectionElement.Fields) { ID = "authyIdField", FieldName = "AuthyId", DataFieldName = "AuthyId", DisplayMode = new FieldDisplayMode?(FieldDisplayMode.Write), Title = "AuthyId", WrapperTag = HtmlTextWriterTag.Li, ResourceClassId = typeof(TwoFactorAuthenticationResources).Name, Hidden = new bool?(false) }; contentViewSectionElement.Fields.Add(textFieldDefinitionElement); } } }
/// <summary> /// Get validator definition for specific field /// </summary> /// <param name="fieldName">Field name</param> /// <returns></returns> public static IValidatorDefinition GetFieldValidatorDefinition(string fieldName) { var views = CustomFieldsContext.GetViews(typeof(SitefinityProfile).FullName); var view = views.FirstOrDefault(); foreach (var section in view.Sections.Values) { if (section != null) { // get configuration element FieldDefinitionElement fieldDefinitionElement = WcfDefinitionBuilder.GetFieldDefinitionElement(fieldName, section); var fieldControlDefinitionElement = fieldDefinitionElement as FieldControlDefinitionElement; if (fieldControlDefinitionElement != null) { // transfer complex properties like validation if (fieldControlDefinitionElement.Validation != null) { return(fieldControlDefinitionElement.Validation); } } } } return(null); }
/// <summary> /// Registers the field selector. /// </summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <typeparam name="E">Type of the e.</typeparam> /// <param name="fieldName">Name of the field.</param> /// <example> /// ContentHelper.RegisterFieldSelector<Document, CustomFieldElement>("CustomField1"); /// </example> public static void RegisterFieldSelector <T, E>(string fieldName) where T : IContent where E : FieldControlDefinitionElement { // Check if the field is not already present for this content type string itemType = typeof(T).FullName; var fieldExists = GetMetaFieldsForType(itemType) .Where(f => f.FieldName == fieldName).SingleOrDefault() != null; if (!fieldExists) { // Ddd the metafield that will hold the data App.WorkWith() .DynamicData() .Type(typeof(T)) .Field() .TryCreateNew(fieldName, typeof(Guid[])) .SaveChanges(true); // Get Backend views(e.g. Edit, Create) configuration var section = GetModuleConfigSection(itemType); string definitionName = GetModuleBackendDefinitionName(itemType); var backendSection = section.ContentViewControls[definitionName]; var views = backendSection.ViewsConfig.Values.Where(v => v.ViewType == typeof(DetailFormView)); foreach (DetailFormViewElement view in views) { // If there are no custom fields added before, the new field will be placed int he CustomFieldsSection var sectionToInsert = CustomFieldsContext.GetSection(view, CustomFieldsContext.customFieldsSectionName, itemType); var fieldConfigElementType = TypeResolutionService.ResolveType(typeof(E).FullName); // Create a new instance of our field configuration in the current view configuration E newElement = Activator.CreateInstance(fieldConfigElementType, new object[] { sectionToInsert.Fields }) as E; if (newElement != null) { // Populate custom field values newElement.DataFieldName = fieldName; newElement.FieldName = fieldName; newElement.Title = fieldName; newElement.DisplayMode = FieldDisplayMode.Write; sectionToInsert.Fields.Add(newElement); } } var manager = ConfigManager.GetManager(); using (new ElevatedModeRegion(manager)) { manager.SaveSection(section); } SystemHelper.RestartApplication(); } }
private static WcfField BuildBooleanWcfField(Type contentType, string fieldName, bool isHidden) { WcfField wcfField = new WcfField() { Name = fieldName, ContentType = contentType.FullName, FieldTypeKey = UserFriendlyDataType.YesNo.ToString(), IsCustom = true, //Database mapping DatabaseMapping = new WcfDatabaseMapping() { ClrType = typeof(bool).FullName, ColumnName = fieldName.ToLower(), Nullable = true, DbType = "BIT" }, //Field definition Definition = new WcfFieldDefinition() { Title = fieldName, FieldName = fieldName, FieldType = typeof(ChoiceField).FullName, RenderChoiceAs = RenderChoicesAs.SingleCheckBox, Hidden = isHidden } }; ChoiceItem yesNoItem = new ChoiceItem() { Text = fieldName, Value = "true", Selected = false }; List <ChoiceItem> items = new List <ChoiceItem>(); items.Add(yesNoItem); JavaScriptSerializer serializer = new JavaScriptSerializer(); wcfField.Definition.Choices = serializer.Serialize(items); CustomFieldsContext.Validate(wcfField, contentType); return(wcfField); }
/// <summary> /// Removes a custom field from the Press article /// </summary> /// <param name="fieldname">custom field name to be removed</param> public void RemoveCustomFieldFromContext(string fieldname) { 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); context.RemoveCustomFields(new string[] { fieldname }, pressArticleType.Name); context.SaveChanges(); SystemManager.RestartApplication(false); }
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> /// Removes a custom field from the Press article /// </summary> /// <param name="fieldname">custom field name to be removed</param> public void RemoveCustomFieldFromContext(string fieldname) { 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); context.RemoveCustomFields(new string[] { fieldname }, pressArticleType.Name); context.SaveChanges(); SystemManager.RestartApplication(false); }
/// <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(); }
/// <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(); }