Copied to clipboard

Flag this post as spam?

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


  • Ross Ekberg 124 posts 364 karma points
    May 02, 2019 @ 13:48
    Ross Ekberg
    0

    Get Folder Content From Media Picker Parameter

    Umbraco 7.13.2

    I am trying to create a partial view macro that displays the content of a media folder. The macro has a parameter called 'folder' and is a Media Picker. My intent is to select a specific media folder and then iterate through the content of the folder, looking for a specific custom media type, called 'Person'. The custom media type has the attributes 'Image', 'Name', and 'Address'. Right now I am just trying to get the folder. All I got so far for the partial view is below.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{
    
        if (Model.MacroParameters["folder"] != null)
        {
            var myParam = Umbraco.Content(Model.MacroParameters["folder"]);
            <div>@myParam.url.toString()</div>
        }
     } 
    

    This produces nothing. How do I know I have found the folder? How do I iterate through the folder's content? How do I check the content's media type?

  • Corné Strijkert 80 posts 456 karma points c-trib
    May 02, 2019 @ 13:59
    Corné Strijkert
    100

    You should not use the Content Picker for this purpose. You have to use the Media Picker to pick media folders.

    Then I would suggest not using Umbraco.Content, because this is using dynamics. Use Umbraco.TypedMedia(folderId) instead. Also, Umbraco.Content() or Umbraco.TypedContent() picks not media, but content, like the name of the methods suggests.

    You can use the following code to get all child media object by providing the media folder ID:

    var folder = Umbraco.TypedMedia(Model.MacroParameters["folder"]);
    if (folder != null && folder.DocumentTypeAlias == "folder")
    {
        foreach (var media in folder.Children().Where(x => x.DocumentTypeAlias == "Person"))
        {
            <img src="@media.Url" />
        }
    }
    
  • Ross Ekberg 124 posts 364 karma points
    May 02, 2019 @ 14:33
    Ross Ekberg
    0

    Thank you for the reply. I am sorry, I did mean to say 'Media Picker'. I am using the media picker. I will update the thread to be correct.

    I will give your solution a try.

  • Ross Ekberg 124 posts 364 karma points
    May 02, 2019 @ 14:55
    Ross Ekberg
    0

    What you suggested works. Thank you very much! I did have to change the first doc type to "Folder".

    Now the trouble I am running into is that the @media.Url value is empty. I don't know if it matters but this is a custom media type with an 'image', 'name', 'address' properties. The 'image' is the "Image Cropper" editor, same as the default "Image" media type.

    I know that the folder has one "Person" image in it, as the foreach loop does put out the code. But the url is just empty.

    Any thoughts?

  • Ross Ekberg 124 posts 364 karma points
    May 02, 2019 @ 15:32
    Ross Ekberg
    0

    I was able to figure it out. Basically, what you suggested was correct. I only had to change the following to get it to work:

    var imgUrl = media.GetPropertyValue("Image");
    <img src="@imgUrl" />
    

    Thank you very much for pointing me in the right direction. I will mark your response as the answer.

  • Corné Strijkert 80 posts 456 karma points c-trib
    May 02, 2019 @ 15:34
    Corné Strijkert
    0

    I think maybe its because you dont use the default umbracoFile alias. You named your property image.

  • Ross Ekberg 124 posts 364 karma points
    May 02, 2019 @ 15:35
    Ross Ekberg
    0

    Interesting. I didn't realize the default alias came with it default properties. Maybe I will fiddle with that.

Please Sign in or register to post replies

Write your reply to:

Draft