Copied to clipboard

Flag this post as spam?

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


  • ap 8 posts 31 karma points
    Apr 15, 2010 @ 09:34
    ap
    0

    DocumentType descendants

    Hi,

     

    Question - how to programmatically (C# user control) find all descendants of some Master Document Type?

     

    Regards

    AP

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Apr 15, 2010 @ 09:43
    Dirk De Grave
    0

    I don't know if there's a specific API to handle this situation, but you could do with a sql query to find all doc types that inherit from a master doc type.

    var docType = new DocumentType(id);
    bool hasChildren = docType.HasChildren;

    If you need to get the doc types inheriting from a master use this query

    select NodeId from cmsContentType where masterContentType = Id;

    and use the NodeId to construct the child document type

    var childDocType = new DocumentType(NodeId);

    Hope this helps.

    Regards,

    /Dirk

  • ap 8 posts 31 karma points
    Apr 15, 2010 @ 09:52
    ap
    0

    Thanks, but what is cmsContentType? Where to find it?

     

    AP

     

  • ap 8 posts 31 karma points
    Apr 15, 2010 @ 10:11
    ap
    0

    Ok, it is a table :) But are there any helpers to access data in Umbraco API?

     

    AP

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Apr 15, 2010 @ 10:18
    Dirk De Grave
    0

    Nope, checked the code (albeit the v4.0.2 branch) and there's no api to get the inheriting doc types (at least none that I'm aware of), but the sql would do the job.

    Helpers to get the access data in umbraco api? Yes, probably, depends what you define as data (the real document data or doc types/templates etc..)?

    Suggest you look into the online msdn docs available here (not really search friendly tho)

     

    Cheers,

    /Dirk

  • Jesper Hauge 298 posts 487 karma points c-trib
    Apr 15, 2010 @ 10:29
    Jesper Hauge
    1

    Sounds like a task for Linq2Umbraco.

    But this got me thinking, and I have an idea for you, if you're familiar with Linq2Xml.

    First you need a representation of the xmldocument behind all operations in th Umbraco frontend. I noticed that several of the umbraco.library.GetXmlXXXX functions use umbraco.content.Instance.XmlContent, which returns an XmlDocument that represents the entire content as far as I can tell. But XmlDocument is only accesible with XPathNodeIterator style querying, so I made an extension method for XmlDocument that converts it to a XDocument:

    using System.Xml;
    using System.Xml.Linq;
    
    namespace Iqit.UmbracoHelpers.Extensions
    {
        public static class XmlContentExtensions
        {
            public static XDocument ToXDocument(this XmlDocument xmlDocument)
            {
                return XDocument.Load(new XmlNodeReader(xmlDocument));
            }
        }
    }

    If you put this class in a external class library, compile it and place it in the bin folder, you should be able to query your code like so:

    using umbraco;
    using Iqit.UmbracoHelpers.Extensions;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        var doc = content.Instance.XmlContent.ToXDocument();
        var nodes = from n in doc.Descendants("node")
                where (string) n.Attribute("nodeTypeAlias") == "YourDocType"
                select n;
    }

    Note that I said "should be able to" - I haven't tested this, but it compiles, and I'm pretty sure it does what you want.

    Regards
    Jesper Hauge

  • ap 8 posts 31 karma points
    Apr 15, 2010 @ 11:31
    ap
    2

    Well, in the end

    DocumentType.GetAllAsList().Where(t => t.MasterContentType == docType.Id)

    is enough for me :)

    Thanks guys

Please Sign in or register to post replies

Write your reply to:

Draft