Copied to clipboard

Flag this post as spam?

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


  • Julius Bartkus 51 posts 76 karma points
    Jan 18, 2011 @ 02:05
    Julius Bartkus
    1

    call and render macro from base

    Hello,

    I am trying to call macro from /base and came up with some solution like: 

    [RestExtension("macro")]
    public class RESTServiceMacro
    {
        [RestExtensionMethod(returnXml = false)]
        public static String getContent(String macroAlias, int pageId)
        {
            try
            {
                macro m = macro.ReturnFromAlias(macroAlias);
    
                Node n = new Node(pageId);
                page p = new page(n.Id, n.Version);
    
                if (m.Properties["rest"] != null)
                {
                    m.Culture = p.GetCulture();
                    m.UICulture = p.GetCulture();
                    Control c = m.renderMacro(new Hashtable(), p.Elements, p.PageID);
                    m.Controls.Add(c);
                    StringWriter output = new StringWriter();
                    m.Server.Execute(m, output, false);
                    return output.ToString();
                }
                else
                {
                    return "access denied";
                }
            }
            catch (Exception ex)
            {
                return "error";
            }
        }
    }

    Does anyone know any better/shorter solution?

    In order to establish some kind of security
    I was looking how to add an attribute to Macro properties tab instead of making workaround and adding a parameter.. I thought it would be nice just to put a checkbox true/false to check wether the macro can be called from /base 

    Any comments are welcome 

    /Julius Bartkus

  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    Jan 18, 2011 @ 09:39
    Matt Brailsford
    3

    He Julius,

    Checkout this thread of someone asking pretty much the same question:

    http://our.umbraco.org/forum/developers/api-questions/16256-Parse-XML-through-XSLT-Engine-in-base

    The answer was to use the umbraco library method RenderMacro

    umbraco.library.RenderMacroContent("<?UMBRACO_MACRO macroAlias=\"macroalias\" ></?UMBRACO_MACRO>", id);

    Cheers

    Matt

  • Julius Bartkus 51 posts 76 karma points
    Jan 18, 2011 @ 16:44
    Julius Bartkus
    0

    Hey Matt,
    thanks for reply.

    I actually have tried this way, but did not succeed. 

    I always got as a result an empty string and so I found this article 

    http://jamesewelch.wordpress.com/2008/07/11/how-to-render-a-aspnet-user-control-within-a-web-service-and-return-the-generated-html/

    as the author stated the problem is there:

    Note, this is within a System.Web.Services.WebService class and not within a System.Web.UI.Page class. Doing this within a System.Web.UI.Page is very simple and you can just create a new instance of your User Control class and render it directly to the page or a string. Since this is within a WebService and not Page class, I discovered that I need to make a mock Page object in order to render the User control as you’ll see below.

    Maybe the method RenderMacroContent can be called only from xslt?

  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    Jan 18, 2011 @ 16:48
    Matt Brailsford
    0

    Hey Julius,

    It should work, but then I guess it depends what you are doing in your macro,

    The second param to RenderMacroContent is the page id that you want to pretend to be basically, so it should have a $currentPage node who's value is set to the XML for the node id passed in. As long as you are accessing page properties only, it should work.

    Matt

  • Jonas Eriksson 930 posts 1825 karma points
    Jan 23, 2011 @ 15:22
    Jonas Eriksson
    0

    Hello,

    could you make it work with your first code sample? I got the same problem with that as I got with RenderMacroContent from a "base" call. A direct call to the function from within a razor macro works fine though. How about that mock Page object - did you try it?

    I made a package a while ago http://our.umbraco.org/projects/website-utilities/rndr-url-macro-renderer that is doing just about this, only without Base - by "cheating" with a template rendering and query string macroalias. It would indeed be cleaner to have the render done in a base function and I hope there's a solution to do so.

    About safety I agree it would be nice to be able to have some additional properties to macros.

    I tried an approach with a simple _ prefix for the macros I liked to be callable "_MyCallableMacro". Yet another approach I tried was to hard code a list of allowed macros in my script (as it is a script it's obviously very easy to edit it).

    Regards

    Jonas

  • Vlad Mikheev 1 post 21 karma points
    Mar 30, 2011 @ 15:37
    Vlad Mikheev
    0

    Hi,

    Just wanted to notice. You can use umbraco.library.RenderMacroContent in /base . But it does not set $currentPage correctly (passed parameter).
    So i had to use macro parameter helpId and than umbraco.library:GetXmlNodeById($helpId).

    Regards, Vlad

  • TimNape 12 posts 32 karma points
    May 25, 2011 @ 07:38
    TimNape
    0

    umbraco.library.RenderMacroContent works for me but when I build a string from C# variables, the RenderMacroContent renders the string I have passed it - any suggestions.

     

     

Please Sign in or register to post replies

Write your reply to:

Draft