Copied to clipboard

Flag this post as spam?

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


  • Ian Grainger 71 posts 135 karma points
    Jul 04, 2016 @ 16:21
    Ian Grainger
    0

    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!

  • Ian Grainger 71 posts 135 karma points
    Jul 05, 2016 @ 13:58
    Ian Grainger
    0

    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.

  • Ian Grainger 71 posts 135 karma points
    Jul 05, 2016 @ 15:14
    Ian Grainger
    0

    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

  • andrew shearer 506 posts 653 karma points
    Dec 01, 2016 @ 23:20
    andrew shearer
    0

    hi Ian - did you find a solution in the end? thanks

  • Ian Grainger 71 posts 135 karma points
    Dec 02, 2016 @ 09:06
    Ian Grainger
    0

    Nope! Put a workaround in to remove the '//' :'-(

Please Sign in or register to post replies

Write your reply to:

Draft