However, having now come back to this after the upgrade I realise I still don't actually know how to create the PublishedContentRequest and assign it to the context so that my rendermacro call will have all the context it needs to get the job done!
I've also realised I might be the first person asking to do exactly this - everyone else is trying to render MVC views I believe?
I managed to get this working by using guesswork (/intuition) - but I'm pretty sure there's a cleaner way (avoiding using a deprecated method for a start!).
Here's my code:
public class PostsApiController : UmbracoApiController
{
public string GetMore(int pageNum)
{
PrepareContext("//nodeName");
var htmlStr = Umbraco.RenderMacro("macro", new { pageNum = pageNum });
return htmlStr.ToString();
}
private void PrepareContext(string contextXPath)
{
var blogContent = Umbraco.TypedContentSingleAtXPath(contextXPath);
var blogUriStr = Umbraco.NiceUrlWithDomain(blogContent.Id);
var blogUri = new Uri(blogUriStr);
var pcr = new PublishedContentRequest(blogUri, UmbracoContext.RoutingContext);
UmbracoContext.PublishedContentRequest = pcr;
UmbracoContext.PublishedContentRequest.Prepare();
}
}
Without the call to Prepare() I got a UmbracoPage not yet initialized error.
Oh. Pretty annoying problem with this is that each time I run this code, the URL of the node I'm using as my context (nodeName) is having an extra '/' appended to the end everywhere it's read! Until you navigate to one of the child nodes, then it goes back to just one '/'.
So the URL is '/nodeName/' on the homepage. Run that code once in the server, then the next page you display has it as: '/nodeName//'. Run it twice and it's: '/nodeName///' - but as soon as you load the next page it goes back to '/nodeName/'.
I assumed that seeing as the PublishedContentRequest was null in my call, it wouldn't matter if I set it as it'd be overwritten later. But this appears not to be the case!?
Render macro inside API
Hi, I just updated to the latest (stable) version of Umbraco (7.4.3) because it sounded like I could use the fixes from this discussion: https://our.umbraco.org/forum/umbraco-7/using-umbraco-7/57240-UmbracoApiController-accessing-RTE-with-Macro-Error#comment-236019 to enable me to call
Umbraco.RenderMacro("")
within my API call.However, having now come back to this after the upgrade I realise I still don't actually know how to create the PublishedContentRequest and assign it to the context so that my rendermacro call will have all the context it needs to get the job done!
I've also realised I might be the first person asking to do exactly this - everyone else is trying to render MVC views I believe?
Can anyone help me with this? Thanks!
I managed to get this working by using guesswork (/intuition) - but I'm pretty sure there's a cleaner way (avoiding using a deprecated method for a start!).
Here's my code:
Without the call to Prepare() I got a
UmbracoPage not yet initialized
error.Oh. Pretty annoying problem with this is that each time I run this code, the URL of the node I'm using as my context (nodeName) is having an extra '/' appended to the end everywhere it's read! Until you navigate to one of the child nodes, then it goes back to just one '/'.
So the URL is '/nodeName/' on the homepage. Run that code once in the server, then the next page you display has it as: '/nodeName//'. Run it twice and it's: '/nodeName///' - but as soon as you load the next page it goes back to '/nodeName/'.
I assumed that seeing as the PublishedContentRequest was null in my call, it wouldn't matter if I set it as it'd be overwritten later. But this appears not to be the case!?
Please help! :S
hi Ian - did you find a solution in the end? thanks
Nope! Put a workaround in to remove the '//' :'-(
is working on a reply...