Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ben Palmer 180 posts 866 karma points c-trib
    May 24, 2021 @ 14:59
    Ben Palmer
    0

    Add Property to Media Type Programatically

    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.

    enter image description here

    Anyone done this successfully or fancy giving it a go and telling me where I'm going wrong?

    Thanks,

    Ben

  • Peter Cort Larsen 3 posts 44 karma points
    Jun 18, 2021 @ 09:06
    Peter Cort Larsen
    1

    Hi,

    Umbraco has a collection of components, which is created during composition. So if you want the component to run, you need to register it.

    Example

    using System;
    using Umbraco.Core.Composing;
    using Umbraco.Core.Models;
    using Umbraco.Core.Services;
    
    namespace Website.Services
    {
        public class MediaType: IComponent
        {
            private readonly IMediaTypeService _umbracoMediaTypeService;
    
            private readonly IDataTypeService _dataTypeService;
    
            public MediaType(IMediaTypeService mediaTypeService, IDataTypeService dataTypeService)
            {
                _umbracoMediaTypeService = mediaTypeService;
    
                _dataTypeService = dataTypeService;
            }
    
            public void Initialize()
            {
                InitialiseImageMediaType(); 
            }
    
            public void Terminate()
            {
                throw new NotImplementedException();
            }
    
            private void InitialiseImageMediaType()
            {
                IMediaType imageType = _umbracoMediaTypeService.Get("Image");
    
                if (imageType != null)
                {
                    //Add true false datatype
                    var trueFalseDataType = _dataTypeService.GetDataType("True/false");
                    PropertyType myProperty = new PropertyType(trueFalseDataType, "myProperty");
                    myProperty.Name = "My true false property";
                    imageType.AddPropertyType(myProperty, "Image");
                    _umbracoMediaTypeService.Save(imageType);
                }
            }  
        }
    }
    

    Then register it like this:

    namespace Website.Composers
    {
    
       public class RegisterIndexerComponentComposer : IUserComposer
        {
            public void Compose(Composition composition)
            {
                composition.Components().Append<MediaType>();
            }
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft