Copied to clipboard

Flag this post as spam?

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


  • Murray Roke 502 posts 965 karma points c-trib
    Nov 11, 2015 @ 04:22
    Murray Roke
    0

    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 ...

  • Marc Goodson 2126 posts 14218 karma points MVP 8x c-trib
    Nov 11, 2015 @ 07:47
    Marc Goodson
    1

    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 ?

  • Murray Roke 502 posts 965 karma points c-trib
    Nov 11, 2015 @ 19:42
    Murray Roke
    0

    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();
            }
    
  • Murray Roke 502 posts 965 karma points c-trib
    Nov 11, 2015 @ 20:56
    Murray Roke
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft