Copied to clipboard

Flag this post as spam?

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


  • vinit 10 posts 83 karma points
    Sep 27, 2023 @ 10:56
    vinit
    0

    How to get child item value of document type in surface controller programmatically

    enter image description here

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Sep 27, 2023 @ 11:46
    Marc Goodson
    0

    Hi Vinit

    Inside a SurfaceController you should have access to the Current Umbraco Page that the SurfaceController is being actioned on via

    var newsAndInsightsList = Umbraco.AssignedContentItem;
    

    And you should be able to access the children of the current page using the Children method:

    foreach (var newsArticle in newsAndInsightsList.Children()){
    
    <li>@newsArticle.Name()</li>
    
    }
    

    There is a Razor cheatsheet that shows off different options for querying pages beneath and above the current page that might be a handy resource:

    https://umbra.co/razorCheatsheet

    regards

    marc

  • vinit 10 posts 83 karma points
    Sep 27, 2023 @ 12:17
    vinit
    0

    enter image description here

    I have using this code error found.

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Sep 27, 2023 @ 12:23
    Marc Goodson
    0

    Hi Vinit

    sorry, yes, inside a Surface Controller you should have access to the Current Page via the CurrentPage property eg

    enter image description here

    regards

    Marc

  • vinit 10 posts 83 karma points
    Sep 27, 2023 @ 12:52
    vinit
    0

    enter image description here

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Sep 27, 2023 @ 17:23
    Marc Goodson
    0
    public JsonResult GetClient()
    {
    
    var newsAndInsightsList = CurrentPage;
    

    no need to add the lines 52-56

    because you are inheriting from SurfaceController it means you already have a property called CurrentPage available.

    Big question though, is if you are returning a JsonResult

    Would it be better to have your controller inherit from UmbracoApiController ??

    https://docs.umbraco.com/umbraco-cms/implementation/controllers#umbraco-api-controllers

    if you are calling it from client side code?

    regards

    Marc

  • vinit 10 posts 83 karma points
    Sep 28, 2023 @ 08:01
    vinit
    0

    I have Ajax call and return data in HTML string. but I didn't get the child item from the document type. Can you help me resolve this issue?

    enter image description here

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Sep 28, 2023 @ 08:27
    Marc Goodson
    0

    Hi Vinit

    Sounds like you need to create a controller that inherits from UmbracoApiController and not SurfaceController.

    Because you are making a client request to the endpoint then that endpoint has no concept of what the current page is. (If you are just writing out the children of the news article, it might be easier to just write these out 'server side')

    but if you need to make a front end request then you could update your 'GetClient()' method to accept the Id of the current page eg

    public JsonResult GetClient(string pageId){
    
    var newsAndInsightsList = Umbraco.Content(pageId);
    
    var newsArticles = newsAndInsightsList.Children;
    

    (and then in the page where this is called, use Model.Id to set a data attribute or variable the javascript can read in order to make the request)

    That said... if there is only one News Section on the site then you could avoid passing in the id of the current page by instead querying the Umbraco Tree from the root to find the newsAndInsightsList page eg

    public JsonResult GetClient(){
    
    var newsAndInsightsList = Umbraco.ContentAtRoot().FirstOrDefault().Descendents(f=>f.ContentType.Alias == "newsAndInsightsListDocTypeAlias";
    
    var newsArticles = newsAndInsightsList.Children;
    

    But if you are just using Umbraco to manage content in a headless way, (eg your front end isn't Umbraco and all this talk of the current page is irrelevant) - then the latest version has a new Content Delivery API - https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api which would be a better route if you are implementing a headless pattern.

    regards

    marc

Please Sign in or register to post replies

Write your reply to:

Draft