Briefly about custom macro fields

In this post I want to provide a bit more details regarding how to add custom macro fields to existing object types.

In Kentico documentation we have good explanation how to create and use custom macro field for the existing object type. But documentation examples often use hardcoded username value to retrieve, for example, user information. In real life things can be more complicated.

Let's consider the scenario. We have Kentico Order object type. And we created custom module and classes to manage customer's credit cards information (for example it can be last four digits for credit card number, credit card expiration date, etc.). Custom object type to store credit card information is called CreditCard. Also additional column was added to the object type Order to store credit card ID just to know which credit card customer used to pay for order. Now we need to display credit card information in order invoice or sent emails to customer with order details and credit card information used for payment. So what is the best way to insert this information into invoice? I think the answer is to use custom macros.

As credit card object is tied to order by ID so credit card object information can be field of order object. The main issue was to identify context of specific order object and get credit card ID stored within the currently used order object (for example, when we edit order information and view order invoice). The code sample below demonstrate how to register custom field for credit card.

[assembly: RegisterExtension(typeof(CustomMacroFields), typeof(OrderInfo))]

public class CustomMacroFields : MacroFieldContainer
    protected override void RegisterFields()
        RegisterField(new MacroField("CreditCard", (context) =>
            //context.Resolver.SourceObject is used to retrieve data of currently used object
            ShoppingCartInfo si = context.Resolver.SourceObject as ShoppingCartInfo;
            if (si != null && si.Order != null)
                return CreditCardDataInfoProvider.GetCreditCardDataInfo(si.Order.GetIntegerValue("OrderCreditCardID", 0));

            return context.DefaultValue;

So as you can see the example is quite simple, but the main focus is how to get appropriate object data from context parameter of type EvaluationContext. Now all properties of Credit card object can be easily retrieved as macro fields.

waqar mazhar
good post
10/31/2017 4:26:47 PM

تور تایلند
nice information and very informative thanks for share this stuff!
10/25/2017 12:29:02 PM

Add To Cart Soft often the first treatment tried for erectile dysfunction in men and .
8/10/2017 9:04:23 AM

assignment writing help australia
They are not able to finish the writing assignments on time. For some students, writing any writing assignments is able to waste their time
7/6/2017 7:15:41 PM

French Open
https://thefrenchopen.org/ The French Open is a major tennis tournament held over two weeks at Stade Roland Garros in Paris, France. Watch French Open 2017 Live Stream Free Online.
5/29/2017 6:24:54 AM

5/24/2017 3:06:32 AM

[url=http://www.vipmontre.fr/]replique montre suisse[/url]
5/24/2017 3:06:02 AM

Great Article it its really informative and innovative keep us posted with new updates. its was really valuable. thanks a lot.<a href="http://www.90rolex.it/">orologi replica</a> very good.
5/24/2017 3:05:12 AM

hot sale replica watches
Both cheap and gorgeous fine imitation watches;<a href="http://www.uamoevent.com/">imitatie rolex horloges</a> filling your success style!
5/11/2017 6:45:30 AM

nike shoes
Great Article it its really informative and innovative keep us posted with new updates. its was reallyvaluable. thanks a lot.<a href="http://www.kreaspacio.com/">orologi replica</a>
<a href="http://www.torico.co.uk/">Nike Shoes</a> .
5/9/2017 6:35:45 AM

resume writing services reviews
The number of people are having programming skills and have a great reputation in the company where they work. I really glad to found your amazing website and that would help lots of people to boost their programming coding skills.
5/4/2017 2:12:16 PM

Mariia Zhokh
I am extending OrderInfo as I use this macro in Order Invoice like {%Order.CreditCard.CardNumber|(user)admin|(hash)c4c7a2ffefbcf2a70eb96163ed68f0994eb0c7e8275357e9ef7ca03d48c230d2%}. But while debugging this context.Resolver.SourceObject is of ShoppingCartInfo type. That's why I am getting ShoppingCart in this context.
2/23/2017 9:04:05 AM

Piyush Kothari
I see you are extending OrderInfo, but how come you are getting Shopingcart in the context, shouldn't you get order?
9/27/2016 4:27:14 PM

 Security code