Copied to clipboard

Flag this post as spam?

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


  • Javz 38 posts 141 karma points
    May 08, 2020 @ 16:20
    Javz
    0

    Get Current page via a Macro?

    I'm trying to get the current page via a macro that is placed in an RTE. When the macro is added into the RTE, it looks for the current page, but I'm assuming this isn't possible as the changes have not been set, therefore can't get the current page?

    I get the following error:

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

    Any suggestions?

    EDIT:

    I'm using the following to get the current page:

    Umbraco.Web.Composing.Current.UmbracoHelper.AssignedContentItem

    However, in the razor file, using Model.Content does get me the current page, but the above gets me a null

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    May 08, 2020 @ 21:45
    Alex Skrypnyk
    0

    Hi Javz

    Can you show me all code you use?

    I use tested this macro partial view code and it works perfectly for me:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{
        var currentPage = Umbraco.AssignedContentItem;
    }
    

    Thanks,

    Alex

  • Javz 38 posts 141 karma points
    May 18, 2020 @ 14:55
    Javz
    0

    I've tried to replicate the issue again:

    Received an error from the server An error occured Cannot return the IPublishedContent because the UmbracoHelper was not constructed with an IPublishedContent. Exception Details System.InvalidOperationException: Cannot return the IPublishedContent because the UmbracoHelper was not constructed with an IPublishedContent.

    And it fails in the following GetCurrentPage method:

    public class UmbracoHelperWrapper : IUmbracoHelper
    {
        private readonly UmbracoHelper _umbracoHelper;
    
        public UmbracoHelperWrapper(UmbracoHelper helper)
        {
            _umbracoHelper = helper ?? throw new ArgumentNullException(nameof(helper));
        }
    
        public IPublishedContent GetCurrentPage()
        {
            return _umbracoHelper.AssignedContentItem;
        }
    }
    

    This happens whenever I call the macro into a page

  • Javz 38 posts 141 karma points
    May 17, 2020 @ 01:14
    Javz
    0

    Hi Alex,

    Thanks for getting back to me!

    Apologies for the late reply.

    In Razor, it works fine, however when trying to use the same line of code in a service class, it throws that error.

    I've had a workaround, where I pass the model within the Razor file to the method (originally, I didn't want to pass but read the current page wherever the method is called)

    The code was the following in the service class:

    public Service(IUmbracoHelper umbracoHelper)
    {
        _umbracoHelper = umbracoHelper ?? throw new ArgumentNullException(nameof(umbracoHelper));
    }
    
    public Item GetPageDetails()
    {
        var home = _umbracoHelper.GetCurrentPage().Parent
    }
    
  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    May 18, 2020 @ 15:22
    Alex Skrypnyk
    0

    Hi Javz

    I believe if you inject UmbracoHelper - you will not get null there.

    Thanks,

    Alex

  • Javz 38 posts 141 karma points
    May 18, 2020 @ 16:10
    Javz
    0

    Hi Alex,

    Correct me if I'm wrong, but I do believe it has been injected?

    In the UmbracoHelperWrapper class, UmbracoHelper is injected as the following:

    public class UmbracoHelperWrapper : IUmbracoHelper
    {
        private readonly UmbracoHelper _umbracoHelper;
    
        public UmbracoHelperWrapper(UmbracoHelper umbracoHelper)
        {
            _umbracoHelper = umbracoHelper ?? throw new ArgumentNullException(nameof(umbracoHelper));
        }
    
        public IPublishedContent GetCurrentPage()
        {
            return _umbracoHelper.AssignedContentItem;
        }
    }
    

    And this then is then injected into the service class as:

        private readonly IUmbracoHelper _umbracoHelper;
        public Service(IUmbracoHelper umbracoHelper)
        {
            _umbracoHelper = umbracoHelper ?? throw new ArgumentNullException(nameof(umbracoHelper));
        }
    
  • Javz 38 posts 141 karma points
    May 18, 2020 @ 19:12
    Javz
    0

    DI was not set up properly for the UmbracoHelper. In V7 I could do the following:

    builder.Register(x => new UmbracoHelperWrapper(new UmbracoHelper(UmbracoContext.Current))).As<IUmbracoHelper>().InstancePerRequest();
    

    But with V8, it seems like I would need to populate all the values?

    Any pointers in the correct direction?

    Thanks

Please Sign in or register to post replies

Write your reply to:

Draft