Extending macro engine using MacroField

Peter Otmar asked on May 17, 2018 12:33

Hi I'm trying to register extra fields extending CMS.Membership.CurrentUserInfo. I have an object called Member which has some properties like strings, dates etc but also lists.

Currently I can add all top level properties by cycling through the object properties.

PropertyInfo[] properties = memberProfileResponse.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo property in properties)
{
    object value = property.GetValue(memberProfileResponse, null);
    MacroField macroField = new MacroField(property.Name, () => value);
    RegisterField(macroField);
}

How do I add lists as a sub-level in the macro tree. I have tried to add the whole object but there is no tree structure. Is there a way to add something like folder structure similar to the {% CurrentUser.Orders %} for example.

Appreciate any help.

Thanks Peter

Recent Answers


David te Kloese answered on May 18, 2018 16:04

Hi,

best I could come up with for users:

[assembly: RegisterExtension(typeof(CustomMacroFields), typeof(UserInfo))]
public class CustomMacroFields : MacroFieldContainer
{
    protected override void RegisterFields()
    {
        base.RegisterFields();

        RegisterField(new MacroField("customProperty", x => x.User.GetValue("customProperty")));
        RegisterField(new MacroField("customProperty2", x => x.User.GetValue("UserName")));


    }
}

result:

Image Text

0 votesVote for this answer Mark as a Correct answer

Peter Otmar answered on May 20, 2018 00:16

Thanks David

I looked into it and managed to get it working implementing "CMS.Base.IDataContainer" interface. Then added my provider which will get the object.

[assembly: RegisterExtension(typeof(MemberProfileMacroFields), typeof(UserInfo))]
namespace RACGP.Kentico.Modules.Macros
{
    /// <summary>
    /// Summary description for MemberProfileFields
    /// </summary>
    public class MemberProfileMacroFields : MacroFieldContainer
    {
        protected override void RegisterFields()
        {
            base.RegisterFields();

            RegisterField(new MacroField("MemberProfile", () => MemberProfileProvider.GetMemberProfileByUserName(MembershipContext.AuthenticatedUser.UserName)));
        }
    }
}

Bit of work, but well worth it. I couldn't find any examples in the Kentico documentation but I think lot of people would find it useful.

Peter

0 votesVote for this answer Mark as a Correct answer

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