Copied to clipboard

Flag this post as spam?

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


  • Ulrik Nedergaard 44 posts 175 karma points
    May 06, 2020 @ 13:42
    Ulrik Nedergaard
    0

    Hi

    I'm trying to make an api that returns a list ( json ) of the result of an examine search. This doesn't work. It says it can't serialize PublishedSearchResult.

    Any ideas - or maybe a different and better way to approach it?

    public class SearchController : UmbracoApiController
    {
        [HttpGet]
        public IEnumerable<PublishedSearchResult> GetResults(string term)
        {
            var result = Umbraco.ContentQuery.Search(term);
    
            return result;
        }
    
    
    }
    
  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    May 06, 2020 @ 14:02
    Ismail Mayat
    100

    Ulrik,

    That wont work, you need to maybe create your own pocos and transform your results to list of those pocos and return those.

    Regards

    Ismail

  • Ulrik Nedergaard 44 posts 175 karma points
    May 07, 2020 @ 13:52
    Ulrik Nedergaard
    0

    Thanks Ismail

    That works :)

    [HttpGet]
        public JsonResult<List<Node>> GetResults(string term)
        {
            var result = Umbraco.ContentQuery.Search(term);
            List<Node> nodes = new List<Node>();
    
            foreach(var item in result)
            {
                nodes.Add(new Node
                {
                    Name = item.Content.Name
                });
            }
    
            return Json( nodes);
        }
    
    
    }
    
    public class Node
    {
        public string Name { get; set; }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft