Copied to clipboard

Flag this post as spam?

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


  • Bex 444 posts 555 karma points
    Apr 08, 2014 @ 12:36
    Bex
    0

    New way to get all documents by type

    Hi

    I have been away from Umbraco (and programming) for a long while and now I'm back, Umbraco's changed.

    I want to get all documents by a document type and access all their properties.

    I can't remember the way I used to do this, nor find any documentation of how to do it now as I can see somethings like DocumentType are now obselete?

    I am using Umbraco 6.1.6 (the last version I used was 4. something..)

     

  • Stefan Kip 1613 posts 4130 karma points c-trib
    Apr 08, 2014 @ 12:39
    Stefan Kip
    0

    If you're using C#, these Extension Methods I wrote might be helpful to you:

    /// <summary>
    /// Returns all child nodes with the specified nodeTypeAlias, starting from the current node
    /// </summary>
    /// <param name="node">The node from where to search</param>
    /// <param name="nodeTypeAlias">The nodeTypeAlias to search for</param>
    /// <param name="recursive">True to search in all descendant, false to search only in children</param>
    /// <returns>A collection of nodes with the specified nodeTypeAlias</returns>
    public static IEnumerable<Node> GetNodesByTypeFromCurrent(this INode node, string nodeTypeAlias, bool recursive = false)
    {
        if (node == null)
            throw new ArgumentNullException("node");
    
        if (recursive)
            return node.GetDescendantNodes().Where(x => x.NodeTypeAlias == nodeTypeAlias);
        return node.GetChildNodes().Where(x => x.NodeTypeAlias == nodeTypeAlias);
    }
    
    /// <summary>
    /// Returns all descendant nodes
    /// </summary>
    /// <param name="node">The node</param>
    /// <param name="includeCurrent">Include the current node in the result</param>
    /// <returns>All descendant nodes</returns>
    public static IEnumerable<Node> GetDescendantNodes(this INode node, bool includeCurrent = false)
    {
        if (node == null)
            throw new ArgumentNullException("node");
    
        if (includeCurrent)
            yield return (Node)node;
        foreach (Node child in node.GetChildNodes())
        {
            yield return child;
            foreach (Node subchild in child.GetDescendantNodes())
            {
                yield return subchild;
            }
        }
    }
    
    /// <summary>
    /// Returns the child nodes of this node
    /// </summary>
    /// <param name="node">The node</param>
    /// <returns>The child nodes</returns>
    public static IEnumerable<Node> GetChildNodes(this INode node)
    {
        if (node == null)
            throw new ArgumentNullException("node");
    
        return node.ChildrenAsList.Cast<Node>();
    }
    
  • Bex 444 posts 555 karma points
    Apr 08, 2014 @ 12:46
    Bex
    0

    @kipusoep Thanks for your quick reply.

    This is a bit more in depth than I need I think. Can you still do something like List

    All I have is the document type alias.

    Can you refresh me, what's the difference between a node and document?

  • Stefan Kip 1613 posts 4130 karma points c-trib
    Apr 08, 2014 @ 12:50
    Stefan Kip
    0

    Difference between a Document and a Node is that a Document is used for write operations and a Node is read-only (cached). There's more info about this on the wiki.
    Allways use a Node when the purpose is displaying stuff on a webpage for example.

    What technique are you using for developing webpages? XSLT? Razor/MVC? Usercontrols/masterpages?

  • Bex 444 posts 555 karma points
    Apr 08, 2014 @ 13:14
    Bex
    0

    Usercontrols and masterpages I am using, razon/mvc weren't around when I last delved into anything and can't seem to do what I want in xslt. I am just trying to filter by properties of the pages based on a search param that the user enters in a txt box. The pages I need to filter are of one document type.

  • Jeremy Pyne 106 posts 246 karma points MVP c-trib
    Apr 08, 2014 @ 18:52
    Jeremy Pyne
    0

    myNode.DescendantsOrSelf("noteType")

    or

    ContentService.GetContentOfContentType(ContentTypeService.GetContentType("myType").Id)

     

    Where the first is a node.  The second is using the newer api.

  • Nicholas Westby 2043 posts 7020 karma points c-trib
    Apr 08, 2014 @ 19:37
    Nicholas Westby
    0

    Note that the ContentService will potentially return unpublished nodes and it will hit the database. You typically want to work with UmbracoHelper and the extension methods on IPublishedContent (in an Umbraco view, you typically have access to Model.Content, which is an IPublishedContent), which will only work with the cached nodes. Unless you know what you are doing, avoid the convent service.

  • Alastair Todd 44 posts 142 karma points
    Apr 07, 2018 @ 13:40
    Alastair Todd
    0

    It is perfectly reasonable to want to query all nodes published or not e.g. for reporting purposes in the back office.

Please Sign in or register to post replies

Write your reply to:

Draft