Copied to clipboard

Flag this post as spam?

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


  • Jay 413 posts 639 karma points
    Jun 17, 2021 @ 10:46
    Jay
    0

    Umbraco 8 - Add new media property upon site load

    Hi All,

    Was wondering if there's a way I can add a new media property (to the Image type) like a true/false property when an application start?

    Need someone to point me to the right direction.

    Thank you

  • Marc Love (uSkinned.net) 431 posts 1669 karma points
    Jun 17, 2021 @ 10:58
    Marc Love (uSkinned.net)
    1

    Hi Johnny,

    You can see more about startup events here:

    https://our.umbraco.com/documentation/reference/events/application-startup

    To update a media property you want to use IMediaTypeService and IDataTypeService which you can add to your class using Dependency Injection.

    Once you have access to IMediaTypeService and IDataTypeService you can update the properties of a media type using:

        var imageType = _mediaTypeService.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");
          _mediaTypeService.Save(imageType );
        }
    
  • Jay 413 posts 639 karma points
    Jun 17, 2021 @ 11:01
    Jay
    0

    Thanks Marc,

    Works accordingly

Please Sign in or register to post replies

Write your reply to:

Draft