Loading

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()
    {
        base.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.


Comments
help with assignment
They are no longer in a position according to end the literature assignments on time. For half students, composition any literature assignments is capable according to abuse theirs time
2/16/2018 5:32:26 AM

essay paper help
They are not able to finish the writing assignments on time. For some students, writing any writing assignments is able to waste their time
2/12/2018 5:54:32 AM

easy
Nice post.
1/15/2018 4:23:55 AM

luora
These all reason are the best that we must play the puzzle game and this game make our mind the sharp or <a href="http://www.ukessayservicesreviews.com/uk-superiorpapers-com-review/">superior paper writing service reviews</a> bring into existence and do the help how we play the puzzle the game because this game make the child sharp or active and best.
1/15/2018 4:22:51 AM

Basketball Legends game
Thanks a lot for such a deep beefing. i get each and every point.
11/23/2017 8:27:46 PM

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

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

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