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 467 posts 875 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 1451 posts 9716 karma points MVP 5x 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 467 posts 875 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 467 posts 875 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

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies