Kentico 8.2 Registering custom macro methods - not working

Bobby Ortega asked on July 2, 2015 02:14

Hi to all,

I'm exploring the creation of custom macro. I've been following the steps and samples here:

but "ConnectStrings" is always missing and not available in the macro editor.

Please enlighten me if I missed something.

using System;

using CMS;
using CMS.MacroEngine;
using CMS.Helpers;

// Makes all methods in the 'CustomMacroMethods' container class available for string objects
[assembly: RegisterExtension(typeof(CustomMacroMethods), typeof(string))]
// Registers methods from the 'CustomMacroMethods' container into the "String" macro namespace
[assembly: RegisterExtension(typeof(CustomMacroMethods), typeof(StringNamespace))]
public class CustomMacroMethods : MacroMethodContainer {
    [MacroMethod(typeof(string), "Combines two strings, or appends a culture suffix when called with one parameter.", 1)]
    [MacroMethodParam(0, "param1", typeof(string), "First part of the string.")]
    [MacroMethodParam(1, "param2", typeof(string), "Second part of the string (optional).")]
    public static object ConnectStrings(EvaluationContext context, params object[] parameters) {
        // Branches according to the number of the method's parameters
        switch (parameters.Length) {
            case 1:
                // Overload with one parameter
                return ValidationHelper.GetString(parameters[0], "") + " - Resolved in culture: " + context.Culture;

            case 2:
                // Overload with two parameters
                return ValidationHelper.GetString(parameters[0], "") + " - " + ValidationHelper.GetString(parameters[1], "");

                // No other overloads are supported
                throw new NotSupportedException();

Recent Answers

Tim Ferres answered on July 2, 2015 13:09

I created a new file (CustomMacroMethod.cs) within the App_Code folder and cut and paste the provided code. I noticed that a closing } character for the class was missing and after adding this in and refreshing the site (a recompile occurred) the macro worked as expected.

To test: add a static text web part to a page template, within the Text field, click the "edit in new window" button, start typing "{% CurrentUser.UserName.C" and "ConnectStrings" should show in the list of options. If its still not working, you could also try forcing a recompile of the site by making a small change to a source file within the App_Code folder and re-run the test.

1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.