Copied to clipboard

Flag this post as spam?

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


  • Luke 10 posts 132 karma points
    Sep 01, 2022 @ 05:26
    Luke
    0

    The type or namespace name 'ContentAtRoot' does not exist in the namespace 'Umbraco'

    Probably a noob question but I am getting error hen trying to create an API Controller

    The type or namespace name 'ContentAtRoot' does not exist in the namespace 'Umbraco' (are you missing an assembly reference?)

      using using System;
      using Umbraco.Cms.Core.Models.PublishedContent;
      using Umbraco.Cms.Web.Common.Controllers;
      using Umbraco.Core.Models; 
      using Umbraco.Web.Mvc;     
    
    namespace MyWebsite.Core.Controller
    
    public class InsightsController : UmbracoApiController
    
       {
    
        public string InsightsData()
    
        {
    
            var articles = Umbraco.ContentAtRoot().FirstOrDefault().ChildrenOfType("article").Where(x => x.IsVisible());
    
    
            // return some stuff;
        }
      }
    }
    

    What am I missing here?

  • Corné Hoskam 81 posts 588 karma points MVP 3x c-trib
    Sep 01, 2022 @ 08:15
    Corné Hoskam
    100

    Hi Luke,

    You could use the UmbracoHelper to be able to use the ContentAtRoot Method, by injecting the umbracoHelperAccessor into your controller via it's constructor.

    An example snippet that you could use:

      using using System;
      using Umbraco.Cms.Core.Models.PublishedContent;
      using Umbraco.Cms.Web.Common.Controllers;
      using Umbraco.Core.Models; 
      using Umbraco.Web.Mvc;     
    
    namespace MyWebsite.Core.Controller
    
    public class InsightsController : UmbracoApiController
    {
    
        private readonly IUmbracoHelperAccessor _umbracoHelperAccessor;
    
        public InsightsController(IUmbracoHelperAccessor umbracoHelperAccessor){
            _umbracoHelperAccessor = umbracoHelperAccessor;
        }
    
        public string InsightsData()  
        {
            if(_umbracoHelperAccessor.TryGetUmbracoHelper(out var umbracoHelper))
            {
                var articles = umbracoHelper.ContentAtRoot().FirstOrDefault().ChildrenOfType("article").Where(x => x.IsVisible());
            // return some stuff;
            }  
    
        // return some stuff;
    
        }
      }
    }
    

    Hope this helped!

    Kind regards,

    Corné Hoskam

  • Luke 10 posts 132 karma points
    Sep 01, 2022 @ 08:54
    Luke
    1

    Thank you so much! Works great!

Please Sign in or register to post replies

Write your reply to:

Draft