Copied to clipboard

Flag this post as spam?

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


  • Pete 213 posts 285 karma points
    Aug 24, 2012 @ 18:12
    Pete
    0

    Get value of property on Media Folder?

    I've added a custom true/false property to the Media Folder data type "myproperty", and am struggling to work out how to access it's value.

    var mediaFolder = @Model.MediaById(1201)
    var check = mediaFolder.HasProperty("MyProperty");
    //Returns True

  • Funka! 398 posts 661 karma points
    Aug 24, 2012 @ 18:57
    Funka!
    0

    I've gotten in the habit of checking my umbraco true/false properties as follows:

    if (mediaFolder.myProperty.GetType() == typeof(bool) && (bool)mediaFolder.myProperty) { /* checkbox is checked! */ }

    This is because sometimes there is an unknown state on the property (null or missing) you might want to assign your own default to (i.e., treat a null as either a true or a false? just change the logic/comparison to reverse.) So, sometimes umbraco gives you this as a proper bool, sometimes not. The code above lets me be sure I actually have a bool when I need one.

    There is also a library helper if you are just looking to output a different string based on this,

    @Library.If(mediaFolder.myProperty, "yay! checked!", "boooo, not checked")

    Best of luck!

  • Pete 213 posts 285 karma points
    Aug 25, 2012 @ 15:21
    Pete
    0

    Ah thanks Funka, keep forgetting the bool check :/

    Do you know of another method of getting a media folder id by name, other than using ucomponents' uquery? ( uQuery.GetMediaByName )

Please Sign in or register to post replies

Write your reply to:

Draft