Getting image URL from Media Picker not working in 7.6
This is what I usually do, if I want to grab an image's URL:
var selection = Umbraco.Content(1234).Children;
foreach(var person in selection) {
IPublishedContent vorto = (IPublishedContent)person;
var img = Umbraco.Media(vorto.GetPropertyValue("image"));
<img src="@img.Url" />
}
This worked until I upgraded to 7.6 a few minutes ago. Vorto values work fine and is grabbed fine from the database, it's simply not converting anymore. The image property is not a Vorto value, but Vorto can automatically grab the translated value or the default one (if not Vorto).
I have tried this as well:
var img = Umbraco.Media(person.GetPropertyValue("image"));
But if you want to use the value converters you can use this code.
var selection = Umbraco.Content(1234).Children;
foreach(var person in selection) {
IPublishedContent vorto = (IPublishedContent)person;
var img = vorto.GetVortoValue<IPublishedContent>("image");
<img src="@img.Url" />
}
Unfortunately it does not seem to work. I get a null reference exception. I copied your example character for character. The other Vorto values work fine though.
I'm currently having a similar looking problem. I'm trying to upgrade a 7.3.1 site to 7.6.0 but I've had lots of trouble with code that interacts with URL and media fields. I've been tinkering with settings, enabling/disabling the "enablePropertyValueConverters" setting and trying other things.
For me the following code is not working:
@foreach (IPublishedContent childPage in thisPageAlbums)
{
string albumCoverImage = string.Empty;
if (childPage.HasValue("mainImage"))
{
albumCoverImage = childPage.GetPropertyValue<IPublishedContent>("mainImage").Url;
}
}
Although the "hasValue" check returns true the GetPropertyValue method returns null.
Any ideas? Is there some official documentation on how these underlying changes should be handled?
Straight after posting the above I checked the "mainImage" data type and it is a Multiple Media Picker. So I've changed the above GetPropertyValue type to childPage.GetPropertyValue<IEnumerable<IPublishedContent>> and the code then started working for me (in this scenario).
You could perhaps check what data type your "image" field is?
HasValue() returns False for me, but HasProperty() returns True. I just triple checked my content, and they all have an image and the property name/tag/id matches perfectly.
Have you upgraded to 7.6 or clean install? Mine is an upgrade - some things started to work for me after I enabled the new ModelsBuilder settings in the web.config.
Odd.. odd.. odd things are happening. I removed all the images, saved the content, inserted the images again, and then saved it all. Now HasValue() returns True and it actually works again.
var img = Umbraco.Media(person.GetPropertyValue("image"));
So weird. I just confirmed this on another page that had the same issue. How weird is that?
Yes, I've been removing and re-adding images and links along the way also, I think this is because they were previously stored as integer references but are now stored in "UDI" format. I thought I had read somewhere that code should be able to work with the new and old formats.
It doesn't seem to be enough to just republish a site, you have to remove and re-add the linked content.
The scary thing is I am upgrading a small website as a test so it's not been too much trouble doing that. We have much bigger websites to upgrade though and we can't go through and fix up all linked content manually...
I'm pretty sure I must be doing something wrong though, or there's a bug...
I am sure there is a fix for this. I just did this on a small website as well, but I can't start to imagine the time I would have to spend, if I were to go on one of our big sites with hundreds of news articles.
If that is the same issue then the line that marked it as "Fixed" says you will "need to re-pick all of the media in your media pickers" - http://issues.umbraco.org/issue/U4-7318
Where is the @item var coming from (is is typed / dynamic etc), I setup a test on my side using Model.Content.GetCropUrl("..","..") and it brings in the URL
the 1118 at the start looks like the media Id from Umbraco, looking up Umbraco.Media might help convert the id to a url
Just to let everyone know I've just hit this too in an upgrade path from 7.2.6 to 7.6.5 and the only thing that worked for me, including trying to relink media, was the .ToString() solution from @Heyden's comment!
Will put comments on the issue tracker letting HQ know it's not as fixed as expected?!?
Thanks nonetheless to all as this really helped me with the final bugs on the migration!
Has anyone experienced issues using the new value converters on Members?
var fullUser = ApplicationContext.Current.Services.MemberService.GetByUsername(curUser.UserName);
IEnumerable<IPublishedContent> files = fullUser.GetValue<IEnumerable<IPublishedContent>>("files");
files is always null (this works fine on content, and I love the new converters), so I have been grabbing the raw string which looks something like this and breaking into an array
Then looping on the array and grabbing via TypedMedia. Unfortunately there is no TypedMedia that accepts an array or IEnumerable of Udi's so I'm doing it in the loop
foreach (string f in stringarray)
{
var udi = Udi.Parse(f); // umb://media/udi
IPublishedContent med = Umbraco.TypedMedia(udi);
}
This all works fine for now, was just curious if there was general issues with Value Converters on IMember ? or if anyone has a cleaner approach as this is even one additional step compared to pre 7.6
@{
//Get the ID of the Field
var mediaId = Umbraco.Field("image");
//Get the MediaType to Write the URL
var media = Umbraco.Media(mediaId.ToString());
//Salve the Path into the Image
var image = media.Url;
}
Make sure that there is image in the field or you will get error message.
Getting image URL from Media Picker not working in 7.6
This is what I usually do, if I want to grab an image's URL:
This worked until I upgraded to 7.6 a few minutes ago. Vorto values work fine and is grabbed fine from the database, it's simply not converting anymore. The
image
property is not a Vorto value, but Vorto can automatically grab the translated value or the default one (if not Vorto).I have tried this as well:
But it's the same deal.
Hi Morten,
In v7.6 this package got integrated in to the core : https://our.umbraco.org/projects/developer-tools/umbraco-core-property-value-converters/
This means that now out of the box property values will be converted to some strongly typed model.
If you want you can disable this feature in the umbracoSettings.config file. Look for this setting and set it to false :
But if you want to use the value converters you can use this code.
Dave
Hi Dave,
It doesn't matter if I set
EnablePropertyValueConverters
to true or false, I will get this exception:Whenever I try to run the code you posted. Any idea what I can try next? Also, I'm looking for the best way to do this, not the best workaround :-)
Hi Morten,
Setting it to false should make your code work.
If you have it my code snippet should work.
But haven't tried using vorto in 7.6 yet I must admit.
Dave
Unfortunately it does not seem to work. I get a null reference exception. I copied your example character for character. The other Vorto values work fine though.
Which line is giving you this error ?
Just remove them, and add them one by one.
Dave
However, this works perfectly:
Something tell me the conversion to IPublishedContent doesn't go well.
I'm currently having a similar looking problem. I'm trying to upgrade a 7.3.1 site to 7.6.0 but I've had lots of trouble with code that interacts with URL and media fields. I've been tinkering with settings, enabling/disabling the "enablePropertyValueConverters" setting and trying other things.
For me the following code is not working:
Although the "hasValue" check returns true the GetPropertyValue method returns null.
Any ideas? Is there some official documentation on how these underlying changes should be handled?
Thanks
Straight after posting the above I checked the "mainImage" data type and it is a Multiple Media Picker. So I've changed the above GetPropertyValue type to
childPage.GetPropertyValue<IEnumerable<IPublishedContent>>
and the code then started working for me (in this scenario).You could perhaps check what data type your "image" field is?
HasValue()
returnsFalse
for me, butHasProperty()
returnsTrue
. I just triple checked my content, and they all have an image and the property name/tag/id matches perfectly.It's a simple Media Picker.
Have you upgraded to 7.6 or clean install? Mine is an upgrade - some things started to work for me after I enabled the new ModelsBuilder settings in the web.config.
Upgrade and they were already added.
Odd.. odd.. odd things are happening. I removed all the images, saved the content, inserted the images again, and then saved it all. Now
HasValue()
returnsTrue
and it actually works again.So weird. I just confirmed this on another page that had the same issue. How weird is that?
Yes, I've been removing and re-adding images and links along the way also, I think this is because they were previously stored as integer references but are now stored in "UDI" format. I thought I had read somewhere that code should be able to work with the new and old formats.
It doesn't seem to be enough to just republish a site, you have to remove and re-add the linked content.
The scary thing is I am upgrading a small website as a test so it's not been too much trouble doing that. We have much bigger websites to upgrade though and we can't go through and fix up all linked content manually...
I'm pretty sure I must be doing something wrong though, or there's a bug...
I am sure there is a fix for this. I just did this on a small website as well, but I can't start to imagine the time I would have to spend, if I were to go on one of our big sites with hundreds of news articles.
Hopefully someone can point to some proper guidance on what has changed, what the impact is and what the steps are to move to the new model.
Or perhaps we just have to set EnablePropertyValueConverters to false and continue as before?
That's what I am doing, but I still have to remove images and set them again before it works.
Uh oh...
I know, right? It's such a bummer.
Don't worry, somebody on here will enlighten us. Any minute now...
There's more related info on this here: http://issues.umbraco.org/issue/U4-7318
If that is the same issue then the line that marked it as "Fixed" says you will "need to re-pick all of the media in your media pickers" - http://issues.umbraco.org/issue/U4-7318
Please no.
I see 7.6.1 has been released. I'll have to see if that changes any of this.
I couldn't get the above code to work but converting my value to a string somehow did?
Without ToString() it doesn't work, might help somebody :)
This worked for me as well.
But what I really trying to achieve was to get the crop url like so:
but it returns the following:
Any ideas how I could possibly get this to work?
Hi Edgar,
Where is the @item var coming from (is is typed / dynamic etc), I setup a test on my side using Model.Content.GetCropUrl("..","..") and it brings in the URL
the 1118 at the start looks like the media Id from Umbraco, looking up Umbraco.Media might help convert the id to a url
or theres heaps of examples on the documentation which might point you in the right direction as well -> https://our.umbraco.org/documentation/getting-started/backoffice/property-editors/built-in-property-editors/image-cropper
sorry I couldn't be any further help, I wasn't able to recreate your situation on my end!
goodluck!
Legend...this worked a treat!!! Many thanks
Hayden, your solution worked for me ;)
I got the same issue in version 7.6.4 using the Umbraco.MediaPicker2
Have anyone found a solution?
Not sure if this is your solution, but for all of the images on our site, we use something like this
Model.Content.imageProperty.FirstOrDefault();
FirstOrDefault because it is a list of ipublished content
Just to let everyone know I've just hit this too in an upgrade path from 7.2.6 to 7.6.5 and the only thing that worked for me, including trying to relink media, was the
.ToString()
solution from @Heyden's comment!Will put comments on the issue tracker letting HQ know it's not as fixed as expected?!?
Thanks nonetheless to all as this really helped me with the final bugs on the migration!
#h5yr!
Has anyone experienced issues using the new value converters on Members?
files
is always null (this works fine on content, and I love the new converters), so I have been grabbing the raw string which looks something like this and breaking into an arrayumb://media/2572389b7fc24ce9a057d7ba31f19f9c,umb://media/8fc3d88b63ba4c4baad3464b0bc176e4,umb://media/c65c3456e9c2435e97971c5f29ef97c2,umb://media/97daf7c8c5dd414bb906390b7d7ec5d0
Then looping on the array and grabbing via TypedMedia. Unfortunately there is no TypedMedia that accepts an array or IEnumerable of Udi's so I'm doing it in the loop
This all works fine for now, was just curious if there was general issues with Value Converters on IMember ? or if anyone has a cleaner approach as this is even one additional step compared to pre 7.6
Cheers!
Hi guys,
Easy way to get images:
Make sure that there is image in the field or you will get error message.
Do Work it with Media Picker 2? I think no.
is working on a reply...