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?