Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
I searched in this forum but I didn't find any answer related to "DescendantsOrSelf" method.
I'm trying to find the best way to retrieve all the objects of type T, using the UmbracoApiControrller.
I have this custom controller:
public class TestApiController : UmbracoApiController
public IHttpActionResult GetProducts()
var testObj = Umbraco.ContentAtRoot().DescendantsOrSelf<TestObject>();
And I'm trying to retrieve all the TestObject from the BO, but "testObj" it's always null.
TestObject it's a "PublishedContentModel", and I also published a page in the BO, and filled this "Test Page" with some data, but nothing has changed.
What am I doing wrong? What's the best way to get all T objects using UmbracoApiController?
I believe you need to use it like this:
var testObj = Umbraco.ContentAtRoot().FirstOrDefault().DescendantsOrSelf<TestObject>();
Is "TestObject" a doctype in the Umbraco?
thank you for your reply.
I tried to use:
But nothing has changed :(
Here you can see the Visual Studio "Watch":
Yes, "Test Object" it's an Umbraco Document Type, and it's nested - using composition - inside a "Page Test" Document Type. Here you can see the BO view:
Here you can see the "Page Test" published under a "Root" node:
Do you think that I did something wrong?
A quick update...
I stopped Visual Studio with a breakpoint after the "testObj" variable, and after three minutes, something appeared in "source" field. Here is what I saw:
Inside method "Results View":
The problem remains the same because it's impossible for me to retrieve those fields.
have you any suggestion?
From what you've said I understand that Test Object has been applied to pageTest using the Compositions menu.
Have you tried the following:
var testObj = Umbraco.ContentAtRoot().FirstOrDefault().DescendantsOrSelf<ITestObject>();
Compositions are applied to the underlying models of the completed doc type as interfaces rather than as concrete classes, else it would be impossible to inherit from multiple compositions because of .Net behaviours.
Thank you Nik,
it make sense, I need to use the interface because of the inheritance (Composition).
It works! :)
And also using the "PageTest" class works (as suggested by Alex).
Try to enumerate the testObject to an array (like testObject.ToArray()) to list the descendants in debug mode.
Also: Umbraco says that using .Children on an object is better than .Decendant* for performance reasons (I'm almost always using "decendants" or "ancestors" my self).
Hi Marco and Nik
Another way is to go with Page Test doctype:
var pageTest = Umbraco.ContentAtRoot().FirstOrDefault().DescendantsOrSelf<PageTest>();
Dear Nik and Alex,
both solutions are working :)
In this case, the result object that I will get, it will be returned by the in memory cache, or all the times the method "DescendantsOrSelf" will query the DB? Because in this case I will face with a performance problem.
That approach you are using there should not be touching the database. The use of Umbraco.ContentAtRoot() will be accessing the PublishedContentCache so you should be good to go. However, the use of DescendantOrSelf / DecendantsOrSelf is not necessarily the most performant approach to use as it will search your entire content tree every time it is executed. It won't stop when it finds the first item I don't believe.
The performance of this much better in v8 that it was in v7, but if there are other ways to find what you are after I'd potentially consider that. Without a full understanding of your overall objective though, it is hard to fully advise.
thank you for your reply.
I need to retrieve or the entire Document Type of a Web Page, like "Page Test" (see the picture in my previous post), or in other cases I need to retrieve the Document Type of a component inside the Web Page (like what you suggested me with "ITestObject").
Doing this, I need to be sure that all the contents returned will be served by the Umbraco's cache.
Is there a better way than to use "DescendatsOrSelf" method?
anybody has any suggestion?
is working on a reply...
Write your reply to:
Image will be uploaded when post is submitted