Get media items from a property with uComponent MultiNodePicker
I have a page where I want to display information from some child pages. In this child pages there is a field based on a datatype using uComponent MultiNodePicer. In this field I selected some images and I want to display the different images. Unfortunately I have some problems and don't find a solution.
I hope that I can get some advice about this because I really don't find the good solution. It's probably something small I don't see after trying to long.
To test I wrote out the InnerText for the property based on the MultiNodePicker and noticed that it displayed directly the nodeid of the image. So I changed my code to:
foreach (var image in item.contentImages) { dynamic mediaItem = new DynamicMedia(image.InnerText); <li><img src='/[email protected]&width=300&height=150' /> </li> }
I'm not sure if this is the correct way of doing this, but it seems to work for me.
If your solution works for you, then all is good :)
However, a quick pointer to the Model.MediaById() method.
Taken from part 5 of the blog post series by Gareth Evans, it is possible to do the following:
@{ var images = Model.MediaById(Model.yourMultiPickerPropertyName); }
This will only work if you return your value as csv, not xml! MediaById (and also NodeById) takes a params object[], which will return a DynamicMediaList.
Words of warning though, DynamicMediaList is just a list wrapper, it doesn't support OrderBy or Where like DynamicNodeList does.
I took some time to test the Model.MediaById solution.
This works but not when there is only one media item selected.
Following error is returned when looping over the items in a foreach when there is only one media item:
Cannot implicitly convert type 'umbraco.MacroEngines.DynamicMedia' to
'System.Collections.IEnumerable'. An explicit conversion exists (are you
missing a cast?)
This is propably because of the different overload methods for MediaById.
There are some methods that return umbraco.MacroEngines.DynamicMediaList (multiple media items)
And other methods return umbraco.MacroEngines.DynamicMedia (only 1 media item)
When
there is only one media item then the methode MediaById(int) is called
and this return DynamicMedia instead of DynamicMediaList
(MediaById(object[])
A solution could be to test first the type
of the variable that was filled by the MediaById methode before doing
some other actions with this variable. But maybe a more interesting solution for the future would be that the functions who return a MediaList have a pluralised name (ex MediaItemsById)
My implementation is now the following:
@if (item.GetProperty("contentImages").Value != "") { var images = Model.MediaById(item.contentImages);
if (images is umbraco.MacroEngines.DynamicMedia) { <img src='/[email protected]&width=342&height=232' /> } else { <ul> @foreach (var image in images) { <li><img src='/[email protected]&width=342&height=232' /></li> } </ul> } }
I get the message "'int' does not contain a definition for 'ToArray'". Casting first to a string (item.contentImages.String().ToArray()) returns the error "'System.Collections.Generic.List' does not contain a definition for 'String'"
Ok, så apparently, the data type returned is different depending on the number of nodes checked on the field. I did a quick look through the DynamicNode source code, and apparently, it tries to parse the value into an appropriate type. As this is the case, your errors makes perfect sense.
Combining the different return values from both MediaById and the properties on @model, I guess this makes the MediaById method kinda flawed.
Lets hope Gareth Evans stops by with a good suggestion on how to move on from here.
Hi Glenn, I'm trying to get this to work on my site. I'm still a noob to Umbraco so please bear with me.
At first I didn't want to do it like this because it is using both Imagegen and uComponents, but the uComponents documentation was not helpful at all so I've been tinkering with this method. It's giving me an error right now:
c:\HostingSpaces\xxxx\xxxx.com\wwwroot\macroScripts\634562957958785063_Slideshow.cshtml(63): error CS0117: 'umbraco.item' does not contain a definition for 'GetProperty'
I was looking over my posts and noticed that I did not reply to this. I ended up using DAMP to finish the slideshow. I don't know why I was trying with uComponents MultiNodePicker when it was so easy to do with Digibiz Advanced Media Picker. That should help to explain how much I've learned after a couple of months of using Umbraco. My slideshow is an implementation of a jquery slideshow called "coin slider" that can be found at http://workshop.rs/projects/coin-slider/.
Here is my code :
if (Model.HasValue("HomePageSlideShow")) { dynamic mediaItems = Model.HomePageSlideShow.mediaItem; if (mediaItems.Count() != 0) { <div id="coin-slider"> @foreach (var item in mediaItems) { var image = item.Image; <a href="@image.mediaURL"><img src="@image.umbracoFile" alt="@image.nodeName" />a> } div> <script>$(document).ready(function() { $('#coin-slider').coinslider(); }); script> }
Get media items from a property with uComponent MultiNodePicker
I have a page where I want to display information from some child pages. In this child pages there is a field based on a datatype using uComponent MultiNodePicer. In this field I selected some images and I want to display the different images. Unfortunately I have some problems and don't find a solution.
I tried already some things from the topic http://our.umbraco.org/forum/developers/razor/17897-How-to-iterate-DynamicXml but without any success.
The code on my template looks like
The error I get when I try this is "'umbraco.MacroEngines.DynamicXml' does not contain a definition for 'nodeId'"
I replaced the second foreach with the following line of code:
this was mentioned in following topic: http://our.umbraco.org/forum/developers/razor/17729-Razor-newbie but then I get the message "'umbraco.MacroEngines.DynamicXml' does not contain a definition for 'item'".
I hope that I can get some advice about this because I really don't find the good solution. It's probably something small I don't see after trying to long.
Thanks all
Glenn
I was able to solve this.
To test I wrote out the InnerText for the property based on the MultiNodePicker and noticed that it displayed directly the nodeid of the image.
So I changed my code to:
I'm not sure if this is the correct way of doing this, but it seems to work for me.
If your solution works for you, then all is good :)
However, a quick pointer to the Model.MediaById() method.
Taken from part 5 of the blog post series by Gareth Evans, it is possible to do the following:
This will only work if you return your value as csv, not xml!
MediaById (and also NodeById) takes a params object[], which will return a DynamicMediaList.
Words of warning though, DynamicMediaList is just a list wrapper, it doesn't support OrderBy or Where like DynamicNodeList does.
The post can be found here
I took some time to test the Model.MediaById solution.
This works but not when there is only one media item selected.
Following error is returned when looping over the items in a foreach when there is only one media item:
Cannot implicitly convert type 'umbraco.MacroEngines.DynamicMedia' to 'System.Collections.IEnumerable'. An explicit conversion exists (are you missing a cast?)
This is propably because of the different overload methods for MediaById.
There are some methods that return umbraco.MacroEngines.DynamicMediaList (multiple media items)
And other methods return umbraco.MacroEngines.DynamicMedia (only 1 media item)
When there is only one media item then the methode MediaById(int) is called and this return DynamicMedia instead of DynamicMediaList (MediaById(object[])
A solution could be to test first the type of the variable that was filled by the MediaById methode before doing some other actions with this variable. But maybe a more interesting solution for the future would be that the functions who return a MediaList have a pluralised name (ex MediaItemsById)
My implementation is now the following:
Interesting find!
Im guessing though, that it will call MediaById(string) and not MediaById(int).
It's good to see that you found your own workaround, though, another solution could be to always cast the string to an array like this:
That way you would get an array of strings even when there is only one node selected.
Let me know if that works out for you.
It seems to be MediaById(int).
I get the message "'int' does not contain a definition for 'ToArray'".
Casting first to a string (item.contentImages.String().ToArray()) returns the error "'System.Collections.Generic.List' does not contain a definition for 'String'"
Haha nice :)
Ok, så apparently, the data type returned is different depending on the number of nodes checked on the field.
I did a quick look through the DynamicNode source code, and apparently, it tries to parse the value into an appropriate type.
As this is the case, your errors makes perfect sense.
Combining the different return values from both MediaById and the properties on @model, I guess this makes the MediaById method kinda flawed.
Lets hope Gareth Evans stops by with a good suggestion on how to move on from here.
Hi Glenn, I'm trying to get this to work on my site. I'm still a noob to Umbraco so please bear with me.
At first I didn't want to do it like this because it is using both Imagegen and uComponents, but the uComponents documentation was not helpful at all so I've been tinkering with this method. It's giving me an error right now:
c:\HostingSpaces\xxxx\xxxx.com\wwwroot\macroScripts\634562957958785063_Slideshow.cshtml(63): error CS0117: 'umbraco.item' does not contain a definition for 'GetProperty'
Hello Dennis,
were you able to solve this issue? If not, can you post your code here so that we can take a look on what could causes the error.
I was looking over my posts and noticed that I did not reply to this. I ended up using DAMP to finish the slideshow. I don't know why I was trying with uComponents MultiNodePicker when it was so easy to do with Digibiz Advanced Media Picker. That should help to explain how much I've learned after a couple of months of using Umbraco. My slideshow is an implementation of a jquery slideshow called "coin slider" that can be found at http://workshop.rs/projects/coin-slider/.
Here is my code :
is working on a reply...