U8 - How to display the media URL from Media Picker property?
I am trying to display the URL of a media item that has been selected from a media picker property. Any assistance would be greatly appreciated.
I have a nested content datatype:
var contentBlocks = Model.Value<IEnumerable<IPublishedElement>>("contentBlocks");
and I am then trying to loop through these, and get a node from a content picker, and then get a media picker property named 'image' from this node:
foreach(var item in contentBlocks){
var contentBlock = item.GetProperty("block").Value<IPublishedElement>();
var imageUrl = contentBlock.GetProperty("image").Value().Url;
}
but I am having trouble retrieving the Url into the 'imageUrl' var. I am receiving the error "Cannot assign method group to an implicitly-typed variable", and I've tried a few different things but still can't get the Url property from the Image.
HI how do I write this for a macro? does it go inside curley braces for example? I would like to add an image to the latest latest blog Macro but so far it is either coming up blank or throwing an error
Thanks very much Paul. Not sure why I was using IPublishedElement (I think I saw it on some example code somewhere), but changing it to IPublishedContent has fixed it.
U8 - How to display the media URL from Media Picker property?
I am trying to display the URL of a media item that has been selected from a media picker property. Any assistance would be greatly appreciated.
I have a nested content datatype:
and I am then trying to loop through these, and get a node from a content picker, and then get a media picker property named 'image' from this node:
but I am having trouble retrieving the Url into the 'imageUrl' var. I am receiving the error "Cannot assign method group to an implicitly-typed variable", and I've tried a few different things but still can't get the Url property from the Image.
Hi Jonok
This is how you do it in v8.
One other thing Paul - what do the question marks do? Do they check if the property exists before attempting to get the URL?
Hi Jonok Yes, they check for nulls.
The ?. is a Null conditional operator:
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-conditional-operators
And the ?? is a Null coalescing operator:
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-coalescing-operator
HI how do I write this for a macro? does it go inside curley braces for example? I would like to add an image to the latest latest blog Macro but so far it is either coming up blank or throwing an error
@post.Excerpt
Thanks very much Paul. Not sure why I was using IPublishedElement (I think I saw it on some example code somewhere), but changing it to IPublishedContent has fixed it.
An element does not have an url :-)
It only lives as a property on a document, like nested content.
is working on a reply...