Copied to clipboard

Flag this post as spam?

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


  • progproger 52 posts 130 karma points
    Aug 19, 2019 @ 16:06
    progproger
    0

    How to get macro content in UmbracoApiController v8

    Hi,

    I'm trying to get macro content in one of the api methods using:

    Current.UmbracoHelper.RenderMacro("macroalias");

    but getting an error:

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

    Can someone please point me the correct way?

  • progproger 52 posts 130 karma points
    Aug 27, 2019 @ 13:02
    progproger
    101

    For those who has the same issue or will ever have...

    Not sure if it's the best way, but i've created new instance of UmbracoHelper and that helped:

    UmbracoHelper helper = new UmbracoHelper(umbPage, Umbraco.TagQuery, _dicFactory, _compRenderer, Umbraco.ContentQuery, Current.UmbracoHelper.MembershipHelper);
    

    where _dicFactory and _compRenderer are injected to existing api controller...

    public UmbracoContentApiController(ICultureDictionaryFactory dicFactory, IUmbracoComponentRenderer compRenderer)
            {
                _dicFactory = dicFactory;
                _compRenderer = compRenderer;
            }
    

    Then you can easily render macro using:

    var macro = helper.RenderMacro("MacroAlias");
    
  • Michele Di Maria 34 posts 239 karma points
    Mar 04, 2021 @ 19:52
    Michele Di Maria
    2

    Thanks, this saved my day!!

Please Sign in or register to post replies

Write your reply to:

Draft