Copied to clipboard

Flag this post as spam?

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


  • Jack Taylor 16 posts 107 karma points
    Aug 01, 2022 @ 22:37
    Jack Taylor
    0

    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 in advance

  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    Aug 02, 2022 @ 08:44
    Nik
    0

    Hi Jack,

    Making the assumption that settings is an IPublishedContent you can get the property value like this:

    var pageToSearch = setting.Value<IEnumerable<IPublishedContent>>("pagesToAppearInSearch");
    

    You will then be able to loop around them, but don't forget to null check it incase there are no values.

    Thanks

    Nik

  • Jack Taylor 16 posts 107 karma points
    Aug 02, 2022 @ 17:46
    Jack Taylor
    1

    Hi Nik,

    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?
    
  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Aug 02, 2022 @ 20:04
    Alex Skrypnyk
    100

    try to add a namespace:

    using Umbraco.Cms.Core.Models.PublishedContent;

  • Jack Taylor 16 posts 107 karma points
    Aug 02, 2022 @ 20:53
    Jack Taylor
    0

    That did the trick! Thank you Alex :)

Please Sign in or register to post replies

Write your reply to:

Draft