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?
I have discovereda thingwhile I was tryingto find a possiblesolution 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.
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?
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; } }
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.
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! :)
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:
The error is:
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:
... 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.
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.
Hope this helps,
/Dennis
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?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.
Hope it helps,
/Dennis
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.
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.
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! :)
is working on a reply...