Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
How do I create a macro programatically? I want to create a migration in code rather than going to the Developer section, right clicking Macros > Create ... etc ...
Hi Murray
There is a MacroService that you can reference and a Macro class that you can create new Macros with:
The gist of which is something like this:
var ms = ApplicationContext.Current.Services.MacroService; var alreadyCreatedMacro = ms.GetByAlias("aliasName"); if (alreadyCreatedMacro == null) { // new macro overload: (string alias, string name, [string controlType = ""], [string controlAssembly = ""], [string xsltPath = ""], [string scriptPath = ""], [bool cacheByPage = false], [bool cacheByMember = false], [bool dontRender = true], [bool useInEditor = false], [int cacheDuration = 0]) var newMacro = new Macro("myNewMacro", "MY New Macro"); ms.Save(newMacro); }
if that helps get you started ?
Macro is Internal, so I get "Macro is inaccessible due to it's protection level"
Figured out I can use the old API
if (Macro.GetByAlias("InsertImage") == null) { var newMacro = Macro.MakeNew("Insert Image"); newMacro.Name = "Insert Image"; newMacro.Alias = "InsertImage"; newMacro.ScriptingFile = "~/Views/MacroPartials/InsertImage.cshtml"; newMacro.UseInEditor = true; newMacro.RenderContent = false; newMacro.Save(); MacroProperty.MakeNew(newMacro, "mediaId", "Image", "Umbraco.MediaPicker").Save(); MacroProperty.MakeNew(newMacro, "caption", "Caption", "Umbraco.Textbox").Save(); MacroProperty.MakeNew(newMacro, "alignment", "Alignment", "MacroAlignmentPickerDataType").Save(); MacroProperty.MakeNew(newMacro, "width", "Width", "MacroWidthPickerDataType").Save(); }
Also worth noting the convention for creating something with the service api is like so:
ApplicationContext.Current.Services.ContentService.CreateContent(...);
the macro service should be similar:
ApplicationContext.Current.Services.MacroService.CreateMacro(....)
but no such method exists.
I've created a youtrack issue here: http://issues.umbraco.org/issue/U4-7390
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
Create Macro Programatically
How do I create a macro programatically? I want to create a migration in code rather than going to the Developer section, right clicking Macros > Create ... etc ...
Hi Murray
There is a MacroService that you can reference and a Macro class that you can create new Macros with:
The gist of which is something like this:
if that helps get you started ?
Macro is Internal, so I get "Macro is inaccessible due to it's protection level"
Figured out I can use the old API
Also worth noting the convention for creating something with the service api is like so:
the macro service should be similar:
but no such method exists.
I've created a youtrack issue here: http://issues.umbraco.org/issue/U4-7390
is working on a reply...