Copied to clipboard

Flag this post as spam?

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


  • Sam Aspin 11 posts 31 karma points
    Oct 16, 2013 @ 13:30
    Sam Aspin
    0

    Get all published content where the document type is a child of a specific parent document type

    I have the following situation. I have a 'Basepage' document type from which I create child document types for each of my page types eg. 'homepage', 'genericpage' etc.

    It's easy to find all children of a particular content node but what I want to do is find all children where their document type derives from the 'Basepage' document type.

    So basically anything with a nodetypealias of 'homepage' or 'genericpage' etc.

  • Fuji Kusaka 2203 posts 4220 karma points
    Oct 16, 2013 @ 20:31
    Fuji Kusaka
    0

    Here is how i would do this

    List<DynamicNode> content = @Model.AncestoOrSelf(1).Descendants("Basepage").Where("Visible").Items;
    if(content.Count > 0){
     foreach(var c in content){
    @c.Name
    }
    }
  • Sam Aspin 11 posts 31 karma points
    Oct 16, 2013 @ 21:35
    Sam Aspin
    0

    Thanks Fuji but that will return descendants that have a document type of basepage. I need to identify content where the document type is a child of the basepage document type

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Oct 17, 2013 @ 09:55
    Jeavon Leopold
    0

    Hi Sam, are you using Umbraco v6 and are you using Razor Macros or Mvc?

    Jeavon

  • Sam Aspin 11 posts 31 karma points
    Oct 17, 2013 @ 10:01
    Sam Aspin
    0

    Hey Jeavon

    I'm using v6 with MVC View Engine and happen to be using a Razor view.

    Sam

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Oct 19, 2013 @ 02:24
    Jeavon Leopold
    0

    Hi Sam,

    Really interesting question and thanks to the v6 API this is not too difficult, here is the code all in a view (my master document type alias in this example is "umbHomepage":

    @{
        var cts = ApplicationContext.Current.Services.ContentTypeService;
        var masterDocTypeId = cts.GetContentType("umbHomepage").Id;
    
        var childrenOfMasterDocTypeId = cts.GetContentTypeChildren(masterDocTypeId).Select(x => x.Id).ToList();
        var allContent = Model.Content.AncestorOrSelf().DescendantsOrSelf().Where(x => childrenOfMasterDocTypeId.Contains(x.DocumentTypeId));
    
        foreach (var page in allContent)
        {
            <p>@page.Name</p>
        }
    }
    

    I would definitely recommend that you use a SurfaceController to do this logic and to then pass a model of IEnumerable<IPublishedContent> to a partial which can just do the final foreach loop.

    In a SurfaceController you can directly access the Services without having to go through ApplicationContext as I have done here.

    Hope that is useful to you.

    Jeavon

  • Ryszard Seniuta 3 posts 74 karma points
    Oct 20, 2015 @ 11:11
    Ryszard Seniuta
    0

    What should I use instead of Model if I use UmbracoApiController ?

  • Arjan 21 posts 91 karma points
    Feb 10, 2016 @ 09:10
    Arjan
    0

    hi guys, finding all content of a certain type is pretty standard functionality, when taking into account that all the metadata is available.

    As Umbraco creates & maintains the metadata I would expect standard Umbraco facilities for these kind of tasks.

    Are these facilities already present in the current version 7.3.4? If not, for which version is it planned?

    I'd like to know if it's worthwhile to invest in building my own APP to query the umbraco repository

    thanks A

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Feb 10, 2016 @ 09:49
    Jeavon Leopold
    1

    Hi Arjan,

    Your question isn't really related to this thread (content where the document type is a child of a specific parent document type) but below is the easiest way to get all content of a certain document type.

    @{
        var allBlogPosts = Umbraco.TypedContentAtXPath("//BlogPost");
    }
    @{
        <ul>
            @foreach (var blogPost in allBlogPosts)
            {
                <li>@blogPost.Name</li>
            }
        </ul>
    }
    

    Jeavon

Please Sign in or register to post replies

Write your reply to:

Draft