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.
How to get child item value of document type in surface controller programmatically
Hi Vinit
Inside a SurfaceController you should have access to the Current Umbraco Page that the SurfaceController is being actioned on via
And you should be able to access the children of the current page using the Children method:
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
I have using this code error found.
Hi Vinit
sorry, yes, inside a Surface Controller you should have access to the Current Page via the CurrentPage property eg
regards
Marc
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
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?
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
(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
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
is working on a reply...