Copied to clipboard

Flag this post as spam?

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


  • Thomas Ravnholt 11 posts 101 karma points
    Aug 10, 2022 @ 13:58
    Thomas Ravnholt
    0

    Looping MultiNodeTreePicker content from macro parameters

    Hi everyone.

    I'm having issues trying to loop out content from a macro, inside a richtext editor.

    I have done the following

    1. Created a New partial view macro named "DownloadFiler"
    2. Inside the Macro DownloadFiler i have added a parameter of Umbraco.MultiNodeTreePicker with alias of "vaelgFiler"
    3. Under settings i've added have toggled on Use in rich text editor and the grid
    4. Lastly i have added the macro in a RichText, and picked some pages from the page.

    Now to my issue

    Inside my macro partial view DownloadFiler.cshtml i want to loop out the nodes i have picked.

    In the Umbraco Docs i found Multinode Treepicker but that won't work since im using a macro with parameters.

    How do i modify the following to work with macro parameters?

    @{
        var typedMultiNodeTreePicker = Model.Value<IEnumerable<IPublishedContent>>("featuredArticles");
        foreach (var item in typedMultiNodeTreePicker)
        {
            <p>@item.Name</p>
        }
    }
    
  • Lucas Bisgaard 19 posts 128 karma points c-trib
    Aug 16, 2022 @ 21:38
    Lucas Bisgaard
    1

    Hello Thomas,

    It will look something like this:

    @{
         string typedMNPAsString = Model.MacroParameters["vaelgFiler"].ToString();
         List<string> contents = new List<string>();
    
         if (!string.IsNullOrWhiteSpace(typedMNPAsString))
         {
              contents = typedMNPAsString.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
         }
    
         foreach (string contentId in contents )
         {
               IPublishedContent item = Umbraco.Content(contentId);
               <p>@item.Name</p>
         } 
    }
    

    All Macro parameters you set, you need to extract the value by using the Model.MacroParameters[alias] for this the Multi Node Picker, the data is stored a string Array of node ids. Therefor we need to split it, to an list Ids we can each over and one by one convert it from Id to content.

  • Thomas Ravnholt 11 posts 101 karma points
    Aug 22, 2022 @ 07:33
    Thomas Ravnholt
    0

    Hello Lucas! Thanks alot, it works perfectly!

Please Sign in or register to post replies

Write your reply to:

Draft