Copied to clipboard

Flag this post as spam?

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


  • jacob phillips 130 posts 372 karma points
    Feb 06, 2014 @ 00:26
    jacob phillips
    0

    Get Property via Macro Parameter

    Umbraco 6.1.6

    I have a multi node picker, the name of which I want to pass through macro parameter on a template, like this:

    @Umbraco.RenderMacro("rCprFeature", new {featureOn="Home", featureSize="Large", position="cprLatestNews", numberToDisplay="10", displayImage="1", rssCount="1", useArchiveDate="0", archiveOnly="0", featureTitle="Latest News"})
    

    The "position" parameter, is actually the name of a contnt picker. Inside my Razor Partial View Macro File, I just want to loop through the nodes for that content picker.

    I've already seen how I can get and use each node in the picker:

    var position = Model.MacroParameters["position"];
    
    
            var dynamicPublishedMNTPNodeList = new List<string>();
    
            foreach (var id in CurrentPage.cprLatestNews)
            {
                dynamicPublishedMNTPNodeList.Add(id.InnerText);
            }
            var dynamicMNTPCollection = Umbraco.Content(dynamicPublishedMNTPNodeList);
    
            var recordCount = dynamicMNTPCollection.Count();
    
            if (dynamicMNTPCollection.Any())
            { 
    
                foreach (var item in dynamicMNTPCollection)
                {
                    @item.Id
                }
            }  
    }
    

    But in this case, I want to pass in the value "cprLatestNews" inside the position variable like this:

    foreach (var id in CurrentPage.@position)
    

    But I get:

    Cannot implicitly convert type 'object' to 'System.Collections.IEnumerable'. An explicit conversion exists (are you missing a cast?)
    

    I've also tried:

    foreach (var id in CurrentPage.GetProperty(@position))
    

    and

    foreach (var id in CurrentPage.GetProperty(@position).ToList())
    

    I started reading this but it seems like there should be a simpler way.

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Feb 06, 2014 @ 08:11
    Dennis Aaen
    0

    Hi Jacob,

    Have you tried something like this:

    foreach(var id in CurrentPage.position)

    Here is an example from the build in script List Child Pages From Changeable Source maybe it could help you, to see how it´s done

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @*
        === Macro Parameters To Create ===
        Show:True   Alias:nodeId Name:Node ID    Type:Content Picker
    *@

    @if (Model.MacroParameters["startNodeID"] != null)
    {
        @* Get the start node as a dynamic node *@
        var startNode = Umbraco.Content(Model.MacroParameters["startNodeID"]);
       
        if (startNode.Children.Where("Visible").Any())
        {
            <ul>
                @foreach (var page in startNode.Children.Where("Visible"))
                {
                    <li><a href="@page.Url">@page.Name</a></li>
                }
            </ul>
        }   
    }

    Hope that helps

    /Dennis

  • jacob phillips 130 posts 372 karma points
    Feb 06, 2014 @ 08:35
    jacob phillips
    0

    I tried this:

    foreach(var id in CurrentPage.position)
    

    and no error, but it doesn't return anything. I'll try the other tomorrow. thx

  • jacob phillips 130 posts 372 karma points
    Feb 07, 2014 @ 01:34
    jacob phillips
    0

    Dennis,

    I think this example is not applicable because it is starting with a nodeId (startNodeID)

    In my case, CurrentPage.[the name of my picker] returns some XML, not a node id.

    The value of my 'position' variable is just a text string.

    I even tried CurrentPage.GetProperty("cprLatestNews")

    I can see that that and this one: CurrentPage.cprLatestNews returns:

    <multinodepicker type="content">
    <nodeid>14152</nodeid>
    <nodeid>14142</nodeid>
    <nodeid>14128</nodeid>
    <nodeid>14122</nodeid>
    <nodeid>14113</nodeid>
    <nodeid>14121</nodeid>
    <nodeid>14114</nodeid>
    <nodeid>14100</nodeid>
    </multinodepicker>
    

    But when I try to use either of those with position, @position, "@position" even @CurrentPage.GetProperty(["@]p... I get nothing every time.

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Feb 08, 2014 @ 21:42
    Jeavon Leopold
    0

    Hi Jacob,

    I think you looking for this?

        foreach (var id in CurrentPage.GetPropertyValue(position.ToString()))
    

    Jeavon

  • jacob phillips 130 posts 372 karma points
    Feb 08, 2014 @ 23:15
    jacob phillips
    0

    Thanks, I tried a variation of that earlier, but I still get:

    2014-02-08 14:05:33,104 [7] WARN  umbraco.macro - [Thread 10] Error loading Partial View 
    (file: ~/Views/MacroPartials/rCprFeature.cshtml). Exception: System.NullReferenceException: Object reference not set to an instance of an object.
       at Umbraco.Web.Models.DynamicPublishedContent.TryInvokeMember(InvokeMemberBinder binder, Object[] args, Object& result)
       at CallSite.Target(Closure , CallSite , Object , String )
       at ASP._Page_Views_MacroPartials_rCprFeature_cshtml.Execute() in c:\inetpub\<removed>\Views\MacroPartials\rCprFeature.cshtml:line 103
       at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
       at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
       at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
       at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
       at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
       at Umbraco.Web.Mvc.ControllerExtensions.RenderViewResultAsString(ControllerBase controller, ViewResultBase viewResult)
       at Umbraco.Web.Macros.PartialViewMacroEngine.Execute(MacroModel macro, INode currentPage)
       at umbraco.macro.LoadPartialViewMacro(MacroModel macro)
       at umbraco.macro.renderMacro(Hashtable pageElements, Int32 pageId)
    
  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Feb 10, 2014 @ 11:18
    Jeavon Leopold
    101

    Ah, it seems that the GetPropertyValue method is a extension so not available on the CurrentPage dynamic object. So we need to use the strongly typed object instead:

    @using Umbraco.Core.Dynamics
    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @*
        === Macro Parameters To Create ===
        Show:True   Alias:nodeId Name:Node ID    Type:Content Picker
    *@
    
    @{   
        if (Model.MacroParameters["position"] != null)
        {
            var position = Model.MacroParameters["position"].ToString();
    
            if (Model.Content.HasValue(position))
            {
                var typedMultiNodeTreePicker = new DynamicXml(Model.Content.GetPropertyValue<string>(position));
                var typedPublishedMNTPNodeList = new List<string>();
                foreach (dynamic id in typedMultiNodeTreePicker)                
                {
                    typedPublishedMNTPNodeList.Add(id.InnerText); 
                }
                var typedMNTPCollection = Umbraco.TypedContent(typedPublishedMNTPNodeList).Where(x => x != null);
                foreach (var item in typedMNTPCollection)
                {     
                    <p>sdsd: @item.Name</p>         
                }       
            }        
        }
    }
    
  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Feb 10, 2014 @ 12:23
    Jeavon Leopold
    0

    You can switch back to dynamics if you want to for the output by changing the foreach, e.g.

            foreach (dynamic item in typedMNTPCollection)
            {     
                <p>@item.Name</p>         
                <p>@item.myCustomProperty</p>     
            }   
    
  • jacob phillips 130 posts 372 karma points
    Feb 10, 2014 @ 19:51
    jacob phillips
    0

    Thanks that worked. Understanding more, the mystery unraveling. Thanks Dennis too.

Please Sign in or register to post replies

Write your reply to:

Draft