Copied to clipboard

Flag this post as spam?

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


  • Dav 44 posts 69 karma points
    Dec 03, 2009 @ 16:30
    Dav
    1

    List all nodeIds by document type

    Gosh, I feel I somehow will embarass myself pressing the "submit" button but having looked at the forum and API still cannot figure out what the answer is. What I'm trying to do is get all nodes that are of a specific doctype:

    DocumentType dt = DocumentType.GetByAlias("ContentPage");
    int[] allNews = Document.getAllUniqueNodeIdsFromObjectType(dt.UniqueId);

    But this returns an empty array with striking regularity. I looked at an earlier post by Niels (http://forum.umbraco.org/yaf_postst8558_getAllUniqueNodeIdsFromObjectType.aspx) but still not sure what he means.

    Is there a way to do what I want with some consise magic or do I need to iterate over all nodes the good ol' way? Apologies if it's something obvious, today marks my first contact with the Umbraco API.

  • Dav 44 posts 69 karma points
    Dec 04, 2009 @ 16:28
    Dav
    0

    Let me answer my own question before someone chips in :-) Just as Niels said, and as I finally got to understand, unique id is not a doctype as we know it. User-created doctypes too uniqueIDs too. But getAllUniqueNodeIdsFromObjectType only filters over a field descibing a much more general "doctype" id, one that was created "before all time" - eg. a "webpage", a "macro", a "stylesheet". So pluging a unique id of a user doctype will always result in an empty result set.

    Perhaps something along the lines of how I initially understood the method will be possible one day :-) But until then it's good ol' recursion.

  • Dennis Milandt 190 posts 517 karma points
    Dec 10, 2009 @ 10:22
    Dennis Milandt
    2

    Are you looking to get the NodeIds of all published content nodes by their DocumentType Alias?

    In that case you can do this:

    var nodeIds = GetNodeIdsByAlias("ContentPage", 0)
            /// <summary>
            /// Get List of Node IDs by DocumentType Alias.
            /// </summary>
            /// <param name="nodeTypeAlias">DocumentType alias to match.</param>
            /// <param name="startNodeId">Select from this Nodes descendants.</param>
            public static List<int> GetNodeIdsByAlias(string nodeTypeAlias, int startNodeId)
            {
                List<int> nodeIds = new List<int>();
                string start = startNodeId > 0 ? string.Format("//node[@id = {0}]", startNodeId) : "";
                XPathNodeIterator it = umbraco.library.GetXmlNodeByXPath(string.Format(start + "//node[@nodeTypeAlias = '{0}']", nodeTypeAlias));
                while (it.MoveNext())
                {
                    int nodeId = Convert.ToInt32(it.Current.GetAttribute("id", it.Current.NamespaceURI));
                    nodeIds.Add(nodeId);
                }
                return nodeIds;
            }

     

    /Dennis Milandt

  • Bruno 30 posts 50 karma points
    Jun 14, 2010 @ 18:12
    Bruno
    0

    Hi,

    above, the example is for published nodes, however i need the oposite ( Not published )..

     

    What i need change for get that?

     

    thanks

Please Sign in or register to post replies

Write your reply to:

Draft