Create custom macro method returning a custom class

Kenny Deblaere asked on August 22, 2016 11:59

Hello

I've written a CustomMacroMethod, which is returning a class, looking like:

public class ReturningObject {
    public string Name {get; set;}
    public int Id {get; set;}
}

In my CustomMacroMethod, I have the following code:

[assembly: RegisterExtension(typeof(CustomContactMacroMethod), typeof(ReturningObject))]
namespace CMSAppAppCode.Old_App_Code
{
    public class CustomContactMacroMethod : MacroMethodContainer
    {
        /// <summary>
        /// Function to get the object
        /// </summary>
        /// <param name="context"></param>
        /// <param name="parameters"></param>
        /// <returns>Returns the object</returns>
       [MacroMethod(typeof(ReturningObject), "Returns the current office", 1)]
       [MacroMethodParam(0, "CurrentPage", typeof(ReturningObject), "The current rendered page")]
       public static ReturningObject GetObject(EvaluationContext context, params object[] parameters)
       {
            var object = new ReturningObject {
                Name = "test",
                Id = 0
             };

            return object;
       }
    }
}

When I in Kentico asks for GetObjecc(currentDocument).Name, it returns an empty string.

Is there something I do wrong?

Recent Answers


Fabian Haeger answered on August 22, 2016 13:40 (last edited on August 22, 2016 13:43)

Is this just a typo?

GetObjecc(currentDocument).Name

Given your definiton of the macro it should be

GetObject(currentDocument).Name

Additional to that, you expect a ReturningObject as an parameter, while currentDocument is definitly not a ReturningObject. The Parameter should more look like something like this:

[MacroMethodParam(0, "CurrentPage", typeof(TreeNode), "The current rendered page")]
0 votesVote for this answer Mark as a Correct answer

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