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
    Dec 03, 2020 @ 09:15
    Ulrik Nedergaard
    0

    Umbraco.ContentQuery.Search replacement for controller

    Hi folks

    After upgrade of a project my search controller stopped working ( UmbracoApiController ) since ContentQuery is obsolete.

    var results = Umbraco.ContentQuery.Search(term);
    

    It should be replaced by IPublishedContentQuery but I can't figure out how to access this easily in a controller. There are a some examples on how to do it in a view out there - but not in a controller like this.

  • Matthew Wise 271 posts 1373 karma points MVP 5x c-trib
    Dec 03, 2020 @ 17:08
    Matthew Wise
    100

    Hi Ulrik,

    You need to use constructor injection on the ApiController as below

    public class YourApiController : UmbracoApiController
    {
        private IPublishedContentQuery _contentQuery;
    
        public YourApiController(IPublishedContentQuery contentQuery)
        {
            _contentQuery = contentQuery;
        }
    
    
        public IHttpActionResult Search(string term)
        {
            // Umbraco.ContentQuery.Search(term);
            _contentQuery.Search(term);
    
            // rest of the code
        } 
    }
    

    You can also access this using Current.PublishedContentQuery.Search(term) However where possible using the injected method is preferable.

    Hope this helps

    Matt

  • Ulrik Nedergaard 44 posts 175 karma points
    Dec 03, 2020 @ 21:58
    Ulrik Nedergaard
    0

    Thanks Matt - it works :)

    However I realized that my previous code wasn't the issue but something further down in the code.

    But hey ... with your code I'm more ready for future upgrades if the previous obsolete classes gets removed :)

Please Sign in or register to post replies

Write your reply to:

Draft