Copied to clipboard

Flag this post as spam?

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


  • steschu 88 posts 489 karma points
    Sep 27, 2018 @ 13:52
    steschu
    0

    Umbraco.TypedMedia OfType with custom MediaTypes

    Hi,

    As described here (https://our.umbraco.com/documentation/Getting-Started/Design/Rendering-Media/) you can use .OfType to convert to distinct media type (such as Image) in order to use a strongly typed object and its properties.

    e.g.: var mediaItem = Umbraco.TypedMedia(1234).OfType<Image>();

    How do I achieve this functionality with custom media types?

    Let's say we create a media type "MyImage" in the MediaTypes root. This media type inherits tabs via Compositions from Image and has another prop called "Title".

    var mediaItem = Umbraco.TypedMedia(1234).OfType<MyImage>(); leads to an error.

    Stephan

  • steschu 88 posts 489 karma points
    Oct 04, 2018 @ 13:51
    steschu
    0

    Can anybody help?

  • Jamie Townsend 62 posts 286 karma points c-trib
    Oct 04, 2018 @ 19:43
    Jamie Townsend
    0

    What’s the error you are getting?

    I assume you’re using PureLive model mode so the objects are created at runtime, have you tried using Dll model mode to see if that works?

    Checkout https://24days.in/umbraco-cms/2016/getting-started-with-modelsbuilder/

  • steschu 88 posts 489 karma points
    Oct 05, 2018 @ 13:41
    steschu
    100

    Hi Jamie,

    Thanks for your help. I switched from PureLive to Dll, but the error remained. BUT: I examined the generated .cs files for the models. And I found out, that it was simply a missspelling in upper in lowercase.

    I named the media type MYImage

    The automatically generated Alias was created as mYImage

    And in the .cs file the class name is MYimage

    I don't know if there's any technical reason why this has to be so. But for me as a user and programmer it is rather cumbersome to deal with three different spellings :-(

    Stephan

  • Jamie Townsend 62 posts 286 karma points c-trib
    Oct 05, 2018 @ 13:45
    Jamie Townsend
    0

    Alias always start with lowercase and classes always start with uppercase, although if you had used dll mode, rebuilt the model in the developer section then you should have had Intellisense removing the typo factor

  • steschu 88 posts 489 karma points
    Oct 05, 2018 @ 14:19
    steschu
    0

    Hi,

    I really got the rule for the alias pretty quick: "Take my name and lower the first letter".

    The rule for the class name is somewhat trickier: Take the alias name and upper the first letter. Check all other letters and if there a multiple upper letters in a row, lower all except the first.

    e.g.: MYFooBAR (Name) -> mYFooBAR (Alias) -> MYfooBar (Class name)

    Stephan

    P.S.: I cannot use VS because the site is directly on the server and I can only access it via RD.

Please Sign in or register to post replies

Write your reply to:

Draft