Copied to clipboard

Flag this post as spam?

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


  • MB 45 posts 229 karma points
    Dec 27, 2018 @ 11:45
    MB
    0

    Find descendants - including unpublished?

    I have an UmbracoAuthorizedJsonController that responds to a request from a BackOffice requestHelper to fetch a list of nodes below a parent.

    Currently using code along these lines, to fetch descendants below the parent nodeID, where the DocumentTypeAlias matches a filter list

    var filter = new[] { "DocumentTypeAlias1", "DocumentTypeAlias2" };
    
    foreach (var node in UmbracoContext.Current.ContentCache.GetById(parentID).Descendants().Where(n => n.IsVisible() && filter.Contains(n.DocumentTypeAlias)))
    {
        //do stuff to build & return list
    }
    

    It works fine for Published content, but now I need to also find Unpublished content... and I'm at a loss at the moment.

    What should I be doing to also include unpublished nodes in my list?

  • Marcio Goularte 312 posts 1046 karma points
    Dec 27, 2018 @ 14:13
    Marcio Goularte
    100

    Unpublished content does not appear in the cache. Only published content is created in the XML cache. You can try to use Examine.

    You need to set supportUnpublished="true" in the indexer you want or you can create a custom indexer.

    https://our.umbraco.com/Documentation/Reference/Config/ExamineSettings/

    And then do the content filter with examine

    https://our.umbraco.com/documentation/reference/searching/examine/quick-start https://24days.in/umbraco-cms/2013/getting-started-with-examine/

    UPDATE

    you can also use contentservice to do this search. But I recommend doing examine, avoid the database calls that contentservice does

    https://our.umbraco.com/documentation/reference/management/services/contentservice/

  • MB 45 posts 229 karma points
    Dec 27, 2018 @ 23:51
    MB
    0

    Cheers - that fixed it.

    Pretty obvious when I stopped and thought about it, that UnPublished nodes would not be in a resultset of IPublishedContent nodes.

    Changing the Descendants query to use ContentService (and the appropriate changes due to handling IContent rather than IPublishedContent nodes) got me out of trouble for the time being, but I'll take a look at how I might be able to use Examine when I get the time.

Please Sign in or register to post replies

Write your reply to:

Draft