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
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?
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.
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).
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).
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.
call and render macro from base
Hello,
I am trying to call macro from /base and came up with some solution like:
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
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
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?
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
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
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
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.
is working on a reply...