Copied to clipboard

Flag this post as spam?

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


  • Dan 1288 posts 3921 karma points c-trib
    Mar 03, 2014 @ 16:39
    Dan
    0

    Get custom property on image media type in 7.0.4

    Hi,

    I wondered if anyone else is able to successfully get the value from a custom property on a media item with Razor in 7.0.4.

    I've added a property to the image media type, called 'test', which is a textstring datatype. However, whenever I try to access the value of this property in my razor script it throws an exception. The code is:

    var mainImage = Umbraco.TypedMedia(mainImageId);
    var test = mainImage.GetPropertyValue<string>("test");
    

    The error is:

    Value cannot be null.
    Parameter name: propertyType
    
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
    
    Exception Details: System.ArgumentNullException: Value cannot be null.
    Parameter name: propertyType
    

    I can get other properties such as Name, file size, dimensions etc, but not any custom property.

    I thought perhaps this was caused by the media items somehow not having that property (if they hadn't been saved since the property was added for example) so I've tried this:

    var mainImage = Umbraco.TypedMedia(mainImageId);
    if(mainImage.HasProperty("test")){
        var test = mainImage.GetPropertyValue<string>("test");
    }
    

    ... and it seems to recognise that the image has the property, but still results in the same error.

    I wonder if I'm doing something silly or if there's a bug with 7.0.4?

    Any pointers gratefully welcomed, thanks.

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Mar 03, 2014 @ 20:05
    Dennis Aaen
    1

    Hi Dan,

    I have discovered a thing while I was trying to find a possible solution for you. When you need to print out the custom property on the image document type (textstring datatype) you will need to write it lowercase to get it outputted. 

    @{
        if(Model.Content.HasValue("mainImageId")){
            var mediaItem = Umbraco.TypedMedia(Model.Content.GetPropertyValue("mainImageId"));
            <img src="@mediaItem.GetPropertyValue("umbracoFile")" alt="@mediaItem.GetPropertyValue("Name")"/> 
            @mediaItem.GetPropertyValue("test");
        }  
    }

    Hope this helps,

    /Dennis

  • Dan 1288 posts 3921 karma points c-trib
    Mar 03, 2014 @ 20:52
    Dan
    0

    Thanks Dennis, but I'm not sure which part you mean should be lowercase? I just can't see why the original code errors when I call GetPropertyValue("test"). The standard properties work, and it 'knows' that the media item has a property, but then even when that property has a value it just won't output because it says the value is null. Can you output a custom property on an image media type in v7.0.4?

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Mar 03, 2014 @ 21:12
    Dennis Aaen
    0

    Hi Dan,

    Yes I can, and the custom property on the image document type is called test and has an alias of test. I have re-written your code so it should work now.

    @{ 
    var mainImage = Umbraco.TypedMedia(Model.Content.GetPropertyValue("mainImageId"));
        if(mainImage.HasProperty("test")){
            var test = mainImage.GetPropertyValue("test");
            @test;
        }
    }

    Hope it helps,

    /Dennis

  • Dan 1288 posts 3921 karma points c-trib
    Mar 03, 2014 @ 21:20
    Dan
    0

    Thanks Dennis, I appreciate you taking the time to try to replicate this issue. Actually what I think is happening is that my site is actually running 7.0.2 (my bad!) and there seems to be some kind of bug related to this (there are a couple of similar but not identical ones in the tracker e.g. http://issues.umbraco.org/issue/U4-3825) - although I'm not entirely clear on the versions it affects. I'm going to try upgrading to 7.0.4 and see if it still happens though.

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Mar 03, 2014 @ 21:24
    Dennis Aaen
    100

    Dan that's okay. When you have upgraded your site to 7.0.4. You could get back and see if the code I have posted in my last post will work for you.

  • Dan 1288 posts 3921 karma points c-trib
    Mar 03, 2014 @ 21:44
    Dan
    0

    Boom! Upgrading to 7.0.4 fixed it instantly. I've not tested the code you posted as my actual code is quite different - I was just posting a very simplified version; I'm sure it works though. Thanks again for checking and verifying that the custom properties do indeed work on media types in 7.0.4, if not 7.0.2! :)

Please Sign in or register to post replies

Write your reply to:

Draft