Umbraco 如何将属性从代码添加到媒体类型?
我使用Umbraco 7.4.12,需要从代码动态地向媒体类型添加属性,而不是从UI添加属性 最好的方法是什么Umbraco 如何将属性从代码添加到媒体类型?,umbraco,umbraco7,Umbraco,Umbraco7,我使用Umbraco 7.4.12,需要从代码动态地向媒体类型添加属性,而不是从UI添加属性 最好的方法是什么 像这样的事情应该行吗 下面我将向默认图像媒体类型添加textstring属性。我刚才甚至还测试了它,它正在工作:-) AddPropertyType方法有一个重载,允许您在需要时将属性添加到给定的选项卡/属性组 protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, Ap
像这样的事情应该行吗 下面我将向默认图像媒体类型添加textstring属性。我刚才甚至还测试了它,它正在工作:-) AddPropertyType方法有一个重载,允许您在需要时将属性添加到给定的选项卡/属性组
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
var contentTypeService = ApplicationContext.Current.Services.ContentTypeService;
var dataTypeService = ApplicationContext.Current.Services.DataTypeService;
var mediaType = contentTypeService.GetMediaType(1032);
if (mediaType != null && !mediaType.PropertyTypeExists("myNewPropertyAlias"))
{
var dataTypeDefinitions = dataTypeService.GetAllDataTypeDefinitions().ToArray();
var textStringDataTypeDefinition = dataTypeDefinitions.FirstOrDefault(p => p.Name.ToLower() == "textstring");
mediaType.AddPropertyType(new PropertyType(textStringDataTypeDefinition) { Name = "My New Property Name", Alias = "myNewPropertyAlias" });
contentTypeService.Save(mediaType);
}
}