Get all published content where the document type is a child of a specific parent document type
I have the following situation. I have a 'Basepage' document type from which I create child document types for each of my page types eg. 'homepage', 'genericpage' etc.
It's easy to find all children of a particular content node but what I want to do is find all children where their document type derives from the 'Basepage' document type.
So basically anything with a nodetypealias of 'homepage' or 'genericpage' etc.
Thanks Fuji but that will return descendants that have a document type of basepage. I need to identify content where the document type is a child of the basepage document type
Really interesting question and thanks to the v6 API this is not too difficult, here is the code all in a view (my master document type alias in this example is "umbHomepage":
@{
var cts = ApplicationContext.Current.Services.ContentTypeService;
var masterDocTypeId = cts.GetContentType("umbHomepage").Id;
var childrenOfMasterDocTypeId = cts.GetContentTypeChildren(masterDocTypeId).Select(x => x.Id).ToList();
var allContent = Model.Content.AncestorOrSelf().DescendantsOrSelf().Where(x => childrenOfMasterDocTypeId.Contains(x.DocumentTypeId));
foreach (var page in allContent)
{
<p>@page.Name</p>
}
}
I would definitely recommend that you use a SurfaceController to do this logic and to then pass a model of IEnumerable<IPublishedContent> to a partial which can just do the final foreach loop.
In a SurfaceController you can directly access the Services without having to go through ApplicationContext as I have done here.
Your question isn't really related to this thread (content where the document type is a child of a specific parent document type) but below is the easiest way to get all content of a certain document type.
@{
var allBlogPosts = Umbraco.TypedContentAtXPath("//BlogPost");
}
@{
<ul>
@foreach (var blogPost in allBlogPosts)
{
<li>@blogPost.Name</li>
}
</ul>
}
Get all published content where the document type is a child of a specific parent document type
I have the following situation. I have a 'Basepage' document type from which I create child document types for each of my page types eg. 'homepage', 'genericpage' etc.
It's easy to find all children of a particular content node but what I want to do is find all children where their document type derives from the 'Basepage' document type.
So basically anything with a nodetypealias of 'homepage' or 'genericpage' etc.
Here is how i would do this
Thanks Fuji but that will return descendants that have a document type of basepage. I need to identify content where the document type is a child of the basepage document type
Hi Sam, are you using Umbraco v6 and are you using Razor Macros or Mvc?
Jeavon
Hey Jeavon
I'm using v6 with MVC View Engine and happen to be using a Razor view.
Sam
Hi Sam,
Really interesting question and thanks to the v6 API this is not too difficult, here is the code all in a view (my master document type alias in this example is "umbHomepage":
I would definitely recommend that you use a SurfaceController to do this logic and to then pass a model of
IEnumerable<IPublishedContent>
to a partial which can just do the final foreach loop.In a SurfaceController you can directly access the Services without having to go through ApplicationContext as I have done here.
Hope that is useful to you.
Jeavon
What should I use instead of Model if I use UmbracoApiController ?
hi guys, finding all content of a certain type is pretty standard functionality, when taking into account that all the metadata is available.
As Umbraco creates & maintains the metadata I would expect standard Umbraco facilities for these kind of tasks.
Are these facilities already present in the current version 7.3.4? If not, for which version is it planned?
I'd like to know if it's worthwhile to invest in building my own APP to query the umbraco repository
thanks A
Hi Arjan,
Your question isn't really related to this thread (content where the document type is a child of a specific parent document type) but below is the easiest way to get all content of a certain document type.
Jeavon
is working on a reply...