Copied to clipboard

Flag this post as spam?

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

  • Ryan Carlin 1 post 71 karma points
    Dec 04, 2019 @ 16:29
    Ryan Carlin

    Cannot return the IPublishedContent because the UmbracoHelper was not constructed with an IPublishedContent

    I have the below surface controller that has an Ajax call from the UI but when it tries to instantiate the SelectViewModel() it is giving me an error with the UmbracoHelper.

    "System.InvalidOperationException: 'Cannot return the IPublishedContent because the UmbracoHelper was not constructed with an IPublishedContent.'"

    Am I supposed to instantiate the UmbracoHelper again or can I not create ViewModelObjects within an Ajax call? Any help appreciated.

    public class SelectSurfaceController : BaseSurfaceController
        public ActionResult TravellersTemplate(int count)
            var model = new SelectViewModel();
            model.TravellersDatesOfBirth = Enumerable.Range(1, count).Select(x => new DateTime()).ToList();
            return PartialView("_Travellers", model);
    public class SelectViewModel : LayoutViewModel
        public SelectViewModel() : base(Current.UmbracoHelper.AssignedContentItem) { }        
        public SelectViewModel(IPublishedContent content) : base(content) { }
  • Mike Ryan 5 posts 75 karma points
    Oct 15, 2021 @ 02:11
    Mike Ryan

    Did you ever figure this one out Ryan? I was trying to do nearly the same thing, and I'm sure quite a few other people too. They discuss it here and link to some documentation. I personally just created an ApiClient that I could call from a template view and returned a custom ViewModel I created from Shopify product info, completely outside of the Umbraco IPublishedContentModels. A bit of overengineering in my opinion, but I don't pretend to be an expert.

  • ewuski 26 posts 137 karma points
    Jul 27, 2022 @ 12:19

    I have the same issue in Umbraco 10 with the following simple line in a surface controller:

    var aaa = _umbracoHelper.AssignedContentItem;

    where _umbracoHelper was constructed with the DI.

    However SurfaceController has a property CurrentPage. I have just assigned it to my model in the controller.

  • Emma Garland 38 posts 119 karma points MVP 5x c-trib
    Nov 30, 2022 @ 11:18
    Emma Garland

    I got this error message when trying to access the CurrentPage from a surface controller. I needed CurrentPage to access the search settings on the root node. While this worked fine in the code itself, I needed the controller method to be testable so I tried to switch to using the UmbracoHelper approach. That is when I started seeing this error.

    I found the error message in the code line, which says:

    In some cases accessing this property will throw an exception if there is not IPublishedContent assigned to the Helper

    this will only ever happen if the Helper is constructed via DI during a non front-end request

    I tried a lot of things to attempt get this to work and to make my surface controller testable:

    • Firstly by injecting the UmbracoHelper into the controller
    • Next by injecting IUmbracoHelperAccessor and using that to create the UmbracoHelper

    Everything I tried gave me the same error message "'Cannot return the IPublishedContent because the UmbracoHelper was not constructed with an IPublishedContent.". This happened both in the code execution, and the unit tests themselves, despite setting up the expectations with various factories.

    In the end, I abstracted the dependency away by injecting my own IContentRetriever service and accessing the search settings I needed in the SurfaceController, rather than the direct CurrentPage approach.

Please Sign in or register to post replies

Write your reply to: