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.
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:
Use theNode.GetProperty("bodyContent").Value; - IIRC this does not use value converters and will return the raw string
You can use a known hack to assign a PublishedContentRequest using the class: EnsurePublishedContentRequestAttribute, to assign a new PublishedContentRequest to the UmbracoContext. An example is in Articulate here on these 2 lines:
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
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?
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.
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 :(
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:
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.
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.
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.
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!
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
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();
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?
Can you explain how you are generating this xml feed from within your controller?
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));
}
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 currentPublishedContentRequest
but since REST calls are not part of the Umbraco pipeline, one does not exist.So you have a couple of options:
theNode.GetProperty("bodyContent").Value;
- IIRC this does not use value converters and will return the raw stringPublishedContentRequest
using the class:EnsurePublishedContentRequestAttribute
, to assign a newPublishedContentRequest
to theUmbracoContext
. An example is in Articulate here on these 2 lines: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
What's gonna happen in case of Vorto?
Unfortunately no method like the above exists.
Exactly the same situation. BodyContent is a Vorto RTE, containing a macro.
Thanks Shannon, working like a charm :)
hi Jeric - could you please post a code sample of how you got this working? thanks
a working example of this would be really appreciated. thanks
what i did is, instead of using
yourNode.GetPropertyValue<string>("bodyContent")
use,
yourNode.GetProperty("bodyContent").Value.ToString()
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?
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.
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!
I am still interested in a proper solution to this also.
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.
Thanks Shannon. Only thing I now encouter is that I can't use the RoutingContext as used in:
Can't find the reference (is an internal or something?) Any idea how to fix this Shannon? Thanks!
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.
Great, thanks! Any idea when this fix will be integrated in the beta NuGet package?
Nice! With the update of Umbraco 7.3 beta 2 & the EnsurePublishedContentRequestAttribute it works :)
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:
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?
Hey Erick,
The solution mentioned above by Shannon is working in combination with Umbraco 7.3 or higher.
Get it's raw value by accessing it through properties. like so:
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!
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 )
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
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 -
Cheers Andy
is working on a reply...