Copied to clipboard

Flag this post as spam?

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


  • Tim 1193 posts 2675 karma points MVP 4x c-trib
    Aug 12, 2010 @ 17:03
    Tim
    0

    Get Macro Output in Code Behind

    Righty,

    Does anyone know if its possible to render the output of a Macro in the code behind of a page? I have an idea about using a Macro to render previews of alert emails (the emails are based on content, so a macro would be a neat solution for generating the html for the email). However, I need to be able to get the rendered contet of the Macro as part of the email sending code, so that I can use it.

    Creating a new Macro in the code behind isn't a problem, but it's rendering it that I'm struggling with. Does anyone have any ideas?

    :)

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Aug 12, 2010 @ 17:07
    Matt Brailsford
    0

    Hey Tim,

    Would this work? (Assuming macro inherits from control)

    public string RenderControl(Control ctrl) 
    {
        StringBuilder sb = new StringBuilder();
        StringWriter tw = new StringWriter(sb);
        HtmlTextWriter hw = new HtmlTextWriter(tw);
    
        ctrl.RenderControl(hw);
        return sb.ToString();
    }

    Matt

  • Tim 1193 posts 2675 karma points MVP 4x c-trib
    Aug 12, 2010 @ 17:54
    Tim
    0

    Hey dude!

    That's what I'd been trying, but I realised I was using the wrong reference to Macro! Your code made me realise that I was using the wrong reference, ta :)

    If anyone else needs to do this in their back office pages:

    - use the reference: using umbraco.presentation.templateControls to get to the Macro object, this won't work with the one in umbraco.businesslogic

    - the macro that you load doesn't have access to currentPage in it (as there's no current page if you're in the back office, haven't figured out a way to pass in a fake current page yet, though I don't need it for this, as I'm passing in the id of the page I want to use in the email)

    If anyone's interested, here's a simple example, passing in a simple macro attribute:

    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using umbraco.presentation.templateControls;
    using System.IO;
    using System.Text;
    
    public partial class umbraco_epiphany_alertPreview : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Macro test = new Macro();
    
            test.Alias = "EmailPreview";
            test.MacroAttributes.Add("alertpage", "1234");
    
            StringBuilder sb = new StringBuilder(); 
            StringWriter tw = new StringWriter(sb); 
            HtmlTextWriter hw = new HtmlTextWriter(tw); 
            test.RenderControl(hw); 
            Response.Write(sb.ToString());
    
            tw.Dispose();
            hw.Dispose();
        }
    }
Please Sign in or register to post replies

Write your reply to:

Draft