Copied to clipboard

Flag this post as spam?

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


  • Johan Alkstål 12 posts 113 karma points
    Mar 05, 2018 @ 09:46
    Johan Alkstål
    0

    Getting different published content to a common page

    I have a home page (HomeController inherits from RenderMvcController) where I want to list published content from two different document types.

    I've been searching and searching for help on how I can access the published content of each document type through the UmbracoHelper in the controller, but found nothing.

    So I would appreciate any help on how this is best achieved?

    My level is Umbraco newb, be gentle. :)

  • David Hutson 48 posts 378 karma points
    Mar 05, 2018 @ 10:34
    David Hutson
    100

    Hi Johan,

    1. You need the DocumentTypeAlias for each document type.
    2. If the document types exist under a specific list then it is more efficient to target the parent and then use linq to loop over the child nodes and return the ones that match. If they live in different places within the content tree then you will have to iterate down from the root content node. I have included a code snippet for scenario one.

    UmbracoHelper documentation is pretty good. For the scenario where the types live under a list this should work.

    var helper = new UmbracoHelper(UmbracoContext.Current);
    var rootnode = helper.TypedContent(LIST_PARENT_NODE_ID);
    var matchingnodes = rootnode.Children.Where(c => c.DocumentTypeAlias == "DOCUMENT_TYPE_ALIAS");
    

    I hope this helps

  • Johan Alkstål 12 posts 113 karma points
    Mar 05, 2018 @ 10:43
    Johan Alkstål
    0

    Hi David and thanks for helping out!

    One question, what is LIST_PARENT_NODE_ID? I understand that it is a guid but what does it represent and where does it come from?

  • David Hutson 48 posts 378 karma points
    Mar 05, 2018 @ 10:57
    David Hutson
    0

    Hi,

    The TypedContent method accepts a integer which represents the Content NodeID. Every page you create in Umbraco is assigned a unique NodeID. You can get the ID for the properties tab or info tab depending on which version of Umbraco 7.X.X

    In my code snippet LISTPARENTNODE_ID is just the NodeID for the list page. For example if you had a page called blog (list) that has child blog articles then you would pass the TypedContent method the NodeID for the blog (list) page then loop over the child blog articles and return them as a collection of IPublishedContent objects to use for display on the page.

  • Johan Alkstål 12 posts 113 karma points
    Mar 05, 2018 @ 14:39
    Johan Alkstål
    0

    Okay thanks David! It works.

    I'm hitting another problem but it's not really related to my initial question.

    Since all objects are of IPublishedContent and the properties I need are on concrete classes found on runtime in Umbraco.Web.PublishedContentModels.MyDocType, I don't know how I can access those properties on class and not the interface?

  • David Hutson 48 posts 378 karma points
    Mar 05, 2018 @ 16:07
    David Hutson
    0

    Hi,

    You just need to cast your IPublishedContent into a concreate type. You can do this with linq as long as you setup the MyDocType class in your Model to inherit from PublishedContentModel. If you are allowing Umbraco to autogenerate the model you should be able to implement this using a parital class with the same name. You will need to use the same namespace on your class as Umbraco e.g. Umbraco.Web.PublishedContentModels

    Below is the Linq you need.

    var matchingnodes = rootnode.Children.Where(c => c.DocumentTypeAlias.Equals("DOCUMENT_TYPE_ALIAS")).OrderBy(x => x.Name).Select(t => new MyDocType(t));
    
  • Dan Diplo 1490 posts 5791 karma points MVP 4x c-trib
    Mar 05, 2018 @ 19:12
    Dan Diplo
    0

    Or you can just use the generic overload...

    var matchingnodes = rootnode.Children<MyDocType>();
    
  • Dan Diplo 1490 posts 5791 karma points MVP 4x c-trib
    Mar 05, 2018 @ 12:45
    Dan Diplo
    0

    I think you can make the queries simpler...

    If the document types are descendants of your Home Controller (which they almost certainly are) then you can do this in your render controller (assuming "yourAlias" is the alias of the document type you wish to find and there is only one of them):

    var page = CurrentPage.Descendant("yourAlias");
    

    That will return you a single IPublishedContent instance of the first page with an alias of "yourAlias" that is a descendant of your home page.

    If your page is a child page (ie. exactly one level deeper than the home page) then this will be more efficient:

    var page = CurrentPage.Children("yourAlias");
    

    If you need to get multiple document types you can use:

    var pages = CurrentPage.Descendants("yourAlias");
    

    This will return IEnumerable<IPublishedContent>

    If it's a specific page you require and you know the ID and don't mind hardcoding this then you can also do:

    var page = Umbraco.TypedContent(1234);
    

    (Where 1234 is the ID of the page).

    Note: In a render controller you already have access to the UmbracoHelper and other contexts - you don't need to recreate these.

Please Sign in or register to post replies

Write your reply to:

Draft