Copied to clipboard

Flag this post as spam?

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


  • John Lilley 3 posts 23 karma points
    Sep 09, 2012 @ 23:40
    John Lilley
    0

    Rendering Umbraco Macros via Code-Behind (4.7, C#)

    Hi All,

    I was wounding if I could ask your expertise and see if there is anybody who can help solve my problem.

    Just to try and paint the picture, I have a Page/Node which has a field called 'pageFunctionality'. This field is of type 'MacroContainer' and what it allows is for business users to add macros to the page within Umbraco which I then render in the template (MasterPage).

    Each 'Macro' in Umbraco (4.7) has a parameter called 'DisplayLocation' and this parameter when the users adds the macro gives them a dropdown list of where the Macro can be placed (I have a method which holds all the ID's of all my Panels and Placeholders in the application  so I can use the 'FindControl' method to add any .Net User Control wherever I like).

    The theory is that the user chooses the DisplayLocation and in the code I can then define where exactly that macro will be placed (anywhere in my masterpage).

    What I need to do is get the field 'pageFunctionality', gets all the macros which have been placed in here (just '.split' this into a string array), find the 'DisplayLocation' and then findcontrol to display the macro on the page.

    Easy right, well nearly, everything is straight forward apart from I am unable to 'Render' the macros back to my page once I have the string from the field (I don't get an error, the macro just does not display).

    Just for testing purposes, for now, all I want to do is get the 'pageFunctionality' field and display this (all from the code-behind of the masterpage, using c#), so I use this:

    Item itemContentMacro = new Item();
    itemContentMacro.ID = "pageFunctionality";
    itemContentMacro.NodeId = iCurrentNodeID.ToString();
    itemContentMacro.Field = "pageFunctionality";

    // Add the Umbraco Item to the MasterPage
    pageGridCol2.Controls.Add(itemContentMacro);

    This works fine, the macro is displayed on the page when rendering through the browser.

    Now what I want to try and do is extract the 'pageFunctiobality' field into a string (just simply a string for now for testing) and the try and insert this string into a control and then add to the masterpage, so I try something like this:

    Item itemContentMacro = new Item();
    itemContentMacro.ID = "pageFunctionality";
    itemContentMacro.NodeId = iCurrentNodeID.ToString();

    // Insert the field content into my string
    string sMyMacro = itemContentMacro.PageElements["pageFunctionality"].ToString();

    // Add the Macro/String to a Literal Control
    Literal literalTest = new Literal();
    literalTest.Text = sMyMacro;

    pageGridCol2.Controls.Add(literalTest);

    I place the MACRO string into a Literal control and then add this to the masterpage. When viewing the page through the browser the macro is NOT displayed.

    The value of the 'sMyMacro' variable is:

    <?UMBRACO_MACRO  macroalias="GoogleMaps"  sGoogleMapsWidth=""  sGoogleMapsHeight=""  bGoogleMapsUseFavIcon="1"  iGoogleMapsZoom=""  sAudience="Public"  />

    All I want to try and do now at this point is wrap this string into a control and have it rendered on the template.

    Hopefully the above makes senses, any ideas how to get the Umbraco Macro from a field, place this into a string and then render it back through the masterpage (ofcourse, after I accomplish this I can process the string further and do what I need to do).

    Thanks in advance for any thoughts or comments.

    Cheers

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Sep 10, 2012 @ 01:02
    Jeroen Breuer
    0
  • John Lilley 3 posts 23 karma points
    Sep 10, 2012 @ 03:23
    John Lilley
    0

    Hi Jeroen,

    Thanks for the reply. I did read through the Hendy Archer's blog the other day, it is similar to what I am trying to achieve but I don't want to have the extra steps of having to create a node in a folder of the site and then having to insert the macro in the body field to then be selectable when a user wants to include the macro on the page/template.

    I want to try and use the Macro Container as once you make the macro available (by creating it in the Developers section) it automatically becomes available (again, without having to create a node with a content field which you then have to insert the macro, I am trying to make the application I am building as scaleable as possible which can be enhanced further over time).

    If I can work out how to insert the macro from the string I get from the field (e.g. <?UMBRACO_MACRO  macroalias="GoogleMaps"  sGoogleMapsWidth=""  sGoogleMapsHeight=""  bGoogleMapsUseFavIcon="1"  iGoogleMapsZoom=""  sAudience="Public"  />) then I should be able to get it working.

Please Sign in or register to post replies

Write your reply to:

Draft