0
votes

I have a setup in Umbraco where I'd like some different document types to have a hierarchical relationship for editors. Hence I configured document type A to allow "children" of document type B. I also configured document type B to allow "children" of document type C.

In a list in a view, I want to show all document of type C that share a specific ancestor of type A (i.e. the immediate parent of type B here is irrelevant). I use something like this code in the controller:

var rootChildren = Umbraco?.TypedContent(RootId)?.Children;
var items = rootChildren
    .SelectMany(rc => rc?.Children?
        .Where(c => c != null
            && c.GetPropertyValue<Boolean>(FieldNames.SomeValue)));
var viewModels = allItems.Select(p => new SomeViewModel
    {
        // Object initialization code
    });
return viewModels.ToArray();

The value of RootId is read from a ConfigurationSection and can thus be changed or non-equal for different environments.

One drawback I feel, is that my code is entangled with the specifics of what I set up for the editors in backoffice. I suppose I could in part solve that by using some "soft" selection method like IEnumerable<IPublishedContent>.DescendantsOrSelf(SomeDocumentTypeAlias). (I suppose being tied up to a document type is a business-as-usual-kind-of-hassle when working with any CMS.)

A "soft" selection method like DescendantOrSelf however does not solve the problem of which node to use as a starting point. Because there can be any number of type A content in the site root, I can't go by the document type for this.

Also, because node ids seem unpredictable with Umbraco, sharing content between developers and other environments, I'm not entirely comfortable with the solution based on configuration, as it's not perfect from a version control perspective.

While reasonably well versed with ASP.NET MVC etc., I don't know that much yet about Umbraco best practices, so this is what I'm asking; How would you as en experienced Umbraco developer go about implementing something like this?

1

1 Answers

0
votes

Not entirely sure I get why this is a problem, but here goes... Sorry if I misunderstood something :-/

AFAIK, you should be able to get what you want from someting like this query:

Umbraco?.TypedContent(ParentId)?.Descendants().OfTypes("yourDocType")

Regarding configuration (how to best get RootId), you could create a node picker property on your root node doctype (or somewhere else relevant to where to list is to be shown) and thus select the node to be used as "root"/parent.

This way, if developers share databases the node id will be set and working, and when you deploy code each environment will have their own id set via the backend/database.

If it's only one node id to be set per site, it's very much a "set it and forget it" scenario, but if several lists are to be used, it's easy for editors to select the parent node themselves.