Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Marco Graziotti 40 posts 166 karma points c-trib
    Apr 09, 2021 @ 19:48
    Marco Graziotti
    0

    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. enter image description here 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

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Apr 09, 2021 @ 20:08
    Alex Skrypnyk
    0

    Hi Marco

    I believe you need to use it like this:

    var testObj = Umbraco.ContentAtRoot().FirstOrDefault().DescendantsOrSelf<TestObject>();
    
  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Apr 09, 2021 @ 20:10
    Alex Skrypnyk
    0

    Is "TestObject" a doctype in the Umbraco?

  • Marco Graziotti 40 posts 166 karma points c-trib
    Apr 10, 2021 @ 19:00
    Marco Graziotti
    0

    Dear Alex,

    thank you for your reply. I tried to use:

    var testObj = Umbraco.ContentAtRoot().FirstOrDefault().DescendantsOrSelf<TestObject>();
    

    But nothing has changed :( Here you can see the Visual Studio "Watch":
    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: Page Test Here you can see the "Page Test" published under a "Root" node: Content Root

    Do you think that I did something wrong?

    Thank you

  • Marco Graziotti 40 posts 166 karma points c-trib
    Apr 10, 2021 @ 19:16
    Marco Graziotti
    0

    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: Visual Studio Watch Inside method "Results View": Results View The problem remains the same because it's impossible for me to retrieve those fields.

    Thank you

  • Marco Graziotti 40 posts 166 karma points c-trib
    Apr 14, 2021 @ 16:10
    Marco Graziotti
    0

    Dear Alex,

    have you any suggestion?

    Thank you

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Apr 14, 2021 @ 16:28
    Nik
    2

    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:

    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.

    Thanks

    Nik

  • Marco Graziotti 40 posts 166 karma points c-trib
    Apr 14, 2021 @ 17:32
    Marco Graziotti
    1

    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

  • Ulf Möllerström 69 posts 246 karma points
    Apr 15, 2021 @ 10:53
    Ulf Möllerström
    0

    Marco,

    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).

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Apr 14, 2021 @ 16:49
    Alex Skrypnyk
    1

    Hi Marco and Nik

    Another way is to go with Page Test doctype:

    var pageTest = Umbraco.ContentAtRoot().FirstOrDefault().DescendantsOrSelf<PageTest>();
    

    Thanks,

    Alex

  • Marco Graziotti 40 posts 166 karma points c-trib
    Apr 14, 2021 @ 17:27
    Marco Graziotti
    2

    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

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Apr 14, 2021 @ 20:28
    Nik
    1

    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

  • Marco Graziotti 40 posts 166 karma points c-trib
    Apr 15, 2021 @ 21:55
    Marco Graziotti
    0

    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

  • Marco Graziotti 40 posts 166 karma points c-trib
    Apr 19, 2021 @ 07:24
    Marco Graziotti
    0

    Dear Nik,

    have you any suggestion?

    Thank you

  • Marco Graziotti 40 posts 166 karma points c-trib
    Apr 21, 2021 @ 12:38
    Marco Graziotti
    0

    Hi guys,

    anybody has any suggestion?

    Thank you

Please Sign in or register to post replies

Write your reply to:

Draft