Copied to clipboard

Flag this post as spam?

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


  • George 28 posts 71 karma points
    Jun 08, 2011 @ 10:32
    George
    0

    Getting all current nodes instead of all current documents

    Hi all;

    I have some C# code like the following:

    public enum DocumentType
        {
            CONTACT = 1221,
            EVENT = 1223,
            JOB = 1227,
            HOME_PAGE = 1256,
        };
    ...later on...
    from d in Document.GetDocumentsOfDocumentType((int)DocumentType.JOB)
    where d.Published && !d.IsTrashed
    select d
    // where document is umbraco.cms.businesslogic.web.Document

    i.e., I get all the current "job" documents for display in a list.

    Unfortunately, I get a lot of "duplicate" documents in the returned list. For example, I get the "same" document with ID 1575, 1568, 1601... etc. I think that most of these are previous revisions of the document.

    Does anyone know how I would change this code to only get "current" documents? I should probably be using something in the "Node" interface for this, but I didn't see a Node.GetNodesOfNodeType method?

    My Umbraco version is 4.6.1.

  • Robert Foster 459 posts 1820 karma points MVP 2x admin c-trib
    Jun 08, 2011 @ 14:01
    Robert Foster
    0

    Bah. Now I have to write all that out again.  So here goes:

    Just using

    from d in Document.GetDocumentsOfDocumentType((int)DocumentType.JOB)
    select d;

    should work ( at least it does in 4.7).  To get the results you are after from the Node framework, you need to write some xpath and check the results:

    var list = new List<Node>();
    var docAlias = new ContentType(DocumentType.JOB).Alias;

    XPathNodeIterator itNode = library.GetXmlNodeByXPath(string.format("\\{0}", docAlias));
    while (itNode.MoveNext()) {
    XmlNode nodeXmlNode = nodeXmlNode = ((IHasXmlNode)itNode.Current).GetNode();
    if (nodeXmlNode != null)
    {
    list.Add(new Node(nodeXmlNode));
    }
    }

    That should do it.  Not as pretty, and you could probably using LinqToXml instead to make it a little neater.  I haven't come across an umbraco library method to do this, but there may be one in uComponents?

    - Rob.

  • George 28 posts 71 karma points
    Jun 17, 2011 @ 07:19
    George
    0

    OK, my bad, the original code works fine - my bug was elsewhere.

    I'm an idiot, sorry for any confusion. :)

Please Sign in or register to post replies

Write your reply to:

Draft