Hi,
I’m attempting to add a new property to the Image
media type. Pretty simple, just want to add a Description
property I can use for alt text without having to set this up every time.
I’ve done a fair bit with data types and content types so my code feels right but clearly I’m either missing something, or there’s something wrong with the saving of media types.
My code is as follows:
using System.Linq;
using Example.Core.Services.Interfaces;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
namespace Example.Core.Services
{
public class ConstructMediaTypeService : IConstructMediaTypeService
{
#region Services
private readonly IMediaTypeService _umbracoMediaTypeService;
private readonly IDataTypeService _dataTypeService;
#endregion
#region Constructors
public ConstructMediaTypeService(IMediaTypeService mediaTypeService, IDataTypeService dataTypeService)
{
_umbracoMediaTypeService = mediaTypeService;
_dataTypeService = dataTypeService;
}
#endregion
#region Initialisation Methods
public void InitialiseMediaTypes()
{
InitialiseImageMediaType();
}
private void InitialiseImageMediaType()
{
IMediaType imageType = _umbracoMediaTypeService.Get("Image");
PropertyGroup imagePropertyGroup = imageType.PropertyGroups.First(x => x.Name == "Image");
IDataType dataType = _dataTypeService.GetDataType("Textstring");
PropertyType descriptionPropertyType = new PropertyType(dataType);
descriptionPropertyType.Id = 1249238749;
descriptionPropertyType.Name = "Description2";
descriptionPropertyType.Alias = "description2";
imageType.AddPropertyType(descriptionPropertyType, "Image");
_umbracoMediaTypeService.Save(imageType);
}
#endregion
}
}
So, kicks on on startup. As I step through the code it all looks correct, I can see the Image
property group gets assigned the new property (please see image below). There are no errors thrown but the property just doesn’t display on the media type.
Anyone done this successfully or fancy giving it a go and telling me where I’m going wrong?
Thanks,
Ben
This is a companion discussion topic for the original entry at https://our.umbraco.com/forum/106096-add-property-to-media-type-programatically