Copied to clipboard

Flag this post as spam?

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


  • James C 17 posts 138 karma points
    Jul 25, 2018 @ 15:37
    James C
    0

    How to obtain all content nodes (published & unpublished) quickly

    I have a project which has a large number of nodes (2000+) and I am creating a custom section which displays them all. Some of the nodes are published and some of them are unpublished.

    I know I can get all nodes with the following line of code:

    var contentList = Services.ContentService.GetChildren(1096);
    

    However, this takes several seconds to load which is too long.

    I stumbled across this great article about the speeds of different methods to query content in Umbraco: https://skrift.io/articles/archive/testing-the-performance-of-querying-umbraco/

    This showed me a method of getting all content nodes much much faster with the following line of code:

    var contentList = Umbraco.TypedContentAtXPath("//blog [@isDoc]");
    

    The only issue with this is that I can't get unpublished nodes this way. Can anyone please help me with a method that obtains unpublished nodes just as fast.

  • Tom Steer 161 posts 596 karma points
    Jul 25, 2018 @ 15:52
    Tom Steer
    0

    Hi James,

    This will be a lengthy process when retrieving a lot of content, as it has to hit the database to get unpublished content. I would recommend using paging in your custom section and then use something like this:

    Services.ContentService.GetPagedDescendants()
    

    Thanks, Tom

  • Harry Spyrou 212 posts 604 karma points
    Jul 25, 2018 @ 16:19
    Harry Spyrou
    0

    Look at this here. Document seems to have a .Published way of finiding the published ones.

    You got me curious so I'm going to try this and come back to you.

  • James C 17 posts 138 karma points
    Jul 26, 2018 @ 12:20
    James C
    0

    Hi Tom,

    thanks for your reply.

    Unfortunately the project is a little too far to change to using paging rather than the tree that is currently being used.

    I'm thinking about just having all content nodes published so that I can access them faster.

  • Ismael 71 posts 354 karma points
    Jul 27, 2018 @ 03:23
    Ismael
    101

    You might be able to get want you want from the index, which might be a bit faster that hitting the db?

    for a rough example:

    var searcher = ExamineManager.Instance.SearchProviderCollection["InternalSearcher"];
    var criteria = searcher.CreateSearchCriteria(BooleanOperation.And);
    var query = "+__IndexType:content";
    var searchQuery = criteria.RawQuery(query);
    var results = searcher.Search(searchQuery);
    
    @foreach (var result in results)
    {
        <p>@result.Fields["nodeName"] @result.Fields["isPublished"]</p>
    }
    

    Notice this is using the Internalsearcher which includes unpublished content but this might also pull back content that is in the recycle bin, however i guess you could put checks in place for that.

    cheers.

  • James C 17 posts 138 karma points
    Jul 27, 2018 @ 09:26
    James C
    1

    Hi Ismael,

    Thank you very much for your response it works like a charm. I ended up with the following code which gets all the nodes I want:

    var searcher = ExamineManager.Instance.SearchProviderCollection["InternalSearcher"];
    var criteria = ExamineManager.Instance.DefaultSearchProvider.CreateSearchCriteria(IndexTypes.Content);
    var query = criteria.Field("nodeTypeAlias", "blog");
    var results = searcher.Search(query.Compile());
    

    Much faster than hitting the db.

  • Marc-André 63 posts 279 karma points
    Nov 01, 2019 @ 19:30
    Marc-André
    0

    Hi James,

    Are you using V8? I try to acheive this and I can't get this to work.

    enter image description here

  • Damien Holley 180 posts 541 karma points
    Sep 02, 2021 @ 22:57
    Damien Holley
    0

    The above is for V7, Umbraco 8 is quite different and you can now inject a lot more into your service. Try injecting IContentPublishedCache or one of the others found in the our umbraco api documentation.

Please Sign in or register to post replies

Write your reply to:

Draft