Calling custom macros from REST interface

Bryan Johnson asked on November 15, 2017 20:45

Is it possible to access custom macro methods through the REST interface? Requesting system macros works fine. Here is an example:

https://mysite.com/rest/macro/CurrentSite.SiteName

But a request to a custom macro generates a 404 when targeted from the REST interface. Example:

https://mysite.com/rest/macro/myCustomMacroClass.myCustomMacro

Is there some additional configuration necessary to expose custom macros to the REST interface? Or is it just not possible? Thanks.

Platform: Kentico 10

Recent Answers


David te Kloese answered on November 17, 2017 09:13

I'm not 100% sure but: https://docs.kentico.com/k10/integrating-3rd-party-systems/kentico-rest-service/getting-data-using-rest#GettingdatausingREST-Dataloadingparameters

says:

/macro/<expression>
/macro/CurrentSite.SiteName     

Evaluates the given macro expression and serializes the result. Only works for macros that return a serializable result.

When creating the macro expression:

  • Do NOT add the encapsulating bracket characters
  • Use URL encoding for forbidden URL characters

Can you verify the above?

0 votesVote for this answer Mark as a Correct answer

Bryan Johnson answered on November 20, 2017 21:01

Thanks, David. I think I'm having trouble before I get to the serialization details. The method signature seems to be unreachable since it throws a 404. I wish the Kentico documentation included an example. Below is my test code. I'm attempting to reach the method using the following request on my dev environment:

https://127.0.0.1/rest/macro/TestCustomMacro.TestMacro

Do you think I should call the method using "TestCustomMacro.TestMacro" as defined by my class and method names? Or am I doing this wrong? Thanks!

using CMS;
using CMS.MacroEngine;
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Xml.Serialization;


/// <summary>
/// Summary description for TestCustomMacro
/// </summary>
// Makes all methods in the 'TestCustomMacro' container class available for string objects
[assembly: RegisterExtension(typeof(TestCustomMacro), typeof(string))]
// Registers methods from the 'TestCustomMacro' container into the "String" macro namespace
[assembly: RegisterExtension(typeof(TestCustomMacro), typeof(CMS.MacroEngine.SystemNamespace))]
public class TestCustomMacro : MacroMethodContainer
{

    [MacroMethod(typeof(string), "Test macro", 0)]
    public static object TestMacro(EvaluationContext context, params object[] parameters)
    {
        clsTestObject testObject = new clsTestObject();
        testObject.PropA = "valueA";
        testObject.PropB = "valueB";
        testObject.PropC = "valueC";


        XmlSerializer xmlSerializer = new XmlSerializer(testObject.GetType());
        using (StringWriter textWriter = new StringWriter())
        {
            xmlSerializer.Serialize(textWriter, testObject);
            return textWriter.ToString();
        }
    }

    //[MacroMethod(typeof(clsTestObject), "Test macro 2", 0)]
    //public static object TestMacro2(EvaluationContext context, params object[] parameters)
    //{
    //    clsTestObject testObject = new clsTestObject();
    //    testObject.PropA = "valueA";
    //    testObject.PropB = "valueB";
    //    testObject.PropC = "valueC";

    //    return testObject;
    //}

}

[Serializable()]
public class clsTestObject : ISerializable
{
    public string PropA;
    public string PropB;
    public string PropC;

    public clsTestObject()
    {
        PropA = "initA";
        PropB = "initB";
        PropC = "initC";
    }

    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("PropA", PropA);
        info.AddValue("PropB", PropB);
        info.AddValue("PropC", PropC);
    }
}
0 votesVote for this answer Mark as a Correct answer

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