UmbracoApiController getting all the objects using method "DescendantsOrSelf<T>()"
Hi everyone,
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:
[RoutePrefix("api/test")]
public class TestApiController : UmbracoApiController
{
[HttpGet]
[Route("")]
public IHttpActionResult GetProducts()
{
var testObj = Umbraco.ContentAtRoot().DescendantsOrSelf<TestObject>();
return Json(testObj);
}
}
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?
var testObj = Umbraco.ContentAtRoot().FirstOrDefault().DescendantsOrSelf<TestObject>();
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:
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.
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.
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).
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.
UmbracoApiController getting all the objects using method "DescendantsOrSelf<T>()"
Hi everyone,
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:
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?
Thank you
Hi Marco
I believe you need to use it like this:
Is "TestObject" a doctype in the Umbraco?
Dear Alex,
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?
Thank you
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.
Thank you
Dear Alex,
have you any suggestion?
Thank you
Hi Marco,
From what you've said I understand that
Test Object
has been applied to pageTest using the Compositions menu.Have you tried the following:
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.
Thanks
Nik
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).
Thank you
Marco,
Try to enumerate the
testObject
to an array (liketestObject.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:
Thanks,
Alex
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.
Thank you,
Marco
Hi Marco,
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.
Thanks
Nik
Hi Nik,
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?
Thank you
Dear Nik,
have you any suggestion?
Thank you
Hi guys,
anybody has any suggestion?
Thank you
is working on a reply...