Copied to clipboard

Flag this post as spam?

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


  • Andrew 25 posts 148 karma points
    May 28, 2019 @ 14:51
    Andrew
    0

    Get Content of a particular document type

    Hi,

    Is there a way to get all content nodes of a particular document type?

    Sure it should be simple but can't seem to find anything for V8.

    Many thanks,

    Andrew

  • Frans de Jong 548 posts 1840 karma points MVP 3x c-trib
    May 28, 2019 @ 15:09
    Frans de Jong
    105

    IENumerable<IPublishedContent> allContentOfType = Umbraco.ContentAtXpath("//documentTypeAlias") should still work.

    I don't know how it performs in V8.

  • Bryna 73 posts 259 karma points
    May 28, 2019 @ 19:26
    Bryna
    1

    Below also will work; however, it is leveraging LINQ as opposed to xpath, so you might read up on https://our.umbraco.com/documentation/Reference/Common-Pitfalls/. I am not sure if the statements regarding LINQ/Xpath still apply, but they are certainly worth keeping in mind.

            IEnumerable<IPublishedContent>
            imageRotator = Model.PublishedContent().Descendants().Where(x => x.ContentType.Alias.ToLower() == "{documentalias}")
    
  • Alex 18 posts 90 karma points
    May 30, 2019 @ 09:11
    Alex
    1

    Andrew, I guess you are looking for "DescendantsOfType"? Model.DescendantsOfType("myDocType")

  • Frans de Jong 548 posts 1840 karma points MVP 3x c-trib
    May 30, 2019 @ 09:13
    Frans de Jong
    0

    I think descendants is more expensive than the xpath solution. It searched the entire tree and gets the items you are looking for.

    Xpath gets the types directly from cache. At least in v7

  • George Phillipson 108 posts 287 karma points
    May 30, 2019 @ 14:34
    George Phillipson
    1

    XPath is a good option, I'm using it on a site I'm building at the moment, as an example in my layout file I have:

    int cmsHomePageId   = UmbracoContext.ContentCache.GetByXPath("//home").First().Id;
    

    I then pass the ID to another class to return some data.

    If you have a quick look at https://our.umbraco.com/documentation/Reference/Common-Pitfalls/ and have a quick read of Too much LINQ - XPath is still your friend which explains XPath well

  • Andrew 25 posts 148 karma points
    Jun 04, 2019 @ 14:34
    Andrew
    0

    Thanks for your help guys, using Xpath did the job. Much appreciated and thanks for the extra links, will give them a read :)

Please Sign in or register to post replies

Write your reply to:

Draft