Trying to get Multinode Tree picker property in API Controller
Hello,
I've currently got a API controller, I'm trying to get a Multinode tree picker property inside this controller and return some data for each node. However I'm getting an error when I try to build Umbraco locally. I've gotten to the point where I sucesfully get the siteSettings node, in this content node is a tree picker property called 'pagesToAppearInSearch'. When I try to build the below I get the error message:
error CS1579: foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public instance or extension definition for 'GetEnumerator'
error CS0428: Cannot convert method group 'Name' to non-delegate type 'string'. Did you intend to invoke the method?
error CS0428: Cannot convert method group 'Id' to non-delegate type 'int'. Did you intend to invoke the method?
My api controller:
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Cms.Core;
using Umbraco.Extensions;
using UmbracoProject.Models;
namespace UmbracoProject.Controllers
{
public class PageController : Umbraco.Cms.Web.Common.Controllers.UmbracoApiController
{
private readonly IPublishedContentQuery _contentQuery;
public PageController(IPublishedContentQuery contentQuery)
{
_contentQuery = contentQuery;
}
[HttpGet]
public List<PageModel> Get()
{
List<PageModel> topics = new();
//GETS THE SITE SETTINGS NODE
var settings = _contentQuery.ContentAtRoot().FirstOrDefault(x => x.ContentType.Alias == "siteSettings");
//GET THE MULTINODE TREE PICKER PROPERTY IN THE SITE SETTINGS NODE
var pageToSearch = settings.GetProperty("pagesToAppearInSearch").GetValue();
//LOOP THROUGH THE EACH TREE NODE
foreach (var page in pageToSearch)
{
topics.Add(new()
{
title = page.Name,
url = page.Url(),
id = page.Id,
createdDate = page.CreateDate.ToString()
});
}
return topics;
}
}
}
Can anyone help me understand how I can get a Multi Treenode picker inside my API controller and return the data for the nodes inside the picker?
Thanks for helping, I've changed what you suggested and am still getting the following when trying to rebuild:
(28,58): error CS0246: The type or namespace name 'IPublishedContent' could not be found (are you missing a using directive or an assembly reference?)
(35,34): error CS0428: Cannot convert method group 'Name' to non-delegate type 'string'. Did you intend to invoke the method?
(37,31): error CS0428: Cannot convert method group 'Id' to non-delegate type 'int'. Did you intend to invoke the method?
Trying to get Multinode Tree picker property in API Controller
Hello,
I've currently got a API controller, I'm trying to get a Multinode tree picker property inside this controller and return some data for each node. However I'm getting an error when I try to build Umbraco locally. I've gotten to the point where I sucesfully get the siteSettings node, in this content node is a tree picker property called 'pagesToAppearInSearch'. When I try to build the below I get the error message:
My api controller:
Can anyone help me understand how I can get a Multi Treenode picker inside my API controller and return the data for the nodes inside the picker?
Thanks in advance
Hi Jack,
Making the assumption that
settings
is an IPublishedContent you can get the property value like this:You will then be able to loop around them, but don't forget to null check it incase there are no values.
Thanks
Nik
Hi Nik,
Thanks for helping, I've changed what you suggested and am still getting the following when trying to rebuild:
try to add a namespace:
using Umbraco.Cms.Core.Models.PublishedContent;
That did the trick! Thank you Alex :)
is working on a reply...