Copied to clipboard

Flag this post as spam?

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


  • Jeric 122 posts 192 karma points
    Oct 09, 2014 @ 14:12
    Jeric
    0

    UmbracoApiController accessing RTE with Macro Error

    I'm using the UmbracoApiController to generate an xml feed.

    The error occur when I need to access the RTE property that comes with macro in it.

    It keeps showing "cannot render a macro when there is no current PublishedContentRequest"

    Any work around for this or any way i can strip out the macro?

  • Shannon Deminick 1526 posts 5272 karma points MVP 3x
    Oct 09, 2014 @ 14:14
    Shannon Deminick
    0

    Can you explain how you are generating this xml feed from within your controller?

  • Jeric 122 posts 192 karma points
    Oct 09, 2014 @ 14:24
    Jeric
    0

    I'm using the below where I passed in the node id to get the node
    var theNode = UmbracoContext.Current.ContentCache.GetById(nodeId);

    if(theNode != null)
    {

    var bodyContents = theNode.GetPropertyValue<string>("bodyContent");


    .....the xml string builder....

    and then when i reached the part which needs to pull out the content of the RTE, it fails because some of the bodyContents comes with macro in it. I've tried removing the macro in the bodyContents and it works.

    xmlStrBuild.AppendLine(String.Format("<description><![CDATA[{0}]]></description>", bodyContents)); 

  • Shannon Deminick 1526 posts 5272 karma points MVP 3x
    Oct 10, 2014 @ 00:47
    Shannon Deminick
    2

    GetPropertyValue<string> uses property value converters which is trying to render any macro content found in your rich text editor, which due to legacy reasons requires a current PublishedContentRequest but since REST calls are not part of the Umbraco pipeline, one does not exist.

    So you have a couple of options:

    Lastly, the better way to deal with this would just be to create a PublishedContentRequest, but that isn't a public API until 7.2: http://issues.umbraco.org/issue/U4-5627

  • Burichan 17 posts 129 karma points
    Jun 14, 2017 @ 08:52
    Burichan
    0

    What's gonna happen in case of Vorto?

    theNode.GetVortoProperty("bodyContent").VortoValue;

    Unfortunately no method like the above exists.

    Exactly the same situation. BodyContent is a Vorto RTE, containing a macro.

  • Jeric 122 posts 192 karma points
    Oct 10, 2014 @ 18:34
    Jeric
    0

    Thanks Shannon, working like a charm :)

  • andrew shearer 512 posts 661 karma points
    Mar 20, 2015 @ 04:21
    andrew shearer
    0

    hi Jeric - could you please post a code sample of how you got this working? thanks

  • andrew shearer 512 posts 661 karma points
    Mar 24, 2015 @ 05:53
    andrew shearer
    0

    a working example of this would be really appreciated. thanks

  • Jeric 122 posts 192 karma points
    Mar 24, 2015 @ 10:56
    Jeric
    0

    what i did is, instead of using 

    yourNode.GetPropertyValue<string>("bodyContent")

    use,

    yourNode.GetProperty("bodyContent").Value.ToString() 

     

  • andrew shearer 512 posts 661 karma points
    Mar 24, 2015 @ 20:55
    andrew shearer
    0

    Ok thanks for confirming.

     

    That’s not so simple for me as we have a library heavily tied to Umbraco.Web.PublishedContentExtensions<T> methods. Anyone know an approach that would work using the new property converters?

  • Rodrigo Dias 3 posts 23 karma points
    May 19, 2015 @ 20:36
    Rodrigo Dias
    0

    Hello guys,

    @Shannon, you mentioned in the last post:

    Lastly, the better way to deal with this would just be to create a PublishedContentRequest, but that isn't a public API until 7.2:http://issues.umbraco.org/issue/U4-5627

    However, I'm not sure what to pass to the PublishedCotentRequest constructor. 

    I'm trying to use a UmbracoApiController to expose a method that returns the IHtmlString result of a RenderMacro (cshtml), but it fails due to the lack of PublishedCotentRequest.

    Is there any way to accomplish this task?

    Thanks in advance.

     

  • Otto 8 posts 73 karma points
    Jun 08, 2015 @ 13:50
    Otto
    0

    I'm also interested in the solution. I'm working on a content controller (UmbracoApiController) which retrieves Umbraco content but I can't get it to work with an macro inside RTE field.

    How do I set the PublishedContentRequest? Couldn't figure it out and using the EnsurePublishedContentRequest attribute didn't help either :(

    Thanks!

  • andrew shearer 512 posts 661 karma points
    Jun 08, 2015 @ 23:48
    andrew shearer
    0

    I am still interested in a proper solution to this also. 

     

  • Shannon Deminick 1526 posts 5272 karma points MVP 3x
    Jun 15, 2015 @ 08:45
    Shannon Deminick
    0

    API controller calls have no affiliation with any Umbraco node... since it's just a REST call. Using the EnsurePublishedContentRequestAttribute will not work because that is an MVC specific attribute. You'd have to create an attribute like this for WebApi. The MVC source can be found here:

    https://github.com/umbraco/Umbraco-CMS/blob/dev-v7/src/Umbraco.Web/Mvc/EnsurePublishedContentRequestAttribute.cs

    You could make one for WebApi and that will probably get you closer. The reality is that Umbraco macro's cannot execute currently because of how legacy logic works and they require the current request to be executing in an Umbraco context, with a current routed node assigned. A REST call is just a REST call, there is no Umbraco node assigned to the request.

    Or, in later v7 versions, you can create a PublishContentRequest object yourself and assign it to the current UmbracoContext.

  • Otto 8 posts 73 karma points
    Jun 19, 2015 @ 15:30
    Otto
    0

    Thanks Shannon. Only thing I now encouter is that I can't use the RoutingContext as used in:

    new PublishedContentRequest(UmbracoContext.Current.CleanedUmbracoUrl, UmbracoContext.Current.RoutingContext);
    

    Can't find the reference (is an internal or something?) Any idea how to fix this Shannon? Thanks!

  • Shannon Deminick 1526 posts 5272 karma points MVP 3x
    Jun 23, 2015 @ 09:16
    Shannon Deminick
    0

    Damn, yeah the getting for UmbracoContext.RoutingContext is internal. You'll have to use reflection to get it to make this work.

    Or, you can clone the core and create this attribute there and use a custom build of Umbraco... we'd also love a PR for this attribute in the core, that would be quite helpful!

    I've update the UmbracoContext.RoutingContext property to be publicly gettable in 7.3 now.

  • Otto 8 posts 73 karma points
    Jun 24, 2015 @ 10:44
    Otto
    0

    Great, thanks! Any idea when this fix will be integrated in the beta NuGet package?

  • Otto 8 posts 73 karma points
    Jul 30, 2015 @ 11:43
    Otto
    0

    Nice! With the update of Umbraco 7.3 beta 2 & the EnsurePublishedContentRequestAttribute it works :)

  • Erick W 2 posts 22 karma points
    Dec 09, 2015 @ 22:23
    Erick W
    0

    I have this problem but the fix mentioned isn't working....I get the exact same macro rendering error in the original post, using the following code:

                var speakerlist = UmbracoContext.ContentCache.GetByXPath("//self::Speaker");
            foreach (var source in speakerlist)
            {
    
                ExportSpeaker result = new ExportSpeaker();
    
                if (source.GetProperty("Speaker_biographyText").Value != null)
                {
                   result.Spea_full_bio = source.GetProperty("Speaker_biographyText").Value.ToString();
                }}
    

    Shouldn't that "GetProperty" not give me the error ("Cannot render a macro when there is no current PublishedContentRequest.") like you mention? I'm on 7.2.5, and trying to do this inside a API controller so I can export a list of our entries for another system to read in.

    Any advice on how to fix this?

  • Otto 8 posts 73 karma points
    Dec 11, 2015 @ 12:40
    Otto
    0

    Hey Erick,

    The solution mentioned above by Shannon is working in combination with Umbraco 7.3 or higher.

  • Romeo Cadaoas 33 posts 110 karma points
    Jun 23, 2016 @ 09:31
    Romeo Cadaoas
    1

    Get it's raw value by accessing it through properties. like so:

    x.Properties.FirstOrDefault(p => p.PropertyTypeAlias == "bodyContent").DataValue.ToString()
    
  • Ian Grainger 71 posts 135 karma points
    Jul 05, 2016 @ 15:30
    Ian Grainger
    0

    I'm trying to do something similar by creating a publishedContentRequest so that I can render a macro. I've nearly got it working but now I'm having a really weird issue of '/'s being appended to the url of the node I care about!

    I made a thread for it here: https://our.umbraco.org/forum/extending-umbraco-and-using-the-api//78463-render-macro-inside-api#comment-78463

    If anyone can give me any insight please do!

  • Michael Osborn 2 posts 71 karma points
    Sep 28, 2017 @ 22:28
    Michael Osborn
    0

    Hey Guys, I understand that this is mega late in the game but this is how I am getting around the issues ( be gentle with me its my first post )

    var umbracoHelper = new UmbracoHelper(UmbracoContext);
    var page = umbracoHelper.Content( Id );
    
    UmbracoContext.PublishedContentRequest = 
                    new PublishedContentRequest(url,  UmbracoContext.RoutingContext);
    
     UmbracoContext.PublishedContentRequest.Prepare();
    
     var publishedContent = UmbracoContext.PublishedContentRequest.PublishedContent;
    

    I have a form macro in content that I tested this with and it seems to render it just fine.

    Id = Id of content url = Uri of content that I make up from page and Request.RequestUri

  • Andrew Lansdowne 43 posts 124 karma points
    Oct 08, 2021 @ 09:49
    Andrew Lansdowne
    0

    Thanks Michael this helped me a lot to get PublishedContentRequest populated in a core library outside of a page/api request using the following code to trick it into using the context of the homepage -

    UmbracoContext.Current.PublishedContentRequest = new PublishedContentRequest(new Uri("https://mysite.localhost/"), UmbracoContext.Current.RoutingContext);
    UmbracoContext.Current.PublishedContentRequest.Prepare();
    

    Cheers Andy

Please Sign in or register to post replies

Write your reply to:

Draft