Return nested content items as JSON with controller
I am trying to learn controllers and have searched a ton without finding a solution to my problem.
I have some meeting dates and times created as nested content in nested content.
The meeting dates being a nested content and the meeting times being nested content inside meeting dates giving a structure like this
> date (24-05-2021)
>> 10:30
>> 12:30
>> 13:30
> date (25-05-2021)
>> 11:30
>> 14:30
>> 15:30
What I am trying to achieve is to create an endpoint with a Umbraco controller to get the times for a specific date chosen on a calendar on the front end.
So if I f.x. select 25-05-2021 in the calendar, the click event should make a call to /umbraco/api/meetingdates/getdates/ with the date 25-05-2021 as the ID to select the content in Umbraco, like ?date=25-05-2021
The date item has the alias MeetingDateItemDay and the time item has meetingdateTimeItem with the overall property containing the items aliased meetingDatesAvailable
With an Api controller, you should just return a normal c# object or list, it will get returned either as xml or json depending on the request, so you can do something similar to below
private List<Markers> GetLocations()
{
List<Markers> locations = new List<Markers>();
var filePath = HostingEnvironment.MapPath("~/App_Data");
using (var streamReader = new StreamReader(Path.Combine(filePath, "MapMarkers.csv")))
using (var reader = new CsvReader(streamReader))
{
// the CSV file has a header record, so we read that first
reader.ReadHeaderRecord();
while (reader.HasMoreRecords)
{
var dataRecord = reader.ReadDataRecord();
var loc = new Markers()
{
Name = dataRecord["title"],
Id = new Guid(dataRecord["id"]), //Convert.ToInt32(dataRecord["id"]),
Description = dataRecord["description"],
Address = dataRecord["address"],
Link = dataRecord["link"],
Categories = dataRecord["category"].Split(','),
Geolocation = new Geolocation() { Latitude = Convert.ToDouble(dataRecord["lat"]), Longitude = Convert.ToDouble(dataRecord["lng"]) }
};
locations.Add(loc);
}
}
return locations;
}
Obviously this isn't reading umbraco data, but the List is returned as a json array of marker objects
Return nested content items as JSON with controller
I am trying to learn controllers and have searched a ton without finding a solution to my problem.
I have some meeting dates and times created as nested content in nested content.
The meeting dates being a nested content and the meeting times being nested content inside meeting dates giving a structure like this
What I am trying to achieve is to create an endpoint with a Umbraco controller to get the times for a specific date chosen on a calendar on the front end.
So if I f.x. select 25-05-2021 in the calendar, the click event should make a call to
/umbraco/api/meetingdates/getdates/
with the date 25-05-2021 as the ID to select the content in Umbraco, like?date=25-05-2021
and return something like
The date item has the alias
MeetingDateItemDay
and the time item hasmeetingdateTimeItem
with the overall property containing the items aliasedmeetingDatesAvailable
Hi,
Don't know if this will help at all, but this is something I wrote to export the metadata from pages.
Just making a 1:1 copy of your code, which I would try to edit gives me
Change the signature of the method from jsonresult to actionresult, that should mix it :)
That unfortunately didn't do the trick :-(
I have pasted my code here
Does that give the same error?
Ah, I see you are using an API controller, mine is in a surface controller so that explains the arror :)
With an Api controller, you should just return a normal c# object or list, it will get returned either as xml or json depending on the request, so you can do something similar to below
Obviously this isn't reading umbraco data, but the List is returned as a json array of marker objects
Thanks a lot, @Huw Reddick - Your answer pointed me in the right direction.
I have created this
Which actually works.
Calling it with
/umbraco/api/meetingdates/MeetingDates/?theDate=26-05-20ยด
give me a response withwhich is what I wanted. Calling a date without any times gives an empty meetingTimes: []
The code above can without a doubt be better! Any suggestions are greatly appreciated!
is working on a reply...