Copied to clipboard

Flag this post as spam?

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


  • Mr A 216 posts 278 karma points
    Aug 01, 2013 @ 01:49
    Mr A
    0

    Get NodeId by Document Type in Umbraco 6

    Hi , 

    I am trying to retrieve nodeId by document type with no success , I have tried the following code but none of them works , what i need to do, is to display children of node whose parent document type is folder,  also i have got 2 languages ,  below is the tree of my content nodes:

    -EN

    --Contact

    --Products[document type = folder]

    ---Product1[document type = folder1]

    ---Product2[document type = folder2]

    --Information

    -ES

    --Contact

    --Products[document type = folder]

    ---Product1[document type = folder1]

    ---Product2[document type = folder2]

    --Information

    Now the problem is I cant use fixed node to display number of products as it wont accross multi language so i have to use it dynamically to get the node id of particular type and then get the children of that doucment type and display in respective language.

    Below is the code i tried with no success:

    var parent = @Umbraco.TypedContent(1206);//wont work for multi languages as id is fixed
    var locationNodes =Model.AncestorOrSelf(1).Descendants("Folder");
        // Get root node:
        var root = Model.AncestorOrSelf(1);
    
        // Get all descendants, filter by type:
        var nodes = root.Descendants("Folder");
        var test = Model.DescendantsOrSelf("Folder");
       var anotherTest = Model.DescendantsOrSelf(1).Where(n => n.DocumentTypeAlias == "Folder");
       var productNodes = @Model.AncestorOrSelf(1).Descendants("FolderFabric");

    None of the above works apart from fixed id one , any suggestions or any assistance will be appreciated 

    Thanks

  • Jeavon Leopold 3002 posts 13185 karma points MVP 6x admin c-trib
    Aug 01, 2013 @ 12:15
    Jeavon Leopold
    0

    Hi, looks like you are using a partial view, what Model are you passing to it?

    I think that var locationNodes = Model.AncestorOrSelf(1).Descendants("Folder"); should work just fine assuming your model is the current page of IPublishedContent

  • Jeavon Leopold 3002 posts 13185 karma points MVP 6x admin c-trib
    Aug 01, 2013 @ 12:16
    Jeavon Leopold
    0

    Oh, one thing to check is the capitalization of your document type alias, is it "Folder" or "folder"

  • Mr A 216 posts 278 karma points
    Aug 01, 2013 @ 13:25
    Mr A
    0

    Yes i am using the partial view passing

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<IPublishedContent>
    

    and call the partial view

    @Html.Partial("Products", @Model.Content)<
    

    And i have rechecked the alias.

  • Jeavon Leopold 3002 posts 13185 karma points MVP 6x admin c-trib
    Aug 01, 2013 @ 13:47
    Jeavon Leopold
    100

    Ok, I just this little snippet a test and it works with the structure you have described:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<IPublishedContent>
    @{
        var locationNodes = Model.AncestorOrSelf(1).Descendants("Folder");
        if (locationNodes.Any())
        {    
            foreach (var publishedContent in locationNodes.First().Children)
            {
                <p>@publishedContent.Name</p>
            }
        }
    }
    
  • Mr A 216 posts 278 karma points
    Aug 01, 2013 @ 13:49
    Mr A
    0
    @{
    var productNode = @Model.AncestorOrSelf(1).Children.Where(x => x.DocumentTypeAlias == "Folder").First().Children;
    }
    
    @foreach(var page in productNode)
                {//content
    }
    

    The above code is working. Tried your code which is also working thanks

  • Jeavon Leopold 3002 posts 13185 karma points MVP 6x admin c-trib
    Aug 01, 2013 @ 13:52
    Jeavon Leopold
    0

    Ah cool, it's a good idea to do the Any() check just in case a site doesn't yet have a "Folder", otherwise the First() would YSOD.

Please Sign in or register to post replies

Write your reply to:

Draft