Copied to clipboard

Flag this post as spam?

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


  • BEWD 90 posts 303 karma points
    Oct 03, 2016 @ 12:44
    BEWD
    0

    Filtering by Media Properties in Partial View Macro

    Hi All

    I have created a Partial View Macro that is displayed via the RTE which basically displays items that's are listed in which ever folder you specify.

    I have also added another property to Media items 'File' which is called location and each media item is tagged as being relevant one or more location(s).

    I would like to filter the Partial View macro to display items by location, I.e, display documents relevant to Company A in one div, Company B in another div etc whilst only having to insert the macro once in the RTE

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @*
    Macro Parameters To Create, for this macro to work:
    Alias:mediaId     Name:Media Folder ID    Type:Single Media Picker
    *@
    
    @if (Model.MacroParameters["mediaId"] != null)
    {
    @* Get the media folder as a dynamic node *@
    var mediaFolder = Umbraco.Media(Model.MacroParameters["mediaId"]);
    if (mediaFolder.Children.Any())
    {
    <div class="docContainer">
    <div class="docHeader"><h4>Company A</h4></div>
    <ul class="docList">
        @* for each item in children of the selected media folder *@
        @foreach (var mediaItem in mediaFolder.Children)
        {
        <li>
            <a target="_blank" href="@mediaItem.umbracoFile" style="text-decoration:none;"> <img src="~/Images/doc_icon.png" alt="QWERTY" />  @mediaItem.Name</a>
        </li>
        }
    </ul>
    </div>
    }}
    

    Is what I am trying to do possible and more importantly does it make sense?

    Thanks

    Ben

  • Sven Geusens 169 posts 881 karma points c-trib
    Oct 11, 2016 @ 08:27
    Sven Geusens
    0

    Hey BEWD

    Change var mediaFolder = Umbraco.Media(Model.MacroParameters["mediaId"]); to var mediaFolder = Umbraco.TypedMedia(Model.MacroParameters["mediaId"]);

    This will give you back an IPublishedContent. Enumerating the children of the IPublishedContent will give you another IPublishedContent and you can query its properties like you would do with a contentNode. mediaItem.GetPropertyValue("thePropertyNameOfYourLocations")

    Hope this helps you along.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies