Copied to clipboard

Flag this post as spam?

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


  • ianhoughton 281 posts 605 karma points c-trib
    Feb 21, 2013 @ 17:58
    ianhoughton
    0

    Get only visible nodes using API

    I have the following code which is working great apart from it's returning nodes that aren't published. Where would I need to tweak it ? I presume I need a .Where("Visible") somewhere but not sure.

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    var currentNode = Node.GetCurrent();
    
                    // Retrieve the latest N articles from the parent folder
                    var nodes = FindChildren(currentNode, t => (t.NodeTypeAlias.Equals("NewsArticle")));
                    var nodesOutput = nodes.OrderByDescending(n => n.GetProperty("newsDate").Value).Take(numberOfArticles);
    
                    newsRepeater.DataSource = nodesOutput;
                    newsRepeater.DataBind();
                }
            }
    private static List<Node> FindChildren(Node currentNode, Func<Node, bool> predicate)
            {
                List<Node> result = new List<Node>();
    
                var nodes = currentNode
                    .Children
                    .OfType<Node>()
                    .Where(predicate);
                if (nodes.Count() != 0)
                    result.AddRange(nodes);
    
                foreach (var child in currentNode.Children.OfType<Node>())
                {
                    nodes = FindChildren(child, predicate);
                    if (nodes.Count() != 0)
                        result.AddRange(nodes);
                }
                return result;
            }
  • Rich Green 2246 posts 4008 karma points
    Feb 21, 2013 @ 18:00
    Rich Green
    0

    I don't know the answer but 'Visible' is not related to published state, it's to do with having umbracoNaviHide set to true / false.

    What verison of Umbraco are you using?

    Rich

  • ianhoughton 281 posts 605 karma points c-trib
    Feb 21, 2013 @ 18:21
    ianhoughton
    0

    Sorry should have said 4.9.1.

  • Rich Green 2246 posts 4008 karma points
    Feb 21, 2013 @ 18:30
    Rich Green
    0

    Hey Ian,

    I'm not doing enough coding these days, but is this relevant?

    http://our.umbraco.org/wiki/reference/api-cheatsheet/difference-between-node-and-document

    Rich

Please Sign in or register to post replies

Write your reply to:

Draft