Copied to clipboard

Flag this post as spam?

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


  • Nikola Romcevic 26 posts 139 karma points
    Oct 23, 2013 @ 14:47
    Nikola Romcevic
    1

    Get Document Type of Child

    I want to do is something like this:

    @foreach (var child in CurrentPage.Children) {
        switch (child.GetDocumentType){ // Obviously "GetDocumentType" doesn't exsist
            case "Type1":
                // Do magic stuff
                break;
            case "Type2":
                // Even more magic here
                break;
            ...
            ...
        }
    }
    

    Apparently, you can't just type child.DocumentType, I've searched the forums and Google without any luck.

    By my own experiments (I'm fairly new to Umbraco) I found out that one way of getting the document type is to do:

    @foreach (var child in CurrentPage.Children){
        Node n = new Node(child.Id);
        switch(n.NodeTypeAlias){
            ...
        }
    }
    

    So my question is: Is this the best way to go to get the document type of a child?

    I'm trying to avoid doing an if..else if..else.

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Oct 23, 2013 @ 14:53
    Ali Sheikh Taheri
    102

    Hi Nikola,

    you should use this DocumentTypeAlias

    so your code would be like:

    @foreach (var child in CurrentPage.Children) {
        switch (child.DocumentTypeAlias){ 
            case "Type1":
                // Do magic stuff
                break;
            case "Type2":
                // Even more magic here
                break;
            ...
            ...
        }
    }
    
  • Nikola Romcevic 26 posts 139 karma points
    Oct 23, 2013 @ 15:17
    Nikola Romcevic
    1

    Hey Ali!

    I tried that before and got a CS0151: A switch expression or case label must be a bool, char, string, integral, enum, or corresponding nullable type.

    BUT, I don't know why I didn't try this before, if I typecast it to string it works:

    switch((string)child.DocumentTypeAlias){
        ...
    }
    

    Thanks for your help

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Oct 23, 2013 @ 16:15
    Ali Sheikh Taheri
    0

    you are welcome Nikola.

    I am wondering what version of Umbraco are you using? because in V6 the output of child.DocumentTypeAlias is string.

    Thanks

    Ali

  • Nikola Romcevic 26 posts 139 karma points
    Oct 24, 2013 @ 09:54
    Nikola Romcevic
    0

    I'm using 6.1.3.

    Why it's not returning a string is beyond me.

    I made a function of my switch-statement that looks like this:

    string getHtmlInput(DynamicPublishedContent dpc){
        string ret = "";
        switch(dpc.DocumentTypeAlias){
            ...
        }
        return ret;
    }
    

    Here the DocumentTypeAlias outputs a string. I probably did something stupid before :)

  • Nikola Romcevic 26 posts 139 karma points
    Oct 24, 2013 @ 10:11
    Nikola Romcevic
    0

    I noticed this:

    foreach (var child in CurrentPage.Children) {
        switch(child.DocumentTypeAlias) { ... } // NOT WORKING
    }
    
    foreach (var child in Model.Content.Children) {
        switch(child.DocumentTypeAlias) { ... } // WORKING!
    }
    
  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Oct 24, 2013 @ 11:24
    Ali Sheikh Taheri
    0

    alright that makes sense now.

    because CurrentPage is dynamic type whereas the Model.Content is not.

    So the type of child.DocumentTypeAlias is unknown until runtime but the Switch statement needs to know the type before the runtime that's why you where getting error.

    in the 2nd foreach loop the child is not dynamic any more and Child.DocumentTypeAlias returns string

    hope it makes it clear.

    Cheers

    Ali

  • Nikola Romcevic 26 posts 139 karma points
    Oct 24, 2013 @ 12:31
    Nikola Romcevic
    0

    That acctually helps out a lot!

    Thanks for all your help!

Please Sign in or register to post replies

Write your reply to:

Draft